javascript通过改变滚动条滚动来显示某些元素的scrollIntoView()方法
scrollIntoView(b)可以在任何HTML上调用,通过滚动滚动条,调用的元素就可以出现在可视区域.
参数如果是true,或者不传参数,则表示调用元素的顶部与浏览器顶部平齐.
如果传入false,调用元素会尽可能出项在视口中.
HTML代码:
1 <input type="button" value="显示红色区域" onclick="displayRed()"/> 2 <div style="width:100%; height:2000px;"></div> 3 <div style="width:100%; height:100px; background-color:Red;" id="guoDiv"></div> 4 <div style="width: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 }
点击按钮后: