Skip to content

Instantly share code, notes, and snippets.

@THEtheChad
Last active June 30, 2022 10:40
Show Gist options
  • Save THEtheChad/4d7fb679f738f49dddcf58b87e582f8f to your computer and use it in GitHub Desktop.
Save THEtheChad/4d7fb679f738f49dddcf58b87e582f8f to your computer and use it in GitHub Desktop.
const offsetBase = document.createElement('div');
offsetBase.style.cssText = 'position:absolute;left:0;top:0';
document.body.appendChild(offsetBase);
export default function getWindowPosition(node: Element) {
const boundingRect = node.getBoundingClientRect();
const baseRect = offsetBase.getBoundingClientRect();
const left = boundingRect.left - baseRect.left;
const top = boundingRect.top - baseRect.top;
const width = boundingRect.right - boundingRect.left;
const height = boundingRect.bottom - boundingRect.top;
return {
...boundingRect,
left,
top,
width,
height,
right: left + width,
bottom: top + height,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment