How to get the distance from the top for an element?

前端 未结 13 1695
北恋
北恋 2020-12-07 12:13

I want to know how to use JavaScript to get the distance of an element from the top of the page not the parent element.

13条回答
  •  执笔经年
    2020-12-07 12:37

    scroll to element's top position;

    var rect = element.getBoundingClientRect();
    var offsetTop = window.pageYOffset + rect.top - rect.height;
    

提交回复
热议问题