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

Get the position of an element relative to the document

JavaScript version

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

Example

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