Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get position of element relative to document

function offset(el) {
    const rect = el.getBoundingClientRect(),
    scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
    scrollTop = window.pageYOffset || document.documentElement.scrollTop;
    return { top: rect.top + scrollTop, left: rect.left + scrollLeft }
}

// example use
const div = document.querySelector('div');
const divOffset = offset(div);
console.log(divOffset.left, divOffset.top);
Source by plainjs.com #
 
PREVIOUS NEXT
Tagged: #position #element #relative #document
ADD COMMENT
Topic
Name
8+3 =