Freemasonry script event during layout initialization

How to execute my function every time a Masonry script reorganizes elements, for example, when resizing a window? In this example, it only works for the first time.

$(document).ready(function()
{
    $('#container').masonry({
     // layoutComplete: masonry_refreshed(), // <-- doesn't work propriety
        itemSelector: '.item'
    });

    masonry_refreshed();
});

function masonry_refreshed()
{
    alert('Masonry refreshed')
}

DEMO: http://codepen.io/anon/pen/HeuKw

+4
source share
2 answers

check: http://masonry.desandro.com/events.html you can use the following masonry event:

msnry.on( 'layoutComplete', masonry_refreshed );

Note that msnry is the name of the variable in which you defined the masonry.


EDIT: Full demo code:

$(document).ready(function(){
    var container = document.querySelector('#container');
    var msnry = new Masonry(container,{
        itemSelector: '.item',
        columnWidth: 70
    });

    msnry.on( 'layoutComplete', masonry_refresh );

    function masonry_refresh(){
        console.log("Masonry resized!");
    }

});

demo: http://jsfiddle.net/Cd6ce/1/


EDIT2: jQuery:

$(document).ready(function(){
    $('#container').masonry({
        itemSelector: '.item',
        columnWidth: 70
    });
    var msnry = $('#container').data('masonry');
    msnry.on( 'layoutComplete', masonry_refresh );

    function masonry_refresh(){
        console.log("Masonry resized!");
    }

});

demo: http://jsfiddle.net/Cd6ce/4/

+4

"" . .: http://masonry.desandro.com/options.html

msnry = new Masonry( self.el, {
    isInitLayout: false,
    itemSelector: '.brick'
});

msnry.on( 'layoutComplete', function(){
    console.log('completed');
});

msnry.layout();
+2

All Articles