如果滚动页面也是dom没有解决的一个问题。为了解决这个问题,浏览器实现了一下方法,以方便开发人员如何更好的控制页面的滚动。在各种专有方法中,html5选择了scrollintoview()作为标准方法。scrollintoview()可以在所有的html元素上调用,通过滚动浏览器窗口或某个容器元素,调用元素就可以出现在视窗中。如果给该方法传入true作为参数,或者不传入任何参数,那么窗口滚动之后会让调动元素顶部和视窗顶部尽可能齐平。如果传入false作为参数,调用元素会尽可能全部出现在视口中(可能的话,调用元素的底部会与视口的顶部齐平。)不过顶部不一定齐平.
一、scrollIntoView
html
scrollIntoView
scrollIntoView(ture)元素上边框与视窗顶部齐平scrollIntoView(false)元素下边框与视窗底部齐平
登录后复制
css
#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; }
登录后复制
js
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); }; }
登录后复制
二、滚动监听
html
scroll
页面结构
测试1测试2测试3测试4测试5
登录后复制
css
.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; }
登录后复制
js
$(function () { $(window).scroll(function () {//为页面添加页面滚动监听事件var wst = $(window).scrollTop(); //滚动条距离顶端值for (var i = 1; i < 6; i++) { //加循环if ($("#f" + i).offset().top <= wst + 10) { //判断滚动条位置$('#nav div').css("background-color", "white"); $(".f" + i).css("background-color", "red"); } } }); $('#nav div').click(function () { $('html,body').animate({scrollTop: $("#" + this.className).offset().top}, 500);// $("#" + this.className)[0].scrollIntoView(true);//h5 scrollIntoView()}); });
登录后复制
全部代码
h5之scrollIntoView控制页面元素滚动 /*scrollIntoView*/#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;}/*scroll*/.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;}scrollIntoView
scrollIntoView(ture)元素上边框与视窗顶部齐平scrollIntoView(false)元素下边框与视窗底部齐平window.onload = function () {/* 如果滚动页面也是DOM没有解决的一个问题。为了解决这个问题,浏览器实现了一下方法,以方便开发人员如何更好的控制页面的滚动。 在各种专有方法中,HTML5选择了scrollIntoView()作为标准方法。scrollIntoView()可以在所有的HTML元素上调用, 通过滚动浏览器窗口或某个容器元素,调用元素就可以出现在视窗中。如果给该方法传入true作为参数,或者不传入任何参数, 那么窗口滚动之后会让调动元素顶部和视窗顶部尽可能齐平。如果传入false作为参数,调用元素会尽可能全部出现在视口中(可能的话,调用元素的底部会与视口的顶部齐平。)不过顶部不一定齐平. */document.querySelector("#roll1").onclick = function () { document.querySelector("#roll_top").scrollIntoView(false); }; document.querySelector("#roll2").onclick = function () { document.querySelector("#roll_top").scrollIntoView(true); }; }$(function () { $(window).scroll(function () {//为页面添加页面滚动监听事件var wst = $(window).scrollTop(); //滚动条距离顶端值for (var i = 1; i < 6; i++) { //加循环if ($("#f" + i).offset().top <= wst + 10) { //判断滚动条位置 $('#nav div').css("background-color", "white"); $(".f" + i).css("background-color", "red"); } } }); $('#nav div').click(function () { $('html,body').animate({scrollTop: $("#" + this.className).offset().top}, 500);// $("#" + this.className)[0].scrollIntoView(true);//h5 scrollIntoView()}); });scroll
页面结构
测试1测试2测试3测试4测试5
登录后复制View Code
以上就是h5之scrollIntoView用法详解的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2910596.html