Skip to content

Instantly share code, notes, and snippets.

@remy
Forked from padolsey/gist:360015
Created April 8, 2010 12:07
Show Gist options
  • Save remy/360018 to your computer and use it in GitHub Desktop.
Save remy/360018 to your computer and use it in GitHub Desktop.
Get element from mouse position
var x,y;
document.onmousemove = function(e){
e = e || window.event;
x = e.clientX;
y = e.clientY;
};
function elementAtMousePosition() {
return document.elementFromPoint(x,y);
}
@litenjacob
Copy link

elementFromPoint is not very well known, but really awesome!
However, do remember that Firefox/IE uses clientX/Y while Safari/Opera uses pageX/Y coordinates!
/Jacob

@remy
Copy link
Author

remy commented Apr 8, 2010

I'm using clientX on another hack that's currently working in Safari, Opera and Firefox...

@litenjacob
Copy link

Huh?
It works in all these browsers? So maybe they've changed it?
Or does it work only as long as you have not scrolled your page? ;)

It seems as if Chrome has adopted clientX, but I do remember that I needed pageX to satisfy Safari the last time I checked.
Just curious - is it for marbles2 or something?

@zeeshanjan82
Copy link

I have been struggling to figure out how to make elementFromPoint work. I am implementing a drag & drop using hammer.js and I would like to know when the panend event has fired which is the DOM element beneath the target

http://plnkr.co/edit/jSsELjc4XOKEk1ffq5Qh?p=preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment