Created
November 29, 2015 18:17
-
-
Save aderaaij/89547e34617b95ac29d1 to your computer and use it in GitHub Desktop.
Basic offset function, replacing jQuery's offset.
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 offset(elt) { | |
var rect = elt.getBoundingClientRect(), bodyElt = document.body; | |
return { | |
top: rect.top + bodyElt .scrollTop, | |
left: rect.left + bodyElt .scrollLeft | |
} | |
} | |
//use: | |
var element = getElementById('element'); | |
var offsetElement = offset(element); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes
document.body
will not give scrollTop properly. If you replacedocument.body
withdocument.documentElement
it works more reliably. jQuery does the same.