Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Calculate window yScroll position

I'm trying to calculate they y position of the scrollbar dynamically as the window scrolls. I can get the initial scroll position on load using:

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

But this doesn't update as the window scrolls around, I need to reload each time to get the updated variable. What do I need to do to keep this value updating as I scroll? I've tried something like:

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

And then created a div with and ID of 'status' to output the result, but I'm getting nothing. Can anyone assist with this?

Thanks, Chris

like image 608
kirisu_kun Avatar asked Dec 16 '22 18:12

kirisu_kun


1 Answers

Why do you think that the scrollTop doesn't update as the window scrolls? When I try it, it works just 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/

like image 113
Guffa Avatar answered Jan 09 '23 20:01

Guffa