-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyLoadImage.html
126 lines (105 loc) · 3.78 KB
/
lazyLoadImage.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.image-list {
height: 90vh;
width: 180px;
padding: 10px 10px;
border: 1px solid #000;
overflow: hidden auto;
scroll-behavior: smooth;
box-sizing: border-box;
}
.ace-image {
width: 150px;
height: 100px;
border: 1px solid #000;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="image-list"></div>
<script>
window.onload = function () {
let viewportHeight
let loadImageCount = 0
let imageList = document.querySelector('.image-list')
let imageSrc = '../images/bg.jpg'
function computedViewportHeight() {
viewportHeight = imageList.clientHeight
}
function throttle(handler, wait) {
if (!wait) wait = 300
let timer = null
let previous = 0
return function () {
let current = new Date()
let interval = wait - (current - previous)
let self = this || window
let args = arguments
if (interval < 0) {
clearTimeout(timer)
timer = null
handler.apply(self, args)
previous = new Date()
} else if (!timer) {
timer = setTimeout(function () {
clearTimeout(timer)
timer = null
handler.apply(self, args)
previous = new Date()
}, interval)
}
}
}
function createImage() {
let img = document.createElement('img')
img.className = 'ace-image'
img.setAttribute('data-src', imageSrc)
return img
}
function addImage() {
let fragment = document.createDocumentFragment()
for (let i = 0; i < 20; i++) {
fragment.append(createImage())
}
imageList.append(fragment)
}
let loadImageHandler = throttle(loadImage, 1000)
let renderHandler = throttle(render, 1000)
function loadImage() {
const imgs = imageList.querySelectorAll('.ace-image')
if (loadImageCount === imgs.length) {
imageList.removeEventListener('scroll', loadImageHandler)
window.removeEventListener('resize', renderHandler)
return
}
const viewportPosition = viewportHeight + imageList.scrollTop
for (let i = loadImageCount; i < imgs.length; i++) {
const img = imgs[i]
const src = img.getAttribute('data-src')
if (src && img.offsetTop < viewportPosition) {
img.src = src
img.removeAttribute('data-src')
loadImageCount++
}
}
}
function render() {
computedViewportHeight()
loadImage()
}
addImage()
render()
imageList.addEventListener('scroll', loadImageHandler)
window.addEventListener('resize', renderHandler)
}
</script>
</body>
</html>