Skip to content

Instantly share code, notes, and snippets.

@bytrangle
Last active August 3, 2021 15:58
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 bytrangle/7b186d20a75358ed7607a70f3ffae609 to your computer and use it in GitHub Desktop.
Save bytrangle/7b186d20a75358ed7607a70f3ffae609 to your computer and use it in GitHub Desktop.
|Helper function for getting mouse position in major browsers
const getMousePosition = (e) => {
let posX = 0, posY = 0;
if (!e) e = window.event;
if (e.pageX ¦¦ e.pageY) {
posX = e.pageX;
posY = e.pageY;
}
// This only applies in IE
else if (e.clientX ¦¦ e.clientY) {
const eventDoc = (e.target && e.target.ownerDocument) ¦¦ document;
const doc = eventDoc.documentElement;
const body = eventDoc.body;
posX = e.clientX + (doc && doc.scrollLeft ¦¦ body && body.scrollLeft ¦¦ 0) - (doc && doc.clientLeft ¦¦ body && body.clientLeft ¦¦ 0)
posY = e.clientY + (doc && doc.scrollTop ¦¦ body && body.scrollTop ¦¦ 0) - (doc && doc.clientTop ¦¦ body && body.clientTop ¦¦ 0)
}
return { x: posX, y: posY };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment