javascript通过改变滚动条滚动来显示某些元素的scrollIntoView()方法

scrollIntoView(b)可以在任何HTML上调用,通过滚动滚动条,调用的元素就可以出现在可视区域.
参数如果是true,或者不传参数,则表示调用元素的顶部与浏览器顶部平齐.
如果传入false,调用元素会尽可能出项在视口中.

HTML代码:

1 <input type="button" value="显示红色区域"  onclick="displayRed()"/>
2 <div style="100%; height:2000px;"></div>
3 <div style="100%; height:100px; background-color:Red;" id="guoDiv"></div>
4 <div style="100%; height:2000px;"></div>
1 //传入参数时true或不传参数时:
2         function displayRed() {
3             document.getElementById("guoDiv").scrollIntoView(true);
4         }

点击按钮后:

1 //传入参数false时:
2         function displayRed() {
3             document.getElementById("guoDiv").scrollIntoView(false);
4         }

点击按钮后:

原文地址:https://www.cnblogs.com/guoyansi19900907/p/3637621.html