Javascript 不同大小图像的响应网格

Javascript 不同大小图像的响应网格,javascript,html,css,responsive-design,Javascript,Html,Css,Responsive Design,在Flickr上,我发现探索部分非常吸引人: 显示不同大小的图片是一个很好的解决方案,如果调整浏览器窗口的大小,所有行都将适应布局 [编辑] 如果不使用下面答案中建议的砌体库,您将如何构建类似的东西? 我在一些javascript伪代码上做了一些工作,我认为逻辑应该是完整的。你觉得怎么样?如何在一个工作的图书馆里翻译 var rowWidhtPx=document.getElementById('gridContainer').offsetWidth;; var maxRowHeightPX

在Flickr上,我发现探索部分非常吸引人:

显示不同大小的图片是一个很好的解决方案,如果调整浏览器窗口的大小,所有行都将适应布局

[编辑] 如果不使用下面答案中建议的砌体库,您将如何构建类似的东西? 我在一些javascript伪代码上做了一些工作,我认为逻辑应该是完整的。你觉得怎么样?如何在一个工作的图书馆里翻译

var rowWidhtPx=document.getElementById('gridContainer').offsetWidth;; 
var maxRowHeightPX= 300;
var minRowHeightPX= 200;

var imageArray=[
    {
        src:'somesrc',
        width:200,
        height:300,
    },
    ...
    ,{
        src:'somesrc',
        width:200,
        height:300,
    }
];
var rowMatrix;
var rowsIndex=0;
rowMatrix[rowsIndex].setHeight(maxRowHeightPX);
imageArray.forEach(function (img,index){
    img.scaleHeight(maxRowHeightPX);

    if(rowMatrix[rowsIndex].actualWidth+img.widht<=rowWidhtPx){
        rowMatrix[rowsIndex].push(img);//set margin gutter to prev image
    }else{
        rowMatrix[rowsIndex].scaleHeight(minRowHeightPX) ;//scale also image heights
        if(rowMatrix[rowsIndex].actualWidth+img.scaleHeight(minRowHeightPX)<=rowWidhtPx ){
            rowMatrix[rowsIndex].push(img);
        }else{
            rowMatrix[rowsIndex].setOptimalHeight();
            //starting from minimum height, take total widht and set it to rowWidthPX, and scale the height accordingly 
            //then takes single images and set also the height              
            rowsIndex++; // add new row
        }
    }
}); 
var rowWidhtPx=document.getElementById('gridContainer').offsetWidth;;
var maxRowHeightPX=300;
var minRowHeightPX=200;
var成像阵列=[
{
src:'somesrc',
宽度:200,
身高:300,
},
...
,{
src:'somesrc',
宽度:200,
身高:300,
}
];
var矩阵;
var rowsIndex=0;
rowMatrix[rowsIndex].setHeight(maxRowHeightPX);
forEach(函数(img,索引){
img.刻度高度(maxRowHeightPX);

如果(rowMatrix[rowsIndex].actualWidth+img.widt我们称之为masonary grids….您可以在这里检查它

库……您可以使用它来创建类似的图库。

虽然您可以使用自定义CSS样式创建这样的网格,但使用响应型CSS框架(如Bootstrap或Buffy)要快得多,也更容易。 以Bootstrap为例,创建响应网格布局非常容易。假设您想要一个类似Flickr的页面,在该页面上,第一行应该有4个图像,第二行应该有2个,第三行应该有6个图像。当屏幕尺寸变小时,图像需要相应地堆叠和缩放。使用Bootstrap,我们可以这样做:

<div class="container">
  <div class="row">
    <div class="col-xs-4 col-md-3">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-3">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-3">
      <image src="..." alt="...">
    </div>
  </div>
  <div class="row">
    <div class="col-xs-12 col-md-6">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-12 col-md-6">
      <image src="..." alt="...">
    </div>
  </div>
  <div class="row">
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
  </div>
</div>

其中,
.row
是图像行,
.col-*-*
类是适当的列。您可以在此处了解有关引导的更多信息:


希望这能回答您的问题。

我在文档中找不到任何东西可以让它处理成行的图像,因为flickr认为您需要了解什么是Masonary?以及为什么这里需要Masonary。yahoo团队在这些工作背后做了大量工作。我给您的链接将帮助您使用一些基本ide正如。但要创建这么多很棒的东西,你需要在上面做一些额外的工作。这不是一个简单的网格:列的数量根据适合行的图像而变化。Flickr使用javascript来处理带有背景图像的链接大小。所以基本上,它根本不是一个网格系统。是的,好的,不是一个网格系统网格正如我们可以告诉思考html+css响应的网格系统一样,但是布局结果仍然是一个网格:)不管怎样,这种性质的请求对于SO来说都是离题的