https://developer.mozilla.org/en-US/...ment/scrollTop
https://developer.mozilla.org/en-US/...dingClientRect
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#two {
width: 100%;
height: 150px;
overflow: auto;
}
li {
display: block;
height: 150px;
color: #fff;
text-align: center;
}
</style>
</head>
<body>
<div id="two">
<ul>
<li style="background-color: tomato">1</li>
<li style="background-color: purple; height: 500px;">2</li>
<li style="background-color: blue">3</li>
</ul>
</div>
<script>
function showItem(x) {
var two = document.getElementById('two'),
item = document.querySelectorAll('#two ul li')[x-1];
if (!item) {
return false;
}
var y = item.getBoundingClientRect().y - two.getBoundingClientRect().y;
two.scrollTop = y;
}
showItem(2);
</script>
</body>
</html>