📌  相关文章
📜  js 获取元素与顶部的距离 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:58.448000             🧑  作者: Mango

代码示例2
function getPosition(element) {
    var xPosition = 0;
    var yPosition = 0;

    while(element) {
        xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
        yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
        element = element.offsetParent;
    }

    return { x: xPosition, y: yPosition };
}