scrollIntoView
scrollIntoView(ture)元素上边框与视窗顶部齐平scrollIntoView(false)元素下边框与视窗底部齐平
#myDiv {height: 900px;background-color: gray; }#roll_top {height: 900px;background-color: green;color: #FFF;font-size: 50px;position: relative; }#bottom {position: absolute;display: block;left: 0;bottom: 0; }
window.Onload= function () { document.querySelector("#roll1").Onclick= function () { document.querySelector("#roll_top").scrollIntoView(false); }; document.querySelector("#roll2").Onclick= function () { document.querySelector("#roll_top").scrollIntoView(true); }; }
scroll
floor1floor2floor3floor4floor5页面结构
测试1测试2测试3测试4测试5
.main div {height: 1000px;width: 300px;margin: 20px;background-color: #C0C0C0; }#nav {position: fixed;width: 100px;height: 200px;top: 40%;right: 10px; }#nav div {cursor: pointer;text-align: center; }
$(function () { $(window).scroll(function () {//为页面添加页面滚动监听事件var wst = $(window).scrollTop(); //滚动条距离顶端值for (var i = 1; i <6; i++) { //加循环if ($("#f" + i).offset().top <= wst + 10) { //判断滚动条位置$(&#39;#nav div&#39;).css("background-color", "white"); $(".f" + i).css("background-color", "red"); } } }); $(&#39;#nav div&#39;).click(function () { $(&#39;html,body&#39;).animate({scrollTop: $("#" + this.className).offset().top}, 500);// $("#" + this.className)[0].scrollIntoView(true);//h5 scrollIntoView()}); });
全部代码
scrollIntoView
scrollIntoView(ture)元素上边框与视窗顶部齐平scrollIntoView(false)元素下边框与视窗底部齐平scroll
floor1floor2floor3floor4floor5页面结构
测试1测试2测试3测试4测试5
以上就是h5之scrollIntoView用法详解的详细内容,更多请关注其它相关文章!