Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Masonry & LazyLoad doesn't want to work together

I am setting up a site for a photographer. It should be built using the Bootstrap 3 framework, and he wants to have a masonry with over 400 images on one page. For this to work LazyLoad is a must. I have now spent several days trying to get LazyLoad to work with Desandros Masonry but with no success..

I've tried all of the examples that one finds googling, but most posts/sites/forums just redirects you, or have copied this stackoverflow answer: Combining LazyLoad and Jquery Masonry

I've tried both methods but unfortunately I get nothing but grey hair..... :(

Here is a simplified live version of the page im working on:
http://nr.x10.mx
In this example I have added a fade-in on page-load, but left the LazyLoad out since I can get it to work.

And here you have a FIDDLE of the following

var container = document.querySelector('#ms-container');
imagesLoaded( container, function() 
{
   var msnry = new Masonry(container, 
                                     { itemSelector: '.ms-item',
                                       columnWidth: '.ms-item',});
}); 

You can also download the whole pack here, including the jquery.lazyload.js HERE

Any help would be highly appreciated


UPDATE

Here you can have 4 different examples of the different problems that occur. I also found to my joy that the Bootstrap .img-responsive class is interfering with LazyLoad.

1 - Masonry without LazyLoad
2 - Masonry and Lazyload - Masonry breaks down and LazyLoad has no effect
3 - LazyLoad without Masonry - LazyLoad has no effect
4 - LazyLoad without Masonry and Bootsrap "img-responsive" removed
5 - Masonry & LazyLoad using first method of SO answer mentioned above
6 - Masonry & LazyLoad using second method of SO answer mentioned above
Both of the last ones gives the following error: [Error] TypeError: 'undefined' is not a function (evaluating '$container.imagesLoaded') global code (5.html, line 110)

Updated zip HERE

Again, any asisstance would be highly appreciated, thank you

like image 399
no0ne Avatar asked Jan 29 '14 14:01

no0ne


People also ask

Which is a masonry material?

The common materials of masonry construction are brick, building stone such as marble, granite, and limestone, cast stone, concrete block, glass block, and adobe. Masonry is generally a highly durable form of construction.

What is meant by masonry building?

2 Modern masonry buildings. Modern masonry buildings refer to buildings in which modern materials are used. In this regard, we can refer to buildings that have ceilings with modern materials such as concrete-block joists, concrete slabs, and walls made of pressed brick, cement blocks, perlite, and so on.

What is the difference between brickwork and masonry?

The simple explanation is that there is a difference in skillset. Bricklayers are typically focused on construction using clay or concrete bricks, and blocks. Masons focus on construction involving stone, marble, granite, and other similar natural materials.

What is the difference between concrete and masonry?

Concrete is a construction material consisting of conglomerate gravel, pebbles, broken stone or slag in a mortar or cement matrix. Masonry is building and fabricating in stone, clay, brick, or concrete block. Masonry also refers to the building units (stone, brick, etc.) themselves.


2 Answers

I made your 5.html work by using the javascript files of the fiddle present on the SO link you posted, answered by Nathan Do. So you probably just had bad versions of the scripts.

The scripts linked on that page are: http://cdn.jsdelivr.net/masonry/2.1.08/jquery.masonry.min.js and http://cdn.jsdelivr.net/jquery.lazyload/1.8.4/jquery.lazyload.js

Here's your original fiddle updated with that page's method: http://jsfiddle.net/L9RLe/

like image 148
ariel Avatar answered Sep 19 '22 06:09

ariel


In your case, though you are creating masonry and adding lazyload effect, the images get overlapped.

You need to follow steps as :

  1. Create Dynamic HTML structure for images along with their respective aspect ratio height and width. Note: images should have the required attributes for applying lazy load effect i.e. class="lazy" data-original="actual image url" and src="dummy imageurl".

  2. Apply lazy load effect.

  3. Then create Masonry.

Lets have an example :

Suppose I am having a javascript array with some image related data as,

var gallery_itemList= [
    {url: "images/example1.jpg", width:"1170", height:"460"},
    {url: "images/example2.jpg", width:"800", height:"320"},
    {url: "images/example3.jpg", width:"1200", height:"870"}];

And below prototype for creating dynamic html, applying lazyload effect and creating Masonry effect as :

var masonryGallery = {
    gallery :'', // gallery to create
    genarateGallery : function() {
      // generate each item html
      var inHTML="", i;
      for(i=0;i<gallery_itemList.length;i++){
        var iWidth, iHeight, fHeight=0;
        iWidth=parseInt(gallery_itemList[i].width);
        iHeight=parseInt(gallery_itemList[i].height);
        fHeight = Math.round((iHeight*300)/iWidth);

        inHTML+='<div class="item" style="height:'+fHeight+'px">';
        inHTML+='<img class="lazy" src="images/loading.gif" data-original="'+gallery_itemList[i].url+'"/>';
        inHTML+='</div>';   
      }
      //add generated html to gallery
      $(masonryGallery.gallery).append(inHTML);       
    },
    applyLazyload : function(){
      $("img.lazy").lazyload();
    },
    createMasonry : function(){
      // create Masonry
      $(masonryGallery.gallery).masonry({
        columnWidth: 350,
        itemSelector: '.item',
        isFitWidth: true,
        isAnimated: !Modernizr.csstransitions
      }).imagesLoaded(function() {
        $(this).masonry('reload');
      });
    },
    init : function(givenGallery) {
        masonryGallery.gallery = givenGallery; // set gallery 
        masonryGallery.genarateGallery(); // generate gallery html
        masonryGallery.applyLazyload();  // apply lazyload effect
        masonryGallery.createMasonry();  // apply masonry effect
    }
};

/* Gallery Intialisation */
(function(){masonryGallery.init('div#content');})(); 
like image 23
Mazzu Avatar answered Sep 21 '22 06:09

Mazzu