Created
November 5, 2014 11:46
-
-
Save VincentHelwig/aba7b628b1205599dba7 to your computer and use it in GitHub Desktop.
Optimised retrieve offset in vanilla js
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
// FROM http://jsperf.com/test-offset-jquery-vs-vanilla | |
function offset(elt) { | |
var rect = elt.getBoundingClientRect(), bodyElt = document.body; | |
return { | |
top: rect.top + bodyElt .scrollTop, | |
left: rect.left + bodyElt .scrollLeft | |
} | |
} | |
var offsetElt = offset(document.getElementById('element')); | |
offsetElt.top; | |
offsetElt.left; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment