JQuery Compute window yScroll position

I am trying to calculate their position of a dynamic scrollbar when scrolling a window. I can get the starting scroll position at boot using:

var scrollY = $(window).scrollTop(); 

But this does not update as the window scrolls, I need to reload every time to get the updated variable. What do I need to do to keep this value while scrolling? I tried something like:

 $(document).scroll(function(e){ $('#status').html(e.scrollY); }); 

And then create a div with the id "status" to output the result, but I get nothing. Can anyone help with this?

Thank you, Chris

+4
source share
1 answer

Why do you think scrollTop doesn't update when you scroll the window? When I try to do this, it works fine:

CSS

 #status { height: 1000px; padding: 100px; } 

Script:

 $(document).scroll(function(e){ $('#status').html($(window).scrollTop()); }); 

HTML:

 <div id="status"></div> 

http://jsfiddle.net/Z4sZp/

+12
source

Source: https://habr.com/ru/post/1336674/


All Articles