Satu Sultana Satu Sultana - 5 months ago 8
jQuery Question

Get a div offset for one page site

Here I have used bootstrap fixed navigation.

I have a section like

<section id="count" class="count-section">
// here some code
</section>


I am trying to apply an animation, if user scroll and see on this portion. So here I am trying to make it by measure offset. I have tried below code

$(window).scroll(function() {
var pos = $('#count').offset().top;
console.log(pos);
}


After reload the page I got value
1539.5333404541016
after scroll page I always get value
1539.5333251953125
.

How can I measure this position from my top fixed navigation ?

Answer

Something like this might work:

$(window).scroll(function() {
    var pos = $('#count').offset().top - $(document).scrollTop();
    console.log(pos);
});

Take a look at $(window).height() and $(document).scrollTop().