-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ls.unveilhooks.js
184 lines (154 loc) · 4.25 KB
/
ls.unveilhooks.js
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
/*
This plugin extends lazySizes to lazyLoad:
background images, videos/posters and scripts
Background-Image:
For background images, use data-bg attribute:
<div class="lazyload" data-bg="bg-img.jpg"></div>
Video:
For video/audio use data-poster and preload="none":
<video class="lazyload" preload="none" data-poster="poster.jpg" src="src.mp4">
<!-- sources -->
</video>
For video that plays automatically if in view:
<video
class="lazyload"
preload="none"
muted=""
data-autoplay=""
data-poster="poster.jpg"
src="src.mp4">
</video>
Scripts:
For scripts use data-script:
<div class="lazyload" data-script="module-name.js"></div>
Script modules using require:
For modules using require use data-require:
<div class="lazyload" data-require="module-name"></div>
*/
(function(window, factory) {
var globalInstall = function(){
factory(window.lazySizes);
window.removeEventListener('lazyunveilread', globalInstall, true);
};
factory = factory.bind(null, window, window.document);
if(typeof module == 'object' && module.exports){
factory(require('lazysizes'));
} else if (typeof define == 'function' && define.amd) {
define(['lazysizes'], factory);
} else if(window.lazySizes) {
globalInstall();
} else {
window.addEventListener('lazyunveilread', globalInstall, true);
}
}(window, function(window, document, lazySizes) {
/*jshint eqnull:true */
'use strict';
var bgLoad, regBgUrlEscape;
var uniqueUrls = {};
if(document.addEventListener){
regBgUrlEscape = /\(|\)|\s|'/;
bgLoad = function (url, cb){
var img = document.createElement('img');
img.onload = function(){
img.onload = null;
img.onerror = null;
img = null;
cb();
};
img.onerror = img.onload;
img.src = url;
if(img && img.complete && img.onload){
img.onload();
}
};
addEventListener('lazybeforeunveil', function(e){
if(e.detail.instance != lazySizes){return;}
var tmp, load, bg, poster;
if(!e.defaultPrevented) {
var target = e.target;
if(target.preload == 'none'){
target.preload = target.getAttribute('data-preload') || 'auto';
}
if (target.getAttribute('data-autoplay') != null) {
if (target.getAttribute('data-expand') && !target.autoplay) {
try {
target.play();
} catch (er) {}
} else {
requestAnimationFrame(function () {
target.setAttribute('data-expand', '-10');
lazySizes.aC(target, lazySizes.cfg.lazyClass);
});
}
}
tmp = target.getAttribute('data-link');
if(tmp){
addStyleScript(tmp, true);
}
// handle data-script
tmp = target.getAttribute('data-script');
if(tmp){
e.detail.firesLoad = true;
load = function(){
e.detail.firesLoad = false;
lazySizes.fire(target, '_lazyloaded', {}, true, true);
};
addStyleScript(tmp, null, load);
}
// handle data-require
tmp = target.getAttribute('data-require');
if(tmp){
if(lazySizes.cfg.requireJs){
lazySizes.cfg.requireJs([tmp]);
} else {
addStyleScript(tmp);
}
}
// handle data-bg
bg = target.getAttribute('data-bg');
if (bg) {
e.detail.firesLoad = true;
load = function(){
target.style.backgroundImage = 'url(' + (regBgUrlEscape.test(bg) ? JSON.stringify(bg) : bg ) + ')';
e.detail.firesLoad = false;
lazySizes.fire(target, '_lazyloaded', {}, true, true);
};
bgLoad(bg, load);
}
// handle data-poster
poster = target.getAttribute('data-poster');
if(poster){
e.detail.firesLoad = true;
load = function(){
target.poster = poster;
e.detail.firesLoad = false;
lazySizes.fire(target, '_lazyloaded', {}, true, true);
};
bgLoad(poster, load);
}
}
}, false);
}
function addStyleScript(src, style, cb){
if(uniqueUrls[src]){
return;
}
var elem = document.createElement(style ? 'link' : 'script');
var insertElem = document.getElementsByTagName('script')[0];
if(style){
elem.rel = 'stylesheet';
elem.href = src;
} else {
elem.onload = function(){
elem.onerror = null;
elem.onload = null;
cb();
};
elem.onerror = elem.onload;
elem.src = src;
}
uniqueUrls[src] = true;
uniqueUrls[elem.src || elem.href] = true;
insertElem.parentNode.insertBefore(elem, insertElem);
}
}));