Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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