JQuery Masonry ! Update columnWidth On Window Resize

*爱你&永不变心* 提交于 2019-12-10 09:35:42

问题


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


回答1:


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




回答2:


bindResize can be used to resize all items in the container when using a fluid layout (bindResize is in https://github.com/desandro/masonry/blob/master/dist/masonry.pkgd.js

$container.masonry({
    itemSelector: '.container'
});

$(window).resize(function () {
    $container.masonry('bindResize')
});


来源:https://stackoverflow.com/questions/8474627/jquery-masonry-update-columnwidth-on-window-resize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!