Skip to content

Instantly share code, notes, and snippets.

@aderaaij
Created November 29, 2015 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aderaaij/89547e34617b95ac29d1 to your computer and use it in GitHub Desktop.
Save aderaaij/89547e34617b95ac29d1 to your computer and use it in GitHub Desktop.
Basic offset function, replacing jQuery's offset.
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);
@malipetek
Copy link

Sometimes document.body will not give scrollTop properly. If you replace document.body with document.documentElement it works more reliably. jQuery does the same.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment