精确获取offsetTop

目的:精确获取offsetTop,解决H5滚动监听的时候,获取的offsetTop有问题,解决安卓和IOS上的表现不一致

先简单写个demo,学习一下基础的知识








    

    

    

    Document

    





    

    
div1
div3

下面是大招


function getOffsetTop () {

    let actualTop = element.offsetTop;

    let current = element.offsetParent;

    while (current !== null) {

        actualTop += current.offsetTop;

        current = current.offsetParent;

    }

    return actualTop;

}

你可能感兴趣的:(精确获取offsetTop)