Skip to content

Instantly share code, notes, and snippets.

@mihdan
Created January 24, 2015 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mihdan/d2886c8aa112c0a0bc8e to your computer and use it in GitHub Desktop.
Save mihdan/d2886c8aa112c0a0bc8e to your computer and use it in GitHub Desktop.
Cross Browser Calculation of X & Y Position
function getPosition(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
return {
x: posx,
y: posy
}
}
// usage
document.addEventListener( "click", function(e) {
var x = getPosition(e).x;
var y = getPosition(e).y;
console.log("x pos: "+ x +" // y pos:"+ y);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment