砌体网格布局,缩放以适应容器

时间:2014-04-10 18:14:30

标签: jquery css jquery-masonry jquery-isotope packery

所以我尝试做这样的事情:http://prothemeus.com/demo/litho/

我遇到过: http://masonry.desandro.com/http://isotope.metafizzy.co/http://packery.metafizzy.co/所有这些都非常相似,但它们都不像顶级网站那样提供扩展。我如何使用其中一个插件创建类似的东西,或者有人知道一个提供缩放以适合容器的选项吗?

就演示而言,我能够发现同位素处理布局,但我无法追踪实际缩放的代码。

任何帮助都会受到极大关注。

2 个答案:

答案 0 :(得分:1)

我完成了忽略所有插件,只有我缩小的smartresize因为它在我发现它的地方解压缩。不确定它是来自保罗爱尔兰人还是其他人,无论是哪种方式,都是jsfiddle。 http://jsfiddle.net/matthewabrman/6R2DU/

//smartresize
(function(e,t){var n=function(e,t,n){var r;return function(){function u(){if(!n)e.apply(s,o);r=null}var s=this,o=arguments;if(r)clearTimeout(r);else if(n)e.apply(s,o);r=setTimeout(u,t||100)}};jQuery.fn[t]=function(e){return e?this.bind("resize",n(e)):this.trigger(t)}})(jQuery,"smartresize")

function redraw_UI() {
    var content_width = $('.content').width()+20;
    images_per_row = Math.floor(content_width / 240);
    var width = Math.round(content_width / images_per_row);
    var height = Math.round(width/3*2);
    $('.content .item').each(function(id){
        var x = Math.round((id % images_per_row) * width);
        var y = Math.floor(id/images_per_row) * height + Math.floor(id/images_per_row) * 20;
        if (navigator.appName.indexOf("Internet Explorer")!=-1){
            $(this).animate({width: width-20+'px', height: height+'px', left: x, top: y},600);
        } else {
            $(this).css({'width': width-20+'px', 'height': height+'px', 'left': x, 'top': y });
        }
    });

    if (images_per_row == 1) {
        closeImagePreview();
    }
}

$(window).smartresize(redraw_UI);
$(window).ready(redraw_UI);

答案 1 :(得分:0)

Masonry使用bindResize方法支持此操作:http://masonry.desandro.com/methods.html#bindresize

$container.masonry(options);
$container.masonry('bindResize')

这将在调整大小时触发布局。你也可以这样做

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

会触发重新布局,但你应该限制它以避免被调用。默认情况下,这将缩放以适合容器。

然后您需要做的是使容器响应,以便根据窗口大小调整大小。为此,您可以查看bootstrap或自己动手。