页面滚动时$(window).scroll函数的不同行为

时间:2012-02-08 09:21:07

标签: jquery symfony1 scroll

我有一个使用Symfony 1.4和Mysql DB构建的Web应用程序。我正在使用此代码来检测用户是否已滚动到页面底部,发送请求以获得更多结果,然后附加结果:

jQuery的:

    <script type="text/javascript">
    var offset = 10;
    var noMore = true;
    $(document).ready(function(){
    //load more feeds on page scroll end
    $(window).scroll(function(){
      if($(window).scrollTop() == $(document).height() - $(window).height()){
        if($('.noFeeds').html()!='No feeds to show') // div shown when initially there are no feeds to show. In that case, we don't call the more feeds function
        {
          if($('#noMore').length) //check if the div that says there are no more feeds, exists or not
          {
           noMore = false; //if it does, set the flag to false so that the function to get more feeds is not called
          }
          if(noMore)
          {
            $('#loading').show();
            offset = offset+10;
            $.post("/user/getMoreFeeds?refferrer=home&offset="+offset+"&id="+<?php echo        $userId;?>, {
             }, function(response){
               $('#moreFeeds').append(response);
               $('#loading').hide();
            });
          }
        }
    }
  });
</script>

在函数getMorefeeds中,我运行查询以使用偏移值获取更多提要,然后将其显示在部分中。部分作为响应返回。在partial中执行的代码是这样的:

PHP:

if(count($feeds)>0)
{
  // Show feeds
}
else
{
  <div id="noMore" class="activity_post" align="center" style="color:#A8A8A8;">No more   feeds to show</div>
}

当我慢慢向下滚动页面时,我会正确地获取所有内容,最后会显示消息“没有更多要显示的内容”。 ,即显示id为“noMore”的div。

但是,如果页面快速滚动,我会看到id为“noMore”的div被添加两次。你能帮我弄清楚这种行为吗?

1 个答案:

答案 0 :(得分:0)

尝试这样做

$(window).scroll(function(event){
  event.preventDefault(); #do sth });