Javascript onscroll and mouse position
I have a script that runs a addEventListener for onmousemove and onscroll on the document body to get the cursor position. A onmousemove event works fine (client + scroll), however when a onscroll event occurs clientX/Y seems to inherit scrollTop/Left valu开发者_如何学JAVAes instead (only scroll). Is there a way around this?
clickDocument = (document.documentElement != undefined && document.documentElement.clientHeight != 0) ? document.documentElement : document.body;
var posx = 0;
var posy = 0;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX;
posy = e.clientY;
}
var scrollx = window.pageXOffset == undefined ? clickDocument.scrollLeft : window.pageXOffset;
var scrolly = window.pageYOffset == undefined ? clickDocument.scrollTop : window.pageYOffset;
Depending on the IE browser version and the doctype, sometimes you need clickDocument.body.scrollTop
or even clickDocument.documentElement.scrollTop
.
(Source: http://javascript.about.com/library/blmousepos.htm)
精彩评论