实现一个简单的瀑布流

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
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
.container {
position: relative;
}
.left,
.middle,
.right {
display: inline-block;
margin: 30px;
width: 500px;
height: auto;
vertical-align: top;
}
img {
width: 100%;
}
</style>
</head>

<body>
<div class="container flex">
<div class="left">
<img src="./avatar.jpg" alt="" />
<img src="./tutu.jpg" alt="" />
</div>
<div class="middle">
<img src="./sister.jpg" alt="" />
<img src="./tutu.jpg" alt="" />
</div>
<div class="right">
<img src="./sister.jpg" alt="" />
</div>
</div>
</body>
<script>
const el = ['./avatar.jpg', './sister.jpg', './tutu.jpg']
const $ = dom => document.querySelector(dom)
const left = $('.left')
const middle = $('.middle')
const right = $('.right')
window.addEventListener('scroll', () => {
const scrolltop =
document.documentElement.scrollTop || document.body.scrollTop
const scrollHeight = Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight
)
const screenHeight =
document.documentElement.clientHeight || document.body.clientHeight
judgeSize()
if (scrollHeight - (screenHeight + scrolltop) <= 100) {
for (let i = 0; i < 12; i++) {
let img = Math.floor(Math.random() * 3)
let dom = judgeSize()
dom.innerHTML += `<img src="${el[img]}"></img>`
}
}
})
const judgeSize = () => {
const leftHeight = left.offsetHeight
const middleHeight = middle.offsetHeight
const rightHeight = right.offsetHeight
const obj = {
[leftHeight]: left,
[middleHeight]: middle,
[rightHeight]: right
}
let arr = [leftHeight, middleHeight, rightHeight].sort((a, b) => a - b)
return obj[arr[0]]
}
</script>
</html>