imagesLoaded方法不使用JQuery砌体和无限滚动

时间:2012-03-19 07:36:54

标签: image jquery-masonry infinite-scroll loaded

我一直在使用JQuery砌体,现在我正在添加无限滚动。在几乎每个砖石“砖”中都有图像,在我使用无限滚动之前,图像加载得很好,一切都很棒。我为无限滚动添加了javascript的下一部分,并在里面添加了imagesLoaded方法,但是当添加新砖时,它们全部堆积在顶部。我的假设是我没有在无限滚动回调中正确添加imagesLoaded方法,但我无法找到我的错误。这是代码

<script type="text/javascript">
    $(function(){
        var $container = $('#container');
        $container.imagesLoaded(function(){
          $container.masonry({
            itemSelector : '.tile',
            columnWidth : 240
          });
        });


    var $container = $('#container');
    $container.infinitescroll({
        navSelector  : ".flickr_pagination",            
                       // selector for the paged navigation (it will be hidden)
        nextSelector : "a.next_page",    
                       // selector for the NEXT link (to page 2)
        itemSelector : "div.tile"          
                       // selector for all items you'll retrieve
      },
      // trigger Masonry as a callback
      function( newElements ) {
        var $newElems = $( newElements );

        $container.imagesLoaded(function() {
            masonry( 'appended', $newElems );
        });
      }
    );
    });
</script>

第一个JQuery砌体调用工作正常,并没有被触及。这是似乎存在问题的第二部分。感谢您的帮助,如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:12)

这是答案

$(function(){

        var $container = $('#container');
        $container.imagesLoaded(function(){
          $container.masonry({
            itemSelector : '.tile',
            columnWidth : 240
          });
        });

    $container.infinitescroll({
          navSelector  : '.flickr_pagination',    // selector for the paged navigation 
          nextSelector : 'a.next_page',  // selector for the NEXT link (to page 2)
          itemSelector : '.tile',     // 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 ); 
            });
          }
        );
    });

问题是我在无限滚动回调函数中调用$ container上的.imagesLoaded()并且我应该在$ newElements上调用它。

相关问题