Skip to content

Instantly share code, notes, and snippets.

@17
Last active August 29, 2015 14:20
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 17/afaa1e1ca27c043b6d41 to your computer and use it in GitHub Desktop.
Save 17/afaa1e1ca27c043b6d41 to your computer and use it in GitHub Desktop.
JQuery offsetX fix
var mousePosition = function(e) {
var offsetX = e.offsetX
, offsetY = e.offsetY
, targetOffset
// Calculate offsetX/Y if missing
if (offsetX == null) {
targetOffset = $(e.target).offset()
offsetX = e.pageX - targetOffset.left
offsetY = e.pageY - targetOffset.top
}
// offsetX = offsetX ^ 0
// offsetY = offsetY ^ 0
return {
x: offsetX
, y: offsetY
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment