Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Masonry ! Update columnWidth On Window Resize

I'm working working on Responsive Layout where I'm using JQuery Masonry as well.

I'm using following script to get current column width.

var curWidth; 
var detector;

detector = $('.magic-column');
curWidth = detector.outerWidth(true);

$(window).resize(function(){
    if(detector.outerWidth(true)!=curWidth){
        curWidth = detector.outerWidth(true);
    }
});

My JQuery Masonry init script is something like this..

$(window).load(function(){
     $(function (){
            $wall.masonry({
                    singleMode: true, 
                    columnWidth: curWidth, // This needs to be update on window load & resize both //
            });
     });
});

My 1st script is fetching width correctly, but in masonry that width isn't updating... How can I implement both load & resize function so that my curWidth will be updated for Masonry as well on window resize

like image 471
MANnDAaR Avatar asked Dec 16 '22 06:12

MANnDAaR


1 Answers

You need to set the columnWidth of the masonry after resize:

$(window).resize(function(){
    if(detector.outerWidth(true)!=curWidth){
        curWidth = detector.outerWidth(true);
        $wall.masonry( 'option', { columnWidth: curWidth });
    }
});

Yuo can read more about the masonry methods here: http://masonry.desandro.com/methods.html

like image 60
Variant Avatar answered Jan 31 '23 19:01

Variant