forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.progressive.js
32 lines (29 loc) · 862 Bytes
/
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
/*
This lazysizes plugin optimizes perceived performance by adding better support for rendering progressive JPGs/PNGs in conjunction with the LQIP pattern.
*/
(function(document){
/*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){
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);
}
})(document);