Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created June 27, 2015 11:01
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 andreasvirkus/a13bb9513414999763af to your computer and use it in GitHub Desktop.
Save andreasvirkus/a13bb9513414999763af to your computer and use it in GitHub Desktop.
//For the times that I need to know how far an element is from the top of the DOM,
//not the top of it’s parent, I use this helper function.
//Loops through all parent nodes of an element to get it's distance from the top of the document
function getDistanceFromTop(element) {
var yPos = 0;
while(element) {
yPos += (element.offsetTop);
element = element.offsetParent;
}
return yPos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment