Skip to content

Instantly share code, notes, and snippets.

@endersaka
Created April 25, 2020 08:14
Show Gist options
  • Save endersaka/bacd6e7666245f3d3a84fe294f16db55 to your computer and use it in GitHub Desktop.
Save endersaka/bacd6e7666245f3d3a84fe294f16db55 to your computer and use it in GitHub Desktop.
Get HTML element position relative to document space
function getElementCoordinates(el) {
const doc = document.documentElement;
const scrollLeft = window.pageXOffset || doc.scrollLeft;
const scrollTop = window.pageYOffset || doc.scrollTop;
const rect = el.getBoundingClientRect();
return {
top: rect.top + scrollTop,
left: rect.left + scrollLeft
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment