Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get distance of element from top of the window which changes when user scrolls

I need to find distance between top of the window to the element. I can calculate that distance using offsetTop, But in offsetTop it only gives element's distance from top of the window, What I want is to decrease the distance when user scrolls up. And increase the distance when user scrolls down. So how to achieve this?

like image 900
Mauj Mishra Avatar asked Jan 24 '26 16:01

Mauj Mishra


1 Answers

Use .getBoundingClientRect().

See more info here: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

like image 186
小聪聪到此一游 Avatar answered Jan 27 '26 05:01

小聪聪到此一游



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!