Skip to content

Instantly share code, notes, and snippets.

@THEtheChad
Last active June 30, 2022 10:40
Show Gist options
  • Select an option

  • Save THEtheChad/4d7fb679f738f49dddcf58b87e582f8f to your computer and use it in GitHub Desktop.

Select an option

Save THEtheChad/4d7fb679f738f49dddcf58b87e582f8f to your computer and use it in GitHub Desktop.

Revisions

  1. THEtheChad revised this gist Jun 30, 2022. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions getWindowPosition.ts
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    const offsetBase = document.createElement('div');
    offsetBase.style.cssText = 'position:absolute;left:0;top:0'
    offsetBase.style.cssText = 'position:absolute;left:0;top:0';
    document.body.appendChild(offsetBase);

    export default function getWindowPosition(node: Element){
    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
    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,
    @@ -18,6 +18,6 @@ export default function getWindowPosition(node: Element){
    width,
    height,
    right: left + width,
    bottom: top + height
    }
    }
    bottom: top + height,
    };
    }
  2. THEtheChad created this gist Jun 30, 2022.
    23 changes: 23 additions & 0 deletions getWindowPosition.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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
    }
    }