Last active
August 29, 2015 14:26
-
-
Save bulatie/d958c4840517684ee4ff to your computer and use it in GitHub Desktop.
get element position height relate viewport
This file contains hidden or 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 getElementViewTop(element) { | |
if (document.documentElement.getBoundingClientRect !== 'undefined') { | |
return element.getBoundingClientRect().top | |
} else { | |
var actualTop = element.offsetTop | |
var current = element.offsetParent | |
while (current !== null) { | |
actualTop += current.offsetTop | |
current = current.offsetParent | |
} | |
var elementScrollTop = document.body.scrollTop | |
return actualTop - elementScrollTop | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment