-
-
Notifications
You must be signed in to change notification settings - Fork 676
/
native_lazyload_conditional.html
215 lines (201 loc) · 6.99 KB
/
native_lazyload_conditional.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>Conditional native lazy loading - Lazyload demos</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
ul,
li {
list-style-type: none;
margin: 0;
padding: 0;
}
a,
img {
display: block;
}
img {
border: 0;
width: 220px;
height: 280px;
}
img:not([src]) {
visibility: hidden;
}
.warning {
padding: 1em;
color: black;
background: lightyellow;
border-radius: 1em;
border: 1px solid gray;
}
/* Fixes Firefox anomaly during image load */
@-moz-document url-prefix() {
img:-moz-loading {
visibility: hidden;
}
}
</style>
</head>
<body>
<h1>Conditional native lazy loading demo</h1>
<h2>Images</h2>
<div id="results1" class="results">
<ul>
<li>
<a href="#/it/donna/stivaletti_cod44721746jj.html"
><img alt="Stivaletti" loading="eager" src=./images/440x560-01.webp width="220"
height="280" /></a
>
</li>
<li>
<a href="#/it/donna/open-toe_cod44740806jx.html"
><img alt="Open toe" loading="eager" src=./images/440x560-02.webp width="220"
height="280" /></a
>
</li>
<li>
<a href="#/it/donna/sneakers-tennis-shoes-basse_cod44735977gr.html"
><img alt="Sneakers & Tennis" class="lazy" data-src=./images/440x560-03.webp
width="220" height="280" /></a
>
</li>
<li>
<a href="#/it/donna/sneakers-tennis-shoes-basse_cod44738717am.html"
><img alt="Sneakers & Tennis shoes basse" class="lazy"
data-src=./images/440x560-04.webp width="220" height="280" /></a
>
</li>
<li>
<a href="#/it/donna/sneakers-tennis-shoes-alte_cod44739940cb.html"
><img alt="Sneakers & Tennis shoes alte" class="lazy"
data-src=./images/440x560-05.webp width="220" height="280" /></a
>
</li>
<li>
<a href="#/it/donna/sneakers-tennis-shoes-alte_cod44740860xg.html"
><img alt="Sneakers & Tennis shoes alte" class="lazy"
data-src=./images/440x560-06.webp width="220" height="280" /></a
>
</li>
<li>
<a href="#/it/donna/sneakers-tennis-shoes-basse_cod44738719vn.html"
><img alt="Sneakers & Tennis shoes basse" class="lazy"
data-src=./images/440x560-07.webp width="220" height="280" /></a
>
</li>
</ul>
</div>
<h2>Iframes</h2>
<iframe class="lazy" data-src="./iframes/i01.html" frameborder="0"></iframe>
<iframe class="lazy" data-src="./iframes/i02.html" frameborder="0"></iframe>
<iframe class="lazy" data-src="./iframes/i03.html" frameborder="0"></iframe>
<h2>Preload = none</h2>
<video
class="lazy"
controls
preload="none"
width="620"
data-src="./videos/1920x1080-01.mp4"
data-poster="./videos/1920x1080-01.webp"
>
<source type="video/mp4" data-src="./videos/1920x1080-01.mp4" />
<source type="video/ogg" data-src="./videos/1920x1080-01.ogv" />
<source type="video/avi" data-src="./videos/1920x1080-01.avi" />
</video>
<h2>Preload = metadata</h2>
<video
class="lazy"
preload="metadata"
controls
width="620"
data-src="./videos/1920x1080-02.mp4"
data-poster="./videos/1920x1080-02.webp"
>
<source type="video/mp4" data-src="./videos/1920x1080-02.mp4" />
<source type="video/ogg" data-src="./videos/1920x1080-02.ogv" />
<source type="video/avi" data-src="./videos/1920x1080-02.avi" />
</video>
<script src="../dist/lazyload.min.js"></script>
<script>
(function () {
function logElementEvent(eventName, element) {
console.log(Date.now(), eventName, element.getAttribute("data-src"));
}
var callback_enter = function (element) {
logElementEvent("🔑 ENTERED", element);
};
var callback_exit = function (element) {
logElementEvent("🚪 EXITED", element);
};
var callback_loading = function (element) {
logElementEvent("⌚ LOADING", element);
};
var callback_loaded = function (element) {
logElementEvent("👍 LOADED", element);
};
var callback_error = function (element) {
logElementEvent("💀 ERROR", element);
element.src = 'url("./images/440x560-Error.webp")';
};
var callback_finish = function () {
logElementEvent("✔️ FINISHED", document.documentElement);
};
var callback_cancel = function (element) {
logElementEvent("🔥 CANCEL", element);
};
function supportsNativeLazyVideo() {
return "loading" in HTMLVideoElement.prototype;
}
// Single instance using native lazy loading on images, videos and iframes
function createSingleInstance() {
var lazyAll = new LazyLoad({
use_native: true,
// NOTICE: they won't be called since the loading will be native
callback_enter: callback_enter,
callback_exit: callback_exit,
callback_cancel: callback_cancel,
callback_loading: callback_loading,
callback_loaded: callback_loaded,
callback_error: callback_error,
callback_finish: callback_finish
});
}
// Double instance using native lazy loading on images and iframes,
// and JS driven lazy loading on videos
function createDoubleInstance() {
var lazyImgsIframes = new LazyLoad({
elements_selector: "img.lazy, iframe.lazy",
use_native: true,
// Assign the callbacks defined above
// NOTICE: they won't all called since the loading will be native
callback_enter: callback_enter,
callback_exit: callback_exit,
callback_cancel: callback_cancel,
callback_loading: callback_loading,
callback_loaded: callback_loaded,
callback_error: callback_error,
callback_finish: callback_finish
});
var lazyVideos = new LazyLoad({
elements_selector: "video.lazy",
// Assign the callbacks defined above
callback_enter: callback_enter,
callback_exit: callback_exit,
callback_cancel: callback_cancel,
callback_loading: callback_loading,
callback_loaded: callback_loaded,
callback_error: callback_error,
callback_finish: callback_finish
});
}
if (supportsNativeLazyVideo()) {
createSingleInstance();
} else {
createDoubleInstance();
}
})();
</script>
</body>
</html>