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

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

代码示例1
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 };
}