Skip to content

Instantly share code, notes, and snippets.

@kristw
Created May 15, 2014 22:25
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 kristw/98c63acef33819396fb4 to your computer and use it in GitHub Desktop.
Save kristw/98c63acef33819396fb4 to your computer and use it in GitHub Desktop.
Get element position on a screen
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