Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Last active December 31, 2015 03:09
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 bloodyowl/7925928 to your computer and use it in GitHub Desktop.
Save bloodyowl/7925928 to your computer and use it in GitHub Desktop.
getRelativeCoors, for mouseevents
function getRelativeCoords(target, evt){
var clientRect = target.getBoundingClientRect()
return {
x : evt.clientX - clientRect.left
, y : evt.clientY - clientRect.top
}
}
@bloodyowl
Copy link
Author

var target = someElement
element.addEventListener(function(evt){
  console.log(getRelativeCoords(target, vet))
})

@lionelB
Copy link

lionelB 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