Created
April 25, 2020 08:14
-
-
Save endersaka/bacd6e7666245f3d3a84fe294f16db55 to your computer and use it in GitHub Desktop.
Get HTML element position relative to document space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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