css瀑布流

css瀑布流

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>css瀑布流</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 80%;
margin: 0 auto;
column-count: 8;
-webkit-column-gap: 1em;
column-gap: 1em;
}
.item {
background-color: #eee;
break-inside: avoid;
-webkit-column-break-inside: avoid;
margin-bottom: 1em;
}
</style>
</head>
<body>
<div class="box">
<div class="item"></div>
</div>
<script>
const randomHeight = () => `${(Math.random() * 2 + 0.5) * 100}%`
const html = [...Array(100).keys()]
.map((i) => `<div class="item" style="padding-bottom: ${randomHeight()}">${i}</div>`)
.join('')
document.querySelector('.box').innerHTML = html
</script>
</body>
</html>