As shown in figure below...
Black box is the screen size, the viewport.
The red dotted box is the size of the entire page, which has been scrolled out of the viewport. The green dotted boxes are li
elements. Now I want to calculate the distance between a specific li
and the top edge of the viewport, represented by the red arrow.
I'm okay with a jQuery solution.
var distanceToTop = document.querySelector("#your-li").getBoundingClientRect().top
Quite easy with jQuery too :
Demo
$('#element').offset().top-$(window).scrollTop();
Best of both worlds :
$('#element')[0].getBoundingClientRect().top;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With