-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponsive-background-images.js
273 lines (239 loc) · 8.46 KB
/
responsive-background-images.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
*
* Responsive Background Images
*
* By Alberto Silva
* https://github.com/asilgag
*
* Based on https://github.com/webatvantage/Responsive-Background-Images
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org>
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory(root));
} else if (typeof exports === 'object') {
module.exports = factory(root);
} else {
root.ResponsiveBackgroundImages = factory(root);
}
})(this, function(root) {
'use strict';
//
// Variables
//
var ResponsiveBackgroundImages = {}; // Object for public APIs
var settings = {
selector: 'background-image-srcset',
interval: 250
};
var loadedUrls = []; // Already loaded images
//
// Methods
//
/**
* A simple forEach() implementation for Arrays, Objects and NodeLists
* @private
* @param {Array|Object|NodeList} collection - Collection of items to iterate
* @param {Function} callback - Callback function for each iteration
* @param {Array|Object|NodeList} scope - Object/NodeList/Array that forEach is iterating over (aka `this`)
*/
var forEach = function(collection, callback, scope) {
if (Object.prototype.toString.call(collection) === '[object Object]') {
for (var prop in collection) {
if (Object.prototype.hasOwnProperty.call(collection, prop)) {
callback.call(scope, collection[prop], prop, collection);
}
}
} else {
for (var i = 0, len = collection.length; i < len; i++) {
callback.call(scope, collection[i], i, collection);
}
}
};
/**
* Merge settings with user options
* @private
* @param {Object} settings - Default settings
* @param {Object} options - User options
* @returns {Object} Merged values of defaults and options
*/
var extend = function(settings, options) {
var extended = {};
forEach(settings, function(value, prop) {
extended[prop] = settings[prop];
});
forEach(options, function(value, prop) {
extended[prop] = options[prop];
});
return extended;
};
/**
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing.
* @private
* @param {Function} func - The function that will be called after it stops being called for N milliseconds
* @param {Number} wait - Interval in milliseconds
* @param {Boolean} immediate - trigger the function on the leading edge, instead of the trailing
* @returns {Function}
* @usage - http://underscorejs.org/#debounce
*/
var debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var now = new Date().getTime();
var last = now - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = new Date().getTime();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
//
// Public APIs
//
/**
* Sets background image based on breakpoint
* @public
*/
ResponsiveBackgroundImages.run = function(options) {
settings = extend(settings, options || {});
// Define local variables
var itemsWithBackground = document.querySelectorAll('[data-' + settings.selector + ']');
var el;
var elSrcSetRawData;
var elWidth;
// Loop through all target elements
for (var i = 0, len = itemsWithBackground.length; i < len; i++) {
// Set current variables
var elSrcSet = [];
el = itemsWithBackground[i];
elSrcSetRawData = el.getAttribute('data-' + settings.selector);
elWidth = el.offsetWidth * window.devicePixelRatio || 1;
// Parse srcset definitions into an array
elSrcSet = ResponsiveBackgroundImages.parseSrcSet(elSrcSetRawData);
if (elSrcSet.length > 0) {
for (var x = 0, xLen = elSrcSet.length; x < xLen; x++) {
// Find images bigger than element width, or fallback to the biggest one
if (elSrcSet[x].width >= elWidth || x === xLen - 1) {
// Lookup for an already loaded bigger image, unless it is forced
var alreadyLoadedBiggerImage = null;
if (!elSrcSet[x].force) {
alreadyLoadedBiggerImage = ResponsiveBackgroundImages.findAlreadyLoadedBiggerImage(
elSrcSet,
elWidth
);
}
var srcToLoad = (alreadyLoadedBiggerImage) ? alreadyLoadedBiggerImage : elSrcSet[x].src;
el.style.backgroundImage = "url('" + srcToLoad + "')";
if (loadedUrls.indexOf(srcToLoad) === -1) {
loadedUrls.push(srcToLoad);
}
break;
}
}
}
}
};
/**
* Parse srcSet data and returns an ordered array
* @private
* @param {String} srcSetRawData - The content of the data attribute in settings.selector
* @returns {Array}
*/
ResponsiveBackgroundImages.parseSrcSet = function(srcSetRawData) {
var srcSet = [];
if (srcSetRawData !== null && srcSetRawData.length > 0) {
var srcSetLines = srcSetRawData.split(',');
for (var j = 0, jLen = srcSetLines.length; j < jLen; j++) {
var srcSetLineParts = srcSetLines[j].match(/(\S+)\s+(\d+)w(\s+force)?/);
srcSet.push({
'src': srcSetLineParts[1],
'width': srcSetLineParts[2],
'force': !!srcSetLineParts[3]
});
}
// Sort by width
srcSet.sort(function(a, b) {
return a.width - b.width;
});
}
return srcSet;
};
/**
* Find an already loaded image for this srcSet
* @private
* @param {Array} srcSet - All the available images for this srcSet, in an array
* @param {Number} minWidth - Min width to look for
* @returns {String|Null}
*/
ResponsiveBackgroundImages.findAlreadyLoadedBiggerImage = function(srcSet, minWidth) {
// srcSet is ordered by width.
// Reverse it to get bigger images first
var srcSetReverse = srcSet.slice(0).reverse();
for (var i = 0, len = srcSetReverse.length; i < len; i++) {
// Take width into account to return a match,
// but also return it if it's the biggest image in set (i === 0)
if (loadedUrls.indexOf(srcSetReverse[i].src) !== -1 &&
(srcSetReverse[i].width >= minWidth || i === 0)) {
return srcSetReverse[i].src;
}
}
return null;
};
/**
* Add resize event
* @public
* @returns {String}
*/
ResponsiveBackgroundImages.addResizeEvent = function() {
window.addEventListener('resize', debounce(ResponsiveBackgroundImages.run , settings.interval));
};
// Auto initialize
ResponsiveBackgroundImages.run();
ResponsiveBackgroundImages.addResizeEvent();
// Return the object
return ResponsiveBackgroundImages;
});