jQuery:如何检测用户何时再次滚动到顶部?

时间:2015-04-02 19:19:56

标签: javascript jquery html css

在我的网站顶部,我有一个类似博客文章的栏。 **当用户再次滚动到顶部时,应该使用SlideUp(使用jQuery) - 例如他读完这篇文章之后。

如何检测这种情况,然后在我网站的头部显示栏?**

2 个答案:

答案 0 :(得分:6)

您可以监控scroll元素的window事件,并检查其scrollTop()

    $(window).scroll(function () {
        if ($(this).scrollTop()  <= 0 ){
            // your code
        }
    }

答案 1 :(得分:0)

使用jquery中的.scrollTop()检查滚动位置很容易。

所以我的想法是在滚动上绑定一个事件以定期检查位置。

hasReadArticle = false
$window = $(window)
articleBottomPosition = ... # get the position of the bottom of the article 
timeout = nil

$window.on "scroll", (e) ->
   clearTimeout timeout if timeout

   timeout = setTimeout ->
     top = $window.scrollTop()

     # you might want to improve this to detect when the bottom of your window arrives at the bottom of the article.
     hasReadArticle = top > articleBottomPosition unless hasReadArticle

     if top <= 0 && hasReadArticle
       # $('your header').slideDown()
   , 100

更简单的方法是使用jquery-waypoints。您可以设置一个处理程序来检测何时到达文章的底部,然后设置变量以记住这一点。然后设置另一个处理程序以检测何时到达页面顶部(方向==&#34;向上&#34;)。

相关问题