Skip to content

Instantly share code, notes, and snippets.

@electerious
Created February 22, 2017 16:16
Show Gist options
  • Save electerious/bbe218df8c709f8c01d25f8b5d3984c9 to your computer and use it in GitHub Desktop.
Save electerious/bbe218df8c709f8c01d25f8b5d3984c9 to your computer and use it in GitHub Desktop.
Get the position of the mouse
const mousePosition = (() => {
let pos = {}
const position = (e) => ({
x: e.pageX,
y: e.pageY
})
document.addEventListener('mousemove', (e) => pos = position(e), false)
document.addEventListener('mouseenter', (e) => pos = position(e), false)
return () => pos
})()
@electerious
Copy link
Author

Example:

mousePosition() // { x: 0, y: 0 }

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