简体   繁体   English

图片库-Pinterest是否喜欢使用CSS进行布局?

[英]Image gallery - Pinterest like layout with CSS?

I'm working on a dynamic php gallery. 我正在动态PHP画廊。 The thumbnails will all have the same width but various heights. 缩略图将具有相同的宽度,但高度各不相同。 They'll be placed from left to right. 它们将从左到右放置。 So, I don't want to use a five columns pattern. 因此,我不想使用五列模式。 I guess it's not possible to do it only with CSS, so maybe you know any jquery script that would do the job? 我猜想不可能仅使用CSS来做到这一点,所以也许您知道任何可以完成这项工作的jquery脚本? I guess this kind of gallery pattern is quite common... 我猜这种画廊模式很普遍。

http://i.stack.imgur.com/Xwdx0.png http://i.stack.imgur.com/Xwdx0.png

Here's the pure css solution using css3 columns. 这是使用css3列的纯css解决方案。 This isn't going to work in older browsers, read here (click). 在较旧的浏览器中将无法使用此功能, 请在此处阅读(单击)。 Live demo here (click). 现场演示在这里(单击)。

You can use masonry.js, isotope.js, or packery.js for more compatible js solutions. 您可以使用masonry.js,isotope.js或packery.js以获得更兼容的js解决方案。

<div class="col-5">
  <div class="sm"></div>
  <div class="lg"></div>
  <div class="sm"></div>
  <div class="sm"></div>
  <div class="lg"></div>

  <div class="lg"></div>
  <div class="sm"></div>
  <div class="lg"></div>
  <div class="lg"></div>
  <div class="lg"></div>  
</div>

css: CSS:

.col-5 {
  -webkit-column-count: 5;
  -moz-column-count: 5;
  column-count: 5;
}

.col-5 > div {
  display: inline-block;
}

.sm {
  height: 75px;
}
.lg {
  height: 125px;
}

If you're trying to make the layout the 'pintrest' way, you can have an array of x columns and iterate through each column to check for the shortest height, and add the next box in that column. 如果您要使布局成为“ pintrest”方式,则可以有一个x列数组,并遍历每列以检查最短高度,然后在该列中添加下一个框。

That way you would know it works for all browsers [unless they have js disabled] and then you can style the width of the columns. 这样,您将知道它适用于所有浏览器(除非禁用了js),然后您可以设置列宽的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM