如何设置类似于Pinterest板的图像列表?

时间:2015-01-03 18:02:23

标签: php html css list

我有一张图片列表。我正试图让它们布局类似于Pintrest板。就像下面的示例图片一样: enter image description here


然而,我能够实现的唯一结果是:

enter image description here

这是我到目前为止所做的:

.continut {
  margin: 0 auto;
  width: 990px;
}
.rand {
  margin: 0 5px;
}
.box-img {
  position: relative;
  float: left;
  width: 25%;
}
.box-img  img {
  border: 0;
  max-width: 100%;
}
    <div class="continut">
      <div class="rand">
        <a class="box-img"><img src="http://40.media.tumblr.com/c8e11d8c0279c6b5cb4fd6d2b7278149/tumblr_nck3r1cGTy1qe4p6do1_1280.jpg"></a>
        <a class="box-img"><img src="http://40.media.tumblr.com/37f245ed9f4f4088b0d41c743d0f70c2/tumblr_mrp0r9nsak1rn86uco1_1280.jpg"></a>
        <a class="box-img"><img src="http://40.media.tumblr.com/9233cef94eb89f94343b9d5a2a890e5c/tumblr_mt35f2lHx01qgu2wio1_1280.jpg"></a>
        <a class="box-img"><img src="http://41.media.tumblr.com/a7950321c8cb51ae537f6ac1c5dd796d/tumblr_nghriwuQoi1r05o81o1_1280.jpg"></a>
        <a class="box-img"><img src="http://40.media.tumblr.com/f4c49ca91e71de45275ec71926deda22/tumblr_nfx14oxXy01tpw2ero1_1280.jpg"></a>
        <a class="box-img"><img src="http://40.media.tumblr.com/8615093ac8f3326e3e3715e4cd289de8/tumblr_nh4472KKyJ1qllucco1_1280.jpg"></a>
        <a class="box-img"><img src="http://40.media.tumblr.com/34b7afdd5501ba317ab047560fb96306/tumblr_nb6ti3I0KM1rpouqmo1_1280.jpg"></a>
        <a class="box-img"><img src="http://41.media.tumblr.com/f2be3ed1e09785589de69142494736cd/tumblr_ngjlr9ftiB1rp1s63o1_1280.jpg"></a>
      </div>
    </div>

2 个答案:

答案 0 :(得分:3)

使用Javascript:

这种类型的布局可以使用名为masonry的JavaScript库来实现。

以下是如何实施的示例:

var container = document.querySelector('#container');
var msnry = new Masonry( container, {
  // options
  itemSelector: '.box-img'
});
.box-img {
  width: 345px;
  width: 250px;
  margin-bottom: 20px;
  margin-left: 5px;
  float: left;
  margin-bottom: 15px;
  position: relative;
}

.box-img img {
  max-width: 100%;
}
<script src="http://masonry.desandro.com/masonry.pkgd.min.js"></script>


<div id="container">
 <a class="box-img">
    <img     src="http://40.media.tumblr.com/c8e11d8c0279c6b5cb4fd6d2b7278149/tumblr_nck3r1cGTy1qe4p6do1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/37f245ed9f4f4088b0d41c743d0f70c2/tumblr_mrp0r9nsak1rn86uco1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/9233cef94eb89f94343b9d5a2a890e5c/tumblr_mt35f2lHx01qgu2wio1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://41.media.tumblr.com/a7950321c8cb51ae537f6ac1c5dd796d/tumblr_nghriwuQoi1r05o81o1_1280.jpg">
  </a>
  
  <a class="box-img"><img src="http://40.media.tumblr.com/f4c49ca91e71de45275ec71926deda22/tumblr_nfx14oxXy01tpw2ero1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/8615093ac8f3326e3e3715e4cd289de8/tumblr_nh4472KKyJ1qllucco1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/34b7afdd5501ba317ab047560fb96306/tumblr_nb6ti3I0KM1rpouqmo1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://41.media.tumblr.com/f2be3ed1e09785589de69142494736cd/tumblr_ngjlr9ftiB1rp1s63o1_1280.jpg">
  </a>
</div>

  

注意:按下运行代码按钮后,它可能看起来不像   这也是因为体积小。按“全屏”   更真实的代表按钮。

请参阅Getting Started

上的文档

答案 1 :(得分:3)

仅使用CSS3列:

此示例仅使用CSS3来实现砌体样式布局。

#container {
    width: 100%;
    max-width: 700px;
    margin: 2em auto;
    margin: 0 auto;
    width: 990px;
}

.cols {
    -moz-column-count:3;
    -moz-column-gap: 3%;
    -moz-column-width: 30%;
    -webkit-column-count:3;
    -webkit-column-gap: 3%;
    -webkit-column-width: 30%;
    column-count: 3;
    column-gap: 3%;
    column-width: 30%;
}

.box-img img {
  max-width: 100%;
}
<div id="container" class="cols">
  <a class="box-img">
    <img     src="http://40.media.tumblr.com/c8e11d8c0279c6b5cb4fd6d2b7278149/tumblr_nck3r1cGTy1qe4p6do1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/37f245ed9f4f4088b0d41c743d0f70c2/tumblr_mrp0r9nsak1rn86uco1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/9233cef94eb89f94343b9d5a2a890e5c/tumblr_mt35f2lHx01qgu2wio1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://41.media.tumblr.com/a7950321c8cb51ae537f6ac1c5dd796d/tumblr_nghriwuQoi1r05o81o1_1280.jpg">
  </a>
  
  <a class="box-img"><img src="http://40.media.tumblr.com/f4c49ca91e71de45275ec71926deda22/tumblr_nfx14oxXy01tpw2ero1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/8615093ac8f3326e3e3715e4cd289de8/tumblr_nh4472KKyJ1qllucco1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://40.media.tumblr.com/34b7afdd5501ba317ab047560fb96306/tumblr_nb6ti3I0KM1rpouqmo1_1280.jpg">
  </a>
  
  <a class="box-img">
    <img src="http://41.media.tumblr.com/f2be3ed1e09785589de69142494736cd/tumblr_ngjlr9ftiB1rp1s63o1_1280.jpg">
  </a>
</div>