Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

offsetTop / getBoundingClientRect().top of the element with position: sticky

When element with position: sticky reach his offset (top: 0, for example) and stops on the screen, i can't get his offsetTop properly, it shows more than necessary. At the same time, I can't get getBoundingClientRect().top, because it shows 0. Please take a look at this example:

https://jsfiddle.net/Lxud76ma/

When the element is sticky, it reserves its place in DOM, but the calculations are takes on its actual location.

So, how can I get coordinates of an element when it is sticky? Any ideas?

like image 933
Michael Rafailyk Avatar asked Nov 19 '25 11:11

Michael Rafailyk


1 Answers

I think i find an answer, but correct me if somebody know more properly way.

Before calculating need to change position of element from sticky to static. Get his coordinates and bring him back to position: sticky.

https://jsfiddle.net/Lk74do8u/

like image 147
Michael Rafailyk Avatar answered Nov 22 '25 04:11

Michael Rafailyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!