winney

It is never too old to learn.

0%
winney

瀑布流效果

JS实现瀑布流效果

1
2
3
4
5
<div class="content">
<div class="item"><img src="./images/pubuliu/1.jpg" alt=""></div>
.....
<div class="item"><img src="./images/pubuliu/30.jpg" alt=""></div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*{
margin: 0;
padding: 0;
}
.content{
width: 100%;
height: 2000px;
}
.content>div{
float: left;
border: 1px solid #ddd;
padding: 10px;
}
.content>div>img{
width: 130px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
window.onload =  function () {
// 获取主容器的宽度
var content = document.getElementsByClassName('content')[0];
contentWidth = content.offsetWidth;

// 获取单个图片的宽度
var imgs = content.children;
var imgsWidth = imgs[0].offsetWidth;

// 第一行可以排列多少张图片
var nums = Math.floor(contentWidth/imgsWidth)

// 收集第一排的所有高度
var arrHeight = [];

for(var i = 0; i < imgs.length; i++) {
if(i < nums) {
arrHeight.push(imgs[i].offsetHeight)
} else {
// 创建一个元素的对象
var obj = {
minH: arrHeight[0],
minI: 0
}

for(var j = 0; j < arrHeight.length; j++) {
if(arrHeight[j] < obj.minH) {
obj.minH = arrHeight[j]
obj.minI = j
}
}

imgs[i].style.position = "absolute"
imgs[i].style.left = imgs[obj.minI].offsetLeft + "px"
imgs[i].style.top = obj.minH + "px"
arrHeight[obj.minI] = arrHeight[obj.minI] + imgs[i].offsetHeight
}
}
}

纯CSS实现瀑布流效果-方法1

1
2
3
4
5
6
7
8
// 结构
<body>
<div class="shell">
<div class="image"><img src="./images/pubuliu/1.jpg" alt=""></div>
......
<div class="image"><img src="./images/pubuliu/30.jpg" alt=""></div>
</div>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 样式
<style>
body{
display: flex;
justify-content: center;
}
.shell{
max-width: 1300px;
column-count: 5;
column-gap: 15px;
}
.image{
margin-bottom: 15px;
}
.image img{
width: 100%;
}
@media (max-width:1200px){
.shell{
column-count: 4;
}
}
@media (max-width:850px){
.shell{
column-count: 3;
}
}
@media (max-width:600px){
.shell{
column-count: 2;
}
}
</style>

pinterest是使用瀑布流最具代表性的网站

纯CSS实现瀑布流效果-方法2-带编号(竖向)

1
2
3
4
5
6
7
8
// 结构
<body>
<div class="shell">
<div class="image"><img src="./images/pubuliu/1.jpg" alt=""></div>
......
<div class="image"><img src="./images/pubuliu/30.jpg" alt=""></div>
</div>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 样式
.shell{
column-count: 4;
column-gap: 0;
}
.image{
padding: 2px;
position: relative;
counter-increment: item-counter;
}
.image img{
display: block;
width: 100%;
height: auto;
}
.image::after{
position: absolute;
display: block;
top: 2px;
left: 2px;
width: 24px;
height: 24px;
text-align: center;
line-height: 24px;
background-color: #000;
color: #fff;
content: counter(item-counter);
}

https://codepen.io/stevenlei/pen/vYNZaZN

纯CSS实现瀑布流效果-方法3-带编号(横向-左到右,上到下)-flexbox

1
2
3
4
5
6
7
8
9
<div class="masonry">
<div class="item">
<img src="https://picsum.photos/360/460?random=1">
</div>
......
<div class="item">
<img src="https://picsum.photos/360/420?random=15">
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
body {
margin: 4px;
font-family: Helvetica;

/* Centering & Limit Width */
margin: auto;
width: 720px;
}

.masonry {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 1000px;
}

.item {
position: relative;
width: 25%;
padding: 2px;
box-sizing: border-box;
counter-increment: item-counter;
}

.item img {
display: block;
width: 100%;
height: auto;
}

.item::after {
position: absolute;
display: block;
top: 2px;
left: 2px;
width: 24px;
height: 24px;
text-align: center;
line-height: 24px;
background-color: #000;
color: #fff;
content: counter(item-counter);
}

.item:nth-child(4n+1) {
order: 1;
}

.item:nth-child(4n+2) {
order: 2;
}

.item:nth-child(4n+3) {
order: 3;
}

.item:nth-child(4n) {
order: 4;
}

图片资源:picsum

引用方式:https://picsum.photos/360/460?random=1

代码参考:https://codepen.io/stevenlei/pen/PoPjBBm