forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.object-fit.js
149 lines (118 loc) · 4.2 KB
/
ls.object-fit.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
(function(window, factory) {
var globalInstall = function(initialEvent){
factory(window.lazySizes, initialEvent);
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, initialEvent) {
'use strict';
var style = document.createElement('a').style;
var fitSupport = 'objectFit' in style;
var positionSupport = fitSupport && 'objectPosition' in style;
var regCssFit = /object-fit["']*\s*:\s*["']*(contain|cover)/;
var regCssPosition = /object-position["']*\s*:\s*["']*(.+?)(?=($|,|'|"|;))/;
var blankSrc = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
var regBgUrlEscape = /\(|\)|'/;
var positionDefaults = {
center: 'center',
'50% 50%': 'center',
};
function getObject(element){
var css = (getComputedStyle(element, null) || {});
var content = css.fontFamily || '';
var objectFit = content.match(regCssFit) || '';
var objectPosition = objectFit && content.match(regCssPosition) || '';
if(objectPosition){
objectPosition = objectPosition[1];
}
return {
fit: objectFit && objectFit[1] || '',
position: positionDefaults[objectPosition] || objectPosition || 'center',
};
}
function initFix(element, config){
var switchClassesAdded, addedSrc;
var lazysizesCfg = lazySizes.cfg;
var styleElement = element.cloneNode(false);
var styleElementStyle = styleElement.style;
var onChange = function(){
var src = element.currentSrc || element.src;
if(src && addedSrc !== src){
addedSrc = src;
styleElementStyle.backgroundImage = 'url(' + (regBgUrlEscape.test(src) ? JSON.stringify(src) : src ) + ')';
if(!switchClassesAdded){
switchClassesAdded = true;
lazySizes.rC(styleElement, lazysizesCfg.loadingClass);
lazySizes.aC(styleElement, lazysizesCfg.loadedClass);
}
}
};
var rafedOnChange = function(){
lazySizes.rAF(onChange);
};
element._lazysizesParentFit = config.fit;
element.addEventListener('lazyloaded', rafedOnChange, true);
element.addEventListener('load', rafedOnChange, true);
styleElement.addEventListener('load', function(){
var curSrc = styleElement.currentSrc || styleElement.src;
if(curSrc && curSrc != blankSrc){
styleElement.src = blankSrc;
styleElement.srcset = '';
}
});
lazySizes.rAF(function(){
var hideElement = element;
var container = element.parentNode;
if(container.nodeName.toUpperCase() == 'PICTURE'){
hideElement = container;
container = container.parentNode;
}
lazySizes.rC(styleElement, lazysizesCfg.loadedClass);
lazySizes.rC(styleElement, lazysizesCfg.lazyClass);
lazySizes.aC(styleElement, lazysizesCfg.loadingClass);
lazySizes.aC(styleElement, lazysizesCfg.objectFitClass || 'lazysizes-display-clone');
if(styleElement.getAttribute(lazysizesCfg.srcsetAttr)){
styleElement.setAttribute(lazysizesCfg.srcsetAttr, '');
}
if(styleElement.getAttribute(lazysizesCfg.srcAttr)){
styleElement.setAttribute(lazysizesCfg.srcAttr, '');
}
styleElement.src = blankSrc;
styleElement.srcset = '';
styleElementStyle.backgroundRepeat = 'no-repeat';
styleElementStyle.backgroundPosition = config.position;
styleElementStyle.backgroundSize = config.fit;
hideElement.style.display = 'none';
element.setAttribute('data-parent-fit', config.fit);
element.setAttribute('data-parent-container', 'prev');
container.insertBefore(styleElement, hideElement);
if(element._lazysizesParentFit){
delete element._lazysizesParentFit;
}
if(element.complete){
onChange();
}
});
}
if(!fitSupport || !positionSupport){
var onRead = function(e){
if(e.detail.instance != lazySizes){return;}
var element = e.target;
var obj = getObject(element);
if(obj.fit && (!fitSupport || (obj.position != 'center'))){
initFix(element, obj);
}
};
window.addEventListener('lazyunveilread', onRead, true);
if(initialEvent && initialEvent.detail){
onRead(initialEvent);
}
}
}));