每次用JavaScript / jQuery滚动时,你能获得用户的滚动位置吗? [英] Can you get the user’s scroll position every time they scroll with JavaScript/jQuery?

查看:153
本文介绍了每次用JavaScript / jQuery滚动时,你能获得用户的滚动位置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

滚动时是否可以获取用户滚动条的当前位置?

Is it possible to grab the current position of the users scroll bar when they scroll?

说,如果它们滚动到最顶部,则为0,如果他们向下滚动它会改变。

Say, if they were scrolled to the very top it would be 0, if they scrolled down it'd change.

我可以在每次访问它时分配一个变量来包含当前滚动位置,或者在JavaScript中是否有一个函数用于此?

Can I assign a variable to contain the current scroll position whenever I access it, or is there a function in JavaScript for this already?

推荐答案

从你的评论中,我得到你正在寻找一个滚动事件监听器,如
http://api.jquery.com/scroll/ ,然后您可以使用 http://api.jquery.com/scrollTop/ 找出卷轴的位置。

From your comment, I get you are looking for a scroll event listener such as http://api.jquery.com/scroll/, and then you can use http://api.jquery.com/scrollTop/ to find out the position of the scroll.

例如:

<script>
$(window).scroll(function () { 
    //You've scrolled this much:
       $('p').text("You've scrolled " + $(window).scrollTop() + " pixels");
});
</script>

http:// jsfiddle.net/pFaTF/

这篇关于每次用JavaScript / jQuery滚动时,你能获得用户的滚动位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆