Skip to content

Instantly share code, notes, and snippets.

@bulatie
Last active August 29, 2015 14:26
Show Gist options
  • Save bulatie/d958c4840517684ee4ff to your computer and use it in GitHub Desktop.
Save bulatie/d958c4840517684ee4ff to your computer and use it in GitHub Desktop.
get element position height relate viewport
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