美文网首页
dom操作,计算元素距离document顶部高度

dom操作,计算元素距离document顶部高度

作者: 瓢鳍小虾虎 | 来源:发表于2020-06-24 11:01 被阅读0次

function getOffset (node) {

    let result = {

        top: 0,

        left: 0 

   }

    if (!node.getClientRects().length) {

        return;

    }

    if (window.getComputedStyle(ele)['display'] === 'none') {    

        return result

    }

    result = node.getBoundingClientRect()

    var docElement = node.ownerDocument.documentElement

    return {

        top: result.top + window.pageYOffset - docElement.clientTop,

        left: result.left + window.pageXOffset - docElement.clientLeft

    }

}

相关文章

网友评论

      本文标题:dom操作,计算元素距离document顶部高度

      本文链接:https://www.haomeiwen.com/subject/qowcfktx.html