imagesLoaded method not working with JQuery masonry and infinite scroll

前端 未结 1 464
暗喜
暗喜 2021-02-03 13:06

I\'ve been using JQuery masonry and now I\'m adding infinite scroll. There are images in nearly every masonry \"brick\" and before I was using infinite scroll the images loaded

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 13:56

    Here's the answer

    $(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 ); 
                });
              }
            );
        });
    

    The problem was I was calling .imagesLoaded() on $container in the infinite scroll callback function and I should have been calling it on $newElements.

    0 讨论(0)
提交回复
热议问题