Created
May 15, 2014 22:25
-
-
Save kristw/98c63acef33819396fb4 to your computer and use it in GitHub Desktop.
Get element position on a screen
This file contains hidden or 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 getPosition(element) { | |
var xPosition = 0; | |
var yPosition = 0; | |
var topContainer; | |
while(element) { | |
xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft); | |
yPosition += (element.offsetTop - element.scrollTop + element.clientTop); | |
if(!element.offsetParent) topContainer = element; | |
element = element.offsetParent; | |
} | |
var topContainerTagName = topContainer.tagName.toLowerCase(); | |
if(topContainerTagName === 'body' || topContainerTagName === 'html'){ | |
yPosition += $window.scrollTop(); | |
xPosition += $window.scrollLeft(); | |
} | |
return { x: xPosition, y: yPosition }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment