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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
width: 100%;
height: 100%;
}
.main{
position: static;
margin: 0;
border: 0;
padding: 0;
background-color: antiquewhite;
overflow: hidden;
}
.main div{
width: 100px;
padding: 10px;
position: absolute;
display: inline-block;
}
</style>
</head>
<body>
<div class="main">
<div class="box"><img style="width:100%;height: 200px"></div>
<div class="box"><img style="width:100%;height: 151px"></div>
<div class="box"><img style="width:100%;height: 256px"></div>
<div class="box"><img style="width:100%;height: 331px"></div>
<div class="box"><img style="width:100%;height: 222px"></div>
<div class="box"><img style="width:100%;height: 444px"></div>
<div class="box"><img style="width:100%;height: 111px"></div>
<div class="box"><img style="width:100%;height: 531px"></div>
<div class="box"><img style="width:100%;height: 661px"></div>
<div class="box"><img style="width:100%;height: 145px"></div>
<div class="box"><img style="width:100%;height: 103px"></div>
<div class="box"><img style="width:100%;height: 133px"></div>
</div>
</body>
<script>
f();
window.onresize = () => f()
function f() {
var box = document.getElementsByClassName("box"),
boxWidth = box[0].offsetWidth,
screenWidth = document.getElementsByClassName("main")[0].offsetWidth,
cols = parseInt(screenWidth/boxWidth),
heightArr = [];
for(let i =0 ;i<box.length;i++){
let boxHeight = box[i].offsetHeight,
left = 0,
top = 0;
box[i].style.position = "absolute";
if(i<cols){
left = i * boxWidth;
top = 0;
heightArr[i] = boxHeight;
}else{
let num = parseInt(i%cols),
min = Math.min(...heightArr),
key = heightArr.indexOf(min);
left = key * boxWidth;
top = min;
heightArr[key] += boxHeight;
}
box[i].style.left = left + "px";
box[i].style.top = top + "px";
}
}
</script>
</html>

最后更新: 2021年09月23日 14:05