Masonry items overlap images

If you visit this website (http://www.infismash.com/), you can see that the items are getting overlapped due to the fact that the images are loaded after the page loads.

I’m using the following syntax:

Read More
<div id="container" class="js-masonry" data-masonry-options='{ "columnWidth": 0, "itemSelector": ".masonry-content" }' >
    <div class="small-12 large-4 columns masonry-content">
        <article>
        ...
        </article>
    </div>
</div>

This one works well when the images are cached but when the images load after the masonry content is loaded, it overlaps.

Any suggestions how to solve this problem?

Thanks.

EDIT

The jQuery v1.11.1 is in the header.

I’ve added the imagesLoaded plugin and now the scripts (in the footer) are like this:

<script type='text/javascript' src='../masonry.pkgd.min.js'></script>
<script type='text/javascript' src='../imagesloaded.pkgd.js'></script>

Both of them are called and I’m trying to add masonry on the following:

<div id="container">

    <div class="small-12 large-4 columns masonry-content">
        <article>
        ...
        </article>
    </div>

    <div class="small-12 large-4 columns masonry-content">
        <article>
        ...
        </article>
    </div>

    <div class="small-12 large-4 columns masonry-content">
        <article>
        ...
        </article>
    </div>

</div>

I’m using the following JavaScript to trigger the masonry and imagesLoaded:

<script type="text/javascript">
    var $container = $('#container');

    $container.imagesLoaded( function(){
      $container.masonry({
        itemSelector : '.masonry-content'
      });
    });
</script>

This doesn’t work.

To put it simply, I’ve tried to add only the masonry like this:

<script>
    $(function(){
      $('#container').masonry({
        // options
        itemSelector : '.masonry-content',
        columnWidth : 240
      });
    });    
</script>

It doesn’t work also.

Any help?

EDIT

I have added class="js-masonry" <div id="container"> and now the masonry works but the imagesLoaded is not working. You can check the link provided at the top of this post.

Related posts

2 comments

  1. You should wait for images to load and than activate Masonry. Recommended plugin by David DeSandro (the author) is imagesloaded and the implementation is simple:

    var $container = $(‘#container’);

    $container.imagesLoaded( function(){
        $container.masonry({
            itemSelector : '.box'
        });
    });
    

    You can see the example at Masonry docs.

  2. Add the imagesloaded plugin found at http://desandro.github.io/imagesloaded/

    I think I have a solution for you. Does this work?

    http://codepen.io/tylerism/pen/LVeNKa

    HTML

    <h1>Masonry + imagesLoaded</h1>
    <div id="container">
      <img class="item" src="http://placebear.com/300/300" />
      <img class="item" src="http://placebear.com/500/300" />
      <img class="item" src="http://placebear.com/300/500" />
      <img class="item" src="http://placebear.com/300/300" />
      <img class="item" src="http://placebear.com/500/300" />
      <img class="item" src="http://placebear.com/300/500" />
      <img class="item" src="http://placebear.com/300/300" />
      <img class="item" src="http://placebear.com/500/300" />
      <img class="item" src="http://placebear.com/300/500" />
      <img class="item" src="http://placebear.com/300/500" />
      <img class="item" src="http://placebear.com/300/300" />
      <img class="item" src="http://placebear.com/500/300" />
      <img class="item" src="http://placebear.com/300/500" />
      <img class="item" src="http://placebear.com/300/500" />
      <img class="item" src="http://placebear.com/300/300" />
      <img class="item" src="http://placebear.com/500/300" />
      <img class="item" src="http://placebear.com/300/500" />
    </div>
    

    JS

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

    I don’t have your exact setup so it is hard for me to test it myself. If this doesn’t work we can try something else.

Comments are closed.