forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.progressive.js
49 lines (43 loc) · 1.33 KB
/
ls.progressive.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
/*
This lazysizes plugin optimizes perceived performance by adding better support for rendering progressive JPGs/PNGs in conjunction with the LQIP pattern.
*/
(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(window.lazySizes) {
globalInstall();
} else {
window.addEventListener('lazyunveilread', globalInstall, true);
}
}(window, function(window, document, lazySizes) {
/*jshint eqnull:true */
'use strict';
var regImg, onLoad;
if('srcset' in document.createElement('img')){
regImg = /^img$/i;
onLoad = function(e){
e.target.style.backgroundSize = '';
e.target.style.backgroundImage = '';
e.target.removeEventListener(e.type, onLoad);
};
document.addEventListener('lazybeforeunveil', function(e){
if(e.detail.instance != lazySizes){return;}
var img = e.target;
if(!regImg.test(img.nodeName)){
return;
}
var src = img.getAttribute('src');
if(src) {
img.style.backgroundSize = '100% 100%';
img.style.backgroundImage = 'url(' + src + ')';
img.removeAttribute('src');
img.addEventListener('load', onLoad);
}
}, false);
}
}));