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

前端 未结 13 1720
北恋
北恋 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条回答
  •  旧时难觅i
    2020-12-07 12:32

    var distanceTop = element.getBoundingClientRect().top;
    

    For details vist a link:

    https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

提交回复
热议问题