Skip to content

Instantly share code, notes, and snippets.

@edgarberm
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save edgarberm/3f9dcab8d46bd13e8ef3 to your computer and use it in GitHub Desktop.

Select an option

Save edgarberm/3f9dcab8d46bd13e8ef3 to your computer and use it in GitHub Desktop.
Convert the returned position from document coordinates to viewport coordinates.
function getElementPosition ( element ) {
var x = 0,
y = 0;
for ( var e = element; e != null; e = e.offsetParent ) {
x += e.offsetLeft;
y += e.offsetTop;
}
for ( var e = element.parentNode; e != null && e.nodeType == 1; e = e.parentNode ) {
x -= e.scrollLeft;
y -= e.scrollTop;
}
return { x : x, y : y };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment