Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to trigger/reload Masonry plugin on click

Because i have different tabs, masonry is not loading the hidden items, so when i click on a new tab the images stack onto each other, i know this question has been asked before and answered with trigger masonry by clicking the tab, but how would i go about doing this without messing up the first tab.

Currently calling masonry with

$(function(){
$('#container').masonry({
// options
itemSelector : '.item',
columnWidth : 260
   });
});`
$(window).load(function(){   $('#container').masonry(); });

and the same for tab 2 but with a different ID - #container2

the tab one works perfectly but tab two stacks the images, until you resize the browser which fixes it and works as normal

like image 432
user2456448 Avatar asked Jun 07 '13 15:06

user2456448


Video Answer


1 Answers

Do it like this:

$(function(){
    $('#container').masonry({
        // options
        itemSelector : '.item',
        columnWidth : 260
   });
});

var masonryUpdate = function() {
    setTimeout(function() {
        $('#container').masonry();
    }, 500);
}
$(document).on('click', masonryUpdate);
$(document).ajaxComplete(masonryUpdate);

Never worry about it again! Or, you may call it again after other animations, like:

$('#something').slideDown(600, masonryUpdate);

Even if you don't, just click anywhere in the page and masonry will update.

like image 171
Pedro Moreira Avatar answered Sep 20 '22 09:09

Pedro Moreira