
正文
javascript通过改变滚动条滚动来显示某些元素的scrollIntoView()方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
scrollIntoView(b)可以在任何HTML上调用,通过滚动滚动条,调用的元素就可以出现在可视区域.
参数如果是true,或者不传参数,则表示调用元素的顶部与浏览器顶部平齐.
如果传入false,调用元素会尽可能出项在视口中.
HTML代码:
<input type="button" value="显示红色区域" onclick="displayRed()"/>
<div style="width:100%; height:2000px;"></div>
<div style="width:100%; height:100px; background-color:Red;" id="guoDiv"></div>
<div style="width:100%; height:2000px;"></div>
//传入参数时true或不传参数时:
function displayRed() {
document.getElementById("guoDiv").scrollIntoView(true);
}
点击按钮后:

//传入参数false时:
function displayRed() {
document.getElementById("guoDiv").scrollIntoView(false);
}
点击按钮后:








