记录位置
window.addEventListener("scroll", function () { var scrrtop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; if (scrrtop > 0) { localStorage.setItem('scrollTop', scrrtop); } });
跳转位置
let offsety = localStorage.getItem('scrollTop') || 0; if (offsety) { window.scrollTo(0, offsety); }
以下为网络收集方法,仅供参考,不一定有效
console.log($(window).scrollTop()); console.log(document.documentElement.scrollTop); window.addEventListener("scroll", function(){ console.log(document.documentElement.scrollTop); console.log(document.body.scrollTop); var scrrtop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; console.log(scrrtop); }); $(window).scroll(function() { //为了保证兼容性,这里取两个值,哪个有值取哪一个 //scrollTop就是触发滚轮事件时滚轮的高度 var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; console.log('滚动距离' + scrollTop); })
setInterval(function () { var docElement = document.documentElement.scrollTop; var pageYOff = window.pageYOffset; var scllTop = document.body.scrollTop; console.log(docElement); console.log(pageYOff); console.log(scllTop); },1000);