JQuery砌体!更新columnWidth On Window Resize

时间:2011-12-12 12:54:37

标签: jquery jquery-masonry window-resize

我正在开发响应式布局,我也在使用JQuery Masonry。

我正在使用以下脚本来获取当前列宽。

var curWidth; 
var detector;

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

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

我的JQuery Masonry init脚本是这样的......

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

我的第一个脚本正确地获取宽度,但是在砌体中宽度没有更新... 如何实现load& amp;调整大小功能,以便我的curWidth也会更新为砌体以及窗口大小调整

2 个答案:

答案 0 :(得分:11)

您需要在调整大小后设置砌体的columnWidth:

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

Yuo可以在这里阅读更多关于砌体方法的内容:http://masonry.desandro.com/methods.html

答案 1 :(得分:1)

使用流体布局时,

bindResize可用于调整容器中的所有项目(bindResize位于https://github.com/desandro/masonry/blob/master/dist/masonry.pkgd.js

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

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