Last active
December 31, 2015 03:09
-
-
Save bloodyowl/7925928 to your computer and use it in GitHub Desktop.
getRelativeCoors, for mouseevents
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
function getRelativeCoords(target, evt){ | |
var clientRect = target.getBoundingClientRect() | |
return { | |
x : evt.clientX - clientRect.left | |
, y : evt.clientY - clientRect.top | |
} | |
} |
Author
bloodyowl
commented
Dec 12, 2013
works better ;)
function getRelativeCoords(target, evt){
var clientRect = target.getBoundingClientRect()
, top = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0
, left = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0
return {
x : evt.pageX - (clientRect.left + left)
, y : evt.pageY - (clientRect.top + top)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment