Skip to content

Instantly share code, notes, and snippets.

Avatar

Ender endersaka

View GitHub Profile
@endersaka
endersaka / get_html_element_coords.js
Created Apr 25, 2020
Get HTML element position relative to document space
View get_html_element_coords.js
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
}
}