Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Last active May 10, 2022 02:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshuacerbito/8eb4d6240de141956e15b885be9b195e to your computer and use it in GitHub Desktop.
Save joshuacerbito/8eb4d6240de141956e15b885be9b195e to your computer and use it in GitHub Desktop.
Get an Element's Offset relative to the document
const getElementOffset = el => {
const rect = el.getBoundingClientRect(),
scrollLeft = window.scrollX || document.documentElement.scrollLeft,
scrollTop = window.scrollY || document.documentElement.scrollTop;
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