Skip to content

Instantly share code, notes, and snippets.

@VincentHelwig
Created November 5, 2014 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VincentHelwig/aba7b628b1205599dba7 to your computer and use it in GitHub Desktop.
Save VincentHelwig/aba7b628b1205599dba7 to your computer and use it in GitHub Desktop.
Optimised retrieve offset in vanilla js
// 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