Skip to content

Instantly share code, notes, and snippets.

@jlong
Last active July 18, 2022 11:03

Revisions

  1. jlong revised this gist Jul 21, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions position.js
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,9 @@ function getViewportOffset(element) {
    top -= node.scrollTop;

    if (/relative|absolute|fixed/.test(position)) {
    left += parseInt(styles.getPropertyValue('border-left-width'), 10);
    top += parseInt(styles.getPropertyValue('border-top-width'), 10);

    left += node.offsetLeft;
    top += node.offsetTop;
    }
  2. jlong revised this gist Jul 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion position.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function getOffset(element) {
    function getViewportOffset(element) {
    var node = element
    , left = node.offsetLeft
    , top = node.offsetTop
  3. jlong created this gist Jul 18, 2014.
    31 changes: 31 additions & 0 deletions position.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    function getOffset(element) {
    var node = element
    , left = node.offsetLeft
    , top = node.offsetTop
    ;

    node = node.parentNode;

    do {
    var styles = getComputedStyle(node);

    if (styles) {
    var position = styles.getPropertyValue('position');

    left -= node.scrollLeft;
    top -= node.scrollTop;

    if (/relative|absolute|fixed/.test(position)) {
    left += node.offsetLeft;
    top += node.offsetTop;
    }

    node = position === 'fixed' ? null : node.parentNode;
    } else {
    node = node.parentNode;
    }

    } while (node);

    return { left: left, top: top };
    }