Get Mouse Position in JavaScript for all browsers

Author posted by Jitendra on Posted on under category Categories Tech Tips and tagged as Tags with Leave a comment on Get Mouse Position in JavaScript for all browsers

 

Get Mouse Movement in Javascript
Get Mouse Movement in Javascript
function mouseXPos(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
return evt.clientX + (document.documentElement.scrollLeft ?
document.documentElement.scrollLeft :
document.body.scrollLeft);
else return null;
}

function mouseYPos(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
return evt.clientY + (document.documentElement.scrollTop ?
document.documentElement.scrollTop :
document.body.scrollTop);
else return null;
}

To use above function, just pass the mouse event.

Live Demo

Related posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.