-
Notifications
You must be signed in to change notification settings - Fork 5
/
imagesloader.es6
178 lines (152 loc) · 5.89 KB
/
imagesloader.es6
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
// @ts-check
/**
* lazy image loader
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/
LIB.ready(function (undef) {
// intersection-observer.min.js
if (!("srcset" in new Image())) {
// try {
document.body.insertAdjacentHTML(
"beforeend",
"<svg xmlns=http://www.w3.org/2000/svg width=1 height=1>" +
"<defs>" +
"<filter id=blur-lqip width=100% height=100% >" +
"<feGaussianBlur stdDeviation=20 />" +
"</filter>" +
"</defs>" +
"</svg>"
);
}
function lazyload() {
LIB.images.lazy(".image-placeholder").on({
/**
*
* @param {HTMLImageElement} img
* @param {HTMLImageElement} oldImage
*/
preload(img, oldImage) {
const legacy = !("currentSrc" in img);
if (!legacy) {
oldImage.insertAdjacentHTML(
"beforebegin",
'<span class=image-placeholder-wrapper><span class=image-placeholder-opacity><span class=image-placeholder-element style=background-image:url(' +
(img.currentSrc || img.src) +
')>'
);
} else {
oldImage.insertAdjacentHTML(
"beforebegin",
'<span class=image-placeholder-wrapper><span class=image-placeholder-svg><svg width=100% height=100% version=1.1 xmlns=http://www.w3.org/2000/svg >' +
'<image xlink:href="' +
(img.currentSrc || img.src) +
'" width=100% height=100% filter=url(#blur-lqip) x=0 y=0 />'
);
}
const container = oldImage.previousElementSibling;
if (legacy) {
if (typeof window.CustomEvent != "function") {
function CustomEvent(event, params) {
params = params || {
bubbles: false,
cancelable: false,
detail: undefined
};
const evt = document.createEvent("CustomEvent");
evt.initCustomEvent(
event,
params.bubbles,
params.cancelable,
params.detail
);
return evt;
}
CustomEvent.prototype = window.Event.prototype;
}
const svg = container.querySelector("svg");
// const svgImage = container.querySelector('svg image');
function resize() {
const height = this.height;
const width = this.width;
svg.setAttribute("height", height);
svg.setAttribute("width", width);
}
img.addEventListener("sourcechange", resize);
img.addEventListener("load", resize);
}
oldImage.classList.remove(
"image-placeholder",
"image-placeholder-lqip",
"image-placeholder-svg"
);
container.insertBefore(oldImage, container.firstElementChild);
},
load,
error(error, img, oldImage) {
load(img, oldImage);
}
});
}
function load(img, oldImage) {
if (oldImage.dataset.src) {
oldImage.src = oldImage.dataset.src;
}
if (oldImage.dataset.srcset) {
oldImage.srcset = oldImage.dataset.srcset;
}
setTimeout(function () {
let container = oldImage;
oldImage.removeAttribute("data-srcset");
oldImage.removeAttribute("data-src");
oldImage.classList.add('image-placeholder-complete');
while (
container instanceof HTMLElement &&
!container.classList.contains("image-placeholder-wrapper")
) {
container = container.parentElement;
}
if (container) {
container.parentElement.insertBefore(oldImage, container);
}
setTimeout(function () {
oldImage.classList.remove('image-placeholder-complete');
if (container) {
container.parentElement.removeChild(container);
}
}, 5);
}, 5);
}
if (
!(
"IntersectionObserver" in window &&
"IntersectionObserverEntry" in window &&
"intersectionRatio" in IntersectionObserverEntry.prototype
)
) {
const script = document.createElement("script");
/*script.onreadystatechange =*/
script.onload = lazyload;
script.defer = true;
script.async = true;
script.src = "{script-src}";
document.body.appendChild(script);
} else {
if (!("isIntersecting" in IntersectionObserverEntry.prototype)) {
Object.defineProperty(
IntersectionObserverEntry.prototype,
"isIntersecting", {
get: function () {
return this.intersectionRatio > 0;
}
}
);
}
lazyload();
}
});