forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.custommedia.js
73 lines (62 loc) · 2.12 KB
/
ls.custommedia.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
/*
html:after {
display: none;
content: '--small: (max-width: 500px) | --medium: (max-width: 1100px) | --large: (min-width: 1100px)';
}
*/
(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 docElem = document.documentElement;
lazySizes.getCustomMedias = (function(){
var regCleanPseudos = /['"]/g;
var regSplit = /\s*\|\s*/g;
var regNamedQueries = /^([a-z0-9_-]+)\s*:\s*(.+)$/i;
var getStyle = function(elem, pseudo){
return ((getComputedStyle(elem, pseudo) || {getPropertyValue: function(){}}).getPropertyValue('content') || 'none').replace(regCleanPseudos, '').trim();
};
var parse = function(string, object){
string.split(regSplit).forEach(function(query){
if(query.match(regNamedQueries)){
object[RegExp.$1] = RegExp.$2;
}
});
};
return function(object, element){
object = object || lazySizes.cfg.customMedia;
element = element || document.querySelector('html');
parse(getStyle(element, ':before'), object);
parse(getStyle(element, ':after'), object);
return object;
};
})();
lazySizes.updateCustomMedia = function(){
var i, len, customMedia;
var elems = docElem.querySelectorAll('source[media][data-media][srcset]');
lazySizes.getCustomMedias();
for(i = 0, len = elems.length; i < len; i++){
if( (customMedia = lazySizes.cfg.customMedia[elems[i].getAttribute('data-media') || elems[i].getAttribute('media')]) ){
elems[i].setAttribute('media', customMedia);
}
}
elems = docElem.querySelector('source[media][data-media][srcset] ~ img');
for(i = 0, len = elems.length; i < len; i++){
lazySizes.uP(elems[i]);
}
lazySizes.autoSizer.checkElems();
};
lazySizes.getCustomMedias();
}));