MANnDAaR MANnDAaR - 6 months ago 36
jQuery Question

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

Answer

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