window.onscroll = function(){ var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; console.log(scrollTop);}window.onload = function(){ window.pageYOffset = document.documentElement.scrollTop = document.body.scrollTop =100;}window.onload函數(shù)的作用是當(dāng)加載該頁(yè)面時(shí)自動(dòng)定位到100的位置。如果沒(méi)有window.onload函數(shù),onscroll可以正確地隨著頁(yè)面滾動(dòng)打印出scrollTop的值。但一旦window.onload函數(shù)里把100賦值給window.pageYOffset后無(wú)論頁(yè)面怎么滾動(dòng)scrollTop的值都一直是100不會(huì)變,請(qǐng)問(wèn)定位100位置后怎么做才能隨著滾動(dòng)正常刷新scrollTop的值?
window.onscroll無(wú)法刷新scrollTop的值?
慕森卡
2018-08-03 14:14:54