Last active
July 17, 2018 19:32
-
-
Save dejurin/399aad0644f77f873a97b46534bc0791 to your computer and use it in GitHub Desktop.
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 getOffset(el) { | |
if (typeof el !== 'undefined') { | |
if (el.getBoundingClientRect()) { | |
var box = el.getBoundingClientRect(), | |
body = document.body, | |
docElem = document.documentElement, | |
scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop, | |
clientTop = docElem.clientTop || body.clientTop || 0, | |
top = box.top + scrollTop - clientTop; | |
return Math.round(top); | |
} else { | |
var top = 0, | |
left = 0 | |
while (el) { | |
top = top + parseInt(el.offsetTop) | |
el = el.offsetParent | |
} | |
return top | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: https://jsfiddle.net/8ofvw7rx/66/