开发者

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


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜