| 1 |
/*! |
| 2 |
* Lazy Load - JavaScript plugin for lazy loading images |
| 3 |
* |
| 4 |
* Copyright (c) 2007-2019 Mika Tuupola |
| 5 |
* |
| 6 |
* Licensed under the MIT license: |
| 7 |
* http://www.opensource.org/licenses/mit-license.php |
| 8 |
* |
| 9 |
* Project home: |
| 10 |
* https://appelsiini.net/projects/lazyload |
| 11 |
* |
| 12 |
* Version: 2.0.0-rc.2 |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
(function (root, factory) { |
| 17 |
if (typeof exports === "object") { |
| 18 |
module.exports = factory(root); |
| 19 |
} else if (typeof define === "function" && define.amd) { |
| 20 |
define([], factory); |
| 21 |
} else { |
| 22 |
root.LazyLoad = factory(root); |
| 23 |
} |
| 24 |
}) (typeof global !== "undefined" ? global : this.window || this.global, function (root) { |
| 25 |
|
| 26 |
"use strict"; |
| 27 |
|
| 28 |
if (typeof define === "function" && define.amd){ |
| 29 |
root = window; |
| 30 |
} |
| 31 |
|
| 32 |
const defaults = { |
| 33 |
src: "data-src", |
| 34 |
srcset: "data-srcset", |
| 35 |
selector: ".lazyload", |
| 36 |
root: null, |
| 37 |
rootMargin: "0px", |
| 38 |
threshold: 0 |
| 39 |
}; |
| 40 |
|
| 41 |
/** |
| 42 |
* Merge two or more objects. Returns a new object. |
| 43 |
* @private |
| 44 |
* @param {Boolean} deep If true, do a deep (or recursive) merge [optional] |
| 45 |
* @param {Object} objects The objects to merge together |
| 46 |
* @returns {Object} Merged values of defaults and options |
| 47 |
*/ |
| 48 |
const extend = function () { |
| 49 |
|
| 50 |
let extended = {}; |
| 51 |
let deep = false; |
| 52 |
let i = 0; |
| 53 |
let length = arguments.length; |
| 54 |
|
| 55 |
/* Check if a deep merge */ |
| 56 |
if (Object.prototype.toString.call(arguments[0]) === "[object Boolean]") { |
| 57 |
deep = arguments[0]; |
| 58 |
i++; |
| 59 |
} |
| 60 |
|
| 61 |
/* Merge the object into the extended object */ |
| 62 |
let merge = function (obj) { |
| 63 |
for (let prop in obj) { |
| 64 |
if (Object.prototype.hasOwnProperty.call(obj, prop)) { |
| 65 |
/* If deep merge and property is an object, merge properties */ |
| 66 |
if (deep && Object.prototype.toString.call(obj[prop]) === "[object Object]") { |
| 67 |
extended[prop] = extend(true, extended[prop], obj[prop]); |
| 68 |
} else { |
| 69 |
extended[prop] = obj[prop]; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
/* Loop through each object and conduct a merge */ |
| 76 |
for (; i < length; i++) { |
| 77 |
let obj = arguments[i]; |
| 78 |
merge(obj); |
| 79 |
} |
| 80 |
|
| 81 |
return extended; |
| 82 |
}; |
| 83 |
|
| 84 |
function LazyLoad(images, options) { |
| 85 |
this.settings = extend(defaults, options || {}); |
| 86 |
this.images = images || document.querySelectorAll(this.settings.selector); |
| 87 |
this.observer = null; |
| 88 |
this.init(); |
| 89 |
} |
| 90 |
|
| 91 |
LazyLoad.prototype = { |
| 92 |
init: function() { |
| 93 |
|
| 94 |
/* Without observers load everything and bail out early. */ |
| 95 |
if (!root.IntersectionObserver) { |
| 96 |
this.loadImages(); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
let self = this; |
| 101 |
let observerConfig = { |
| 102 |
root: this.settings.root, |
| 103 |
rootMargin: this.settings.rootMargin, |
| 104 |
threshold: [this.settings.threshold] |
| 105 |
}; |
| 106 |
|
| 107 |
this.observer = new IntersectionObserver(function(entries) { |
| 108 |
Array.prototype.forEach.call(entries, function (entry) { |
| 109 |
if (entry.isIntersecting) { |
| 110 |
self.observer.unobserve(entry.target); |
| 111 |
let src = entry.target.getAttribute(self.settings.src); |
| 112 |
let srcset = entry.target.getAttribute(self.settings.srcset); |
| 113 |
if ("img" === entry.target.tagName.toLowerCase()) { |
| 114 |
if (src) { |
| 115 |
entry.target.src = src; |
| 116 |
} |
| 117 |
if (srcset) { |
| 118 |
entry.target.srcset = srcset; |
| 119 |
} |
| 120 |
} else { |
| 121 |
entry.target.style.backgroundImage = "url(" + src + ")"; |
| 122 |
} |
| 123 |
} |
| 124 |
}); |
| 125 |
}, observerConfig); |
| 126 |
|
| 127 |
Array.prototype.forEach.call(this.images, function (image) { |
| 128 |
self.observer.observe(image); |
| 129 |
}); |
| 130 |
}, |
| 131 |
|
| 132 |
loadAndDestroy: function () { |
| 133 |
if (!this.settings) { return; } |
| 134 |
this.loadImages(); |
| 135 |
this.destroy(); |
| 136 |
}, |
| 137 |
|
| 138 |
loadImages: function () { |
| 139 |
if (!this.settings) { return; } |
| 140 |
|
| 141 |
let self = this; |
| 142 |
Array.prototype.forEach.call(this.images, function (image) { |
| 143 |
let src = image.getAttribute(self.settings.src); |
| 144 |
let srcset = image.getAttribute(self.settings.srcset); |
| 145 |
if ("img" === image.tagName.toLowerCase()) { |
| 146 |
if (src) { |
| 147 |
image.src = src; |
| 148 |
} |
| 149 |
if (srcset) { |
| 150 |
image.srcset = srcset; |
| 151 |
} |
| 152 |
} else { |
| 153 |
image.style.backgroundImage = "url('" + src + "')"; |
| 154 |
} |
| 155 |
}); |
| 156 |
}, |
| 157 |
|
| 158 |
destroy: function () { |
| 159 |
if (!this.settings) { return; } |
| 160 |
this.observer.disconnect(); |
| 161 |
this.settings = null; |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
root.lazyload = function(images, options) { |
| 166 |
return new LazyLoad(images, options); |
| 167 |
}; |
| 168 |
|
| 169 |
if (root.jQuery) { |
| 170 |
const $ = root.jQuery; |
| 171 |
$.fn.lazyload = function (options) { |
| 172 |
options = options || {}; |
| 173 |
options.attribute = options.attribute || "data-src"; |
| 174 |
new LazyLoad($.makeArray(this), options); |
| 175 |
return this; |
| 176 |
}; |
| 177 |
} |
| 178 |
|
| 179 |
return LazyLoad; |
| 180 |
}); |
| 181 |
|