Created
July 16, 2012 03:40
-
-
Save toruta39/3120325 to your computer and use it in GitHub Desktop.
Calculate the actual offset value of any element, recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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