h5中控制元素滚动到指定位置。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
#myDIV {
height: 250px;
width: 250px;
overflow: auto;
background: green;
}
#content {
margin:500px;
height: 800px;
width: 2000px;
background-color: coral;
position: relative;
}
</style>
</head>
<body>
<p>单击按钮以滚动到元素的顶部或底部</p>
<button onclick="toTop()">滚动到元素的顶部</button>
<button onclick="toBottom()">滚动到元素的底部</button>
<button onclick="toTopTrans()">到顶部加动画</button>
<div id="myDIV">
<div id="content">
<div style="position:absolute;top:0;">顶部</div>
<div style="position:absolute;bottom:0">顶部</div>
</div>
</div>
<script>
var elmnt = document.getElementById("content");
function toTop() {
elmnt.scrollIntoView(true);
}
function toBottom() {
elmnt.scrollIntoView(false);
}
function toTopTrans(){
elmnt.scrollIntoView({
behavior:'smooth'
})
}
</script>
</body>
</html>