<html>
<head></head>
<body>
<div>
<ul>
<li>
<span>오렌지 좋구요<span>
</li>
<li>
<span>바나도 좋구요</span>
</li>
<li>
<span>민트초코는 너무 좋구요</span>
</li>
</ul>
<button onclick="focus_element()">focus</button>
</div>
</body>
</html>
body {
display:flex;
justify-content: center;
align-items: center;
width: 70%;
height: 100vh;
margin: 0 auto;
}
ul {
width: 200px;
height: 100px;
list-style: none;
border: 1px solid black;
overflow: scroll;
}
li {
margin: 5px;
padding: 5px;
background-color: yellow;
font-size: 20px;
display:flex;
}
span {
flex: 0 1 auto;
}
let cnt = 1;
function focus_element() {
cnt++;
let ul = document.querySelector("ul");
let li = document.createElement("li");
li.textContent = "편식은 나빠요. 골고루 먹어야 180이상 큰답니다.";
ul.append(li);
let li_list = document.querySelectorAll('li');
li_list[li_list.length - 1].scrollIntoView({behavior: "smooth"});
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.