Skip to content

Instantly share code, notes, and snippets.

@simonwoo
Created August 6, 2014 08:25
Show Gist options
  • Save simonwoo/414b821ed9f2f35c0a36 to your computer and use it in GitHub Desktop.
Save simonwoo/414b821ed9f2f35c0a36 to your computer and use it in GitHub Desktop.
Get an Element's Position Using JavaScript
function getPosition(element) {
var xPosition = 0;
var yPosition = 0;
while(element) {
xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
element = element.offsetParent;
}
return { x: xPosition, y: yPosition };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment