【问题标题】:Different behaviours for $(window).scroll function on page scroll页面滚动时 $(window).scroll 函数的不同行为
【发布时间】:2012-02-08 09:21:06
【问题描述】:

我有一个使用 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 中,我运行查询以使用偏移值获取更多提要,然后将其部分显示。部分作为响应返回。在部分内部执行的代码是这样的:

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 被添加了两次。你能帮我弄清楚这种行为吗?

【问题讨论】:

  • 请不要在问题中添加{SOLVED]。将答案标记为已接受就足以表明问题已解决。
  • 哦!对不起。我没有意识到这一点。我认为添加 [SOLVED] 会有所帮助。我会编辑问题。

标签: jquery symfony1 scroll


【解决方案1】:

试着这样做

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-29
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多