Last active
August 3, 2021 15:58
-
-
Save bytrangle/7b186d20a75358ed7607a70f3ffae609 to your computer and use it in GitHub Desktop.
|Helper function for getting mouse position in major browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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