. 1 scrollWidth full width web body 2 the scrollHeight GAO text pages . 3 scrollTop web roll to be high . 4 the scrollLeft web is rolled to the left
1 Compatible wording 2 window.onscroll = function (EV) { . 3 var scrollTop document.documentElement.scrollTop || = || window.pageYOffset document.body.scrollTop; . 4 the console.log (scrollTop); . 5 }
Use encapsulation format compatibility json
1 function scroll() { 2 if(window.pageYOffset !== null){ // 最新的浏览器 3 return { 4 "top": window.pageYOffset, 5 "left": window.pageXOffset 6 } 7 }else if(document.compatMode === 'CSS1Compat'){ // W3C 8 return { 9 "top": document.documentElement.scrollTop, 10 "left": document.documentElement.scrollLeft 11 } 12 } 13 return { 14 "top": document.body.scrollTop, 15 "left": document.body.scrollLeft 16 } 17 } 18 19 window.addEventListener('scroll', function (ev) { 20 // console.log(scroll().top); 21 console.log(myTool.scroll().top); 22 });