Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
getElementOffset in JavaScript
// calcule the offset in pixels to top of the site,
// from a DOM element (el)
export default function getElementOffset(el) {
let top = 0
let left = 0
// grab the offset of the element relative to it's parent,
// then repeat with the parent relative to it's parent,
// ... until we reach an element without parents.
do {
top += el.offsetTop
left += el.offsetLeft
el = el.offsetParent
} while (el)
return { top, left }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.