简体   繁体   中英

Retrieving the position of relative element to window

What is the most accurate way of retrieving top position of relative positioned element to window?

i know about getBoundingClientRect() but this returned wrong numbers.

I have several elements ( relative positioned ) inside containers and need to retrieve elements offsetTop to window top. The containers are both relative or absolute positioned. Is there any method without iterating over parent elements and so on?

document.querySelectorAll('.your-selector')[0]
    .getBoundingClientRect().top + window.pageYOffset - document.documentElement.clientTop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM