【问题标题】:Implementing infinite scroll实现无限滚动
【发布时间】:2014-03-31 03:25:46
【问题描述】:

我正在尝试使用无限滚动,但是当我向下滚动时什么都没有发生。这是我当前分页的样子:

<ul class="elgg-pagination">
   <li class="elgg-state-disabled"><span>« Previous</span></li>
   <li class="elgg-state-selected"><span>1</span></li>
   <li><a href="http://siteexample.com/casting?offset=10">2</a></li>
   <li><a href="http://siteexample.com/casting?offset=20">3</a></li>
   <li><a href="http://siteexample.com/casting?offset=30">4</a></li>
   <li><a href="http://siteexample.com/casting?offset=40">5</a></li>
   <li><a href="http://siteexample.com/casting?offset=50">6</a></li>
   <li><a href="http://siteexample.com/casting?offset=10">Next »</a></li>
</ul>

这就是我的 javascript 的样子:

$(window).scroll(function(){
var mostOfTheWayDown = ($(document).height() - $(window).height()) * 9 / 10;   
if ($(window).scrollTop() >= mostOfTheWayDown){
$container.infinitescroll({

  navSelector  : $('a',find('.elgg-state-selected')),    // selector for the paged     navigation 
  nextSelector : $('a',find('.elgg-state-selected').next('li').not('.elgg-state-disabled')),  // selector for the NEXT link (to page 2)
  itemSelector : '.item1',     // selector for all items you'll retrieve
  loading: {
  finishedMsg: 'No more pages to load.',
  img: 'http://i.imgur.com/6RMhx.gif'
}
  },
  // trigger Masonry as a callback
  function( newElements ) {
    // hide new items while they are loading
    var $newElems = $( newElements ).css({ opacity: 0 });
    // ensure that images load before adding to masonry layout
    $newElems.imagesLoaded(function(){
      // show elems now they're ready
      $newElems.animate({ opacity: 1 });
      $container.masonry( 'appended', $newElems, true ); 
     });
  }
);
  }

【问题讨论】:

  • 你打开控制台检查错误了吗
  • TypeError: find(...).next 不是函数
  • 那你就知道问题出在哪里了,这样的选择器里面没有find。
  • 我应该如何获取 navSelector 和 nextselector 值?我还是有点困惑
  • $('.elgg-state-selected').find('a');怎么样

标签: javascript jquery infinite-scroll


【解决方案1】:

我会推荐这个插件,不要浪费你的时间重新发明轮子,享受 jQuery 的精彩!

http://www.infinite-scroll.com/

// infinitescroll() is called on the element that surrounds 
// the items you will be loading more of
  $('#content').infinitescroll({

    navSelector  : "div.navigation",            
                   // selector for the paged navigation (it will be hidden)
    nextSelector : "div.navigation a:first",    
                   // selector for the NEXT link (to page 2)
    itemSelector : "#content div.post"          
                   // selector for all items you'll retrieve
  });

你也可以试试这个:

http://airbnb.github.io/infinity/demo-on.html

【讨论】:

    猜你喜欢
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2016-04-02
    相关资源
    最近更新 更多