[Home] > Snippets  > Browser  > DOM  >  Get the position of an element relative to the document

Get the position of an element relative to the document

JavaScript

const getPosition = (ele) => ((r = ele.getBoundingClientRect()), { left: r.left + window.scrollX, top: r.top + window.scrollY })

// In use
getPosition(document.body) // { left: 0, top: 0 }