getting element parent offset in js

Table of Contents

const offsetChildParent = (child,parent) => {
    // use bounding box to get a consistent
    const childBoundingRect = child.getBoundingClientRect();
    const parentBoundingRect = parent.getBoundingClientRect();
    return {top: childBoundingRect.top - parentBoundingRect.top ,
            bottom: childBoundingRect.bottom - parentBoundingRect.bottom ,
            left: childBoundingRect.left - parentBoundingRect.left,
            right: parentBoundingRect.right - childBoundingRect.right
           }
}

Backlinks

Author: Linfeng He

Created: 2024-04-03 Wed 20:16