要獲取當前頁面的滾動條縱座標位置,用:javascript
document.
documentElement.
scrollTop;
而不是:html
document.
body.
scrollTop;
documentElement 對應的是 html 標籤,而 body 對應的是 body 標籤。
在標準w3c下,document.body.scrollTop恆爲0,須要用document.documentElement.scrollTop來代替java
若是你想定位鼠標相對於頁面的絕對位置時,你會發現google裏面1000篇文章裏面有999.99篇會讓你使用 event.clientX+document.body.scrollLeft,event.clientY+document.body.scrollTop, 若是你發現你的鼠標定位偏離了你的想象,請不要奇怪,這是再正常不過的事情。 ie5.5以後已經不支持document.body.scrollX對象了。 因此在編程的時候,請加上這樣的判斷 if (document.body && document.body.scrollTop && document.body.scrollLeft) { top=document.body.scrollTop; left=document.body.scrollleft; } if (document.documentElement && document.documentElement.scrollTop && document.documentElement.scrollLeft) { top=document.documentElement.scrollTop; left=document.documentElement.scrollLeft; }