Skip to content

Instantly share code, notes, and snippets.

@toruta39
Created July 16, 2012 03:40
Show Gist options
  • Save toruta39/3120325 to your computer and use it in GitHub Desktop.
Save toruta39/3120325 to your computer and use it in GitHub Desktop.
Calculate the actual offset value of any element, recursively
function getElementOffset(element, property) {
//Calculate the actual property name
property = "offset"+property[0].toUpperCase()+property.slice(1).toLowerCase();
if (property == "offsetLeft" || property == "offsetTop") {
var actualOffset = element[property];
var current = element.offsetParent;
//Look up the node tree to add up all the offset value
while (current != null) {
actualOffset += current[property];
current = current.offsetParent;
}
return actualOffset;
} else if (property == "offsetHeight" || property == "offsetWidth") {
return element[property];
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment