Getting the mouse co-ordinates in JavaScript (all browsers)
We are going to create two functions mouseX(event), mouseY(event). In each function we do the following
- Get the event triggered
- Find the X or Y co-ordinate of that event
function mouseX(evt) {
evt = (evt) ? evt : ((event) ? event : null);
if (evt.pageX) return evt.pageX;
if (evt.clientX)
return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
else return null;
}function mouseY(evt) {
evt = (evt) ? evt : ((event) ? event : null);
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;
About this entry
You’re currently reading “Getting the mouse co-ordinates in JavaScript (all browsers),” an entry on Dazzatechfind's Blog
- Published:
- May 22, 2010 / 11:52 am
- Category:
- javascript
- Tags:
- all browsers, co-ordinates, event, firefox, IE, javascript, mouse, netscape, opera
No comments yet
Jump to comment form | comment rss [?] | trackback uri [?]