jQuery masonry layout complete event not working

geoffs3310 picture geoffs3310 · Dec 10, 2014 · Viewed 7.7k times · Source

I have followed the documentation exactly and the layout complete event isn't working. Example can be seen here:

http://jsfiddle.net/9464buy5/

<div id="items">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>
#items {
    width: 500px;
}
.item { 
    background: #ff0000;
    width: 200px;
    height: 200px;
    margin-bottom:20px;
}
$(document).ready(function() {
    var $container = $('#items');
    $container.masonry({
        itemSelector: '.item',
        columnWidth: 220,
        gutter: 20
    });

    $container.masonry('on', 'layoutComplete', function(msnryInstance, laidOutItems) {
        alert("");
    });
});

Anyone know if this a known bug or have I done something wrong?

Answer

Colin Oakes picture Colin Oakes · Sep 1, 2015

Here is the code to do this in jQuery:

var $grid = $('.grid').masonry({
    // disable initial layout
    isInitLayout: false,
    //...
});
// bind event
$grid.masonry( 'on', 'layoutComplete', function() {
    console.log('layout is complete');
});
// manually trigger initial layout
$grid.masonry();

This was taken from the bottom of this page: http://masonry.desandro.com/options.html