| 1 |
(function (root, factory) { |
| 2 |
if (typeof exports === "object") { |
| 3 |
module.exports = factory(root); |
| 4 |
} else if (typeof define === "function" && define.amd) { |
| 5 |
define([], factory); |
| 6 |
} else { |
| 7 |
root.LazyLoad = factory(root); |
| 8 |
} |
| 9 |
})(typeof global !== "undefined" ? global : this.window || this.global, function (root) { |
| 10 |
"use strict"; |
| 11 |
if (typeof define === "function" && define.amd) { |
| 12 |
root = window; |
| 13 |
} |
| 14 |
const defaults = { |
| 15 |
src: "data-src", |
| 16 |
srcset: "data-srcset", |
| 17 |
selector: ".lazyload", |
| 18 |
root: null, |
| 19 |
rootMargin: "0px", |
| 20 |
threshold: 0 |
| 21 |
}; |
| 22 |
const extend = function () { |
| 23 |
let extended = {}; |
| 24 |
let deep = false; |
| 25 |
let i = 0; |
| 26 |
let length = arguments.length; |
| 27 |
if (Object.prototype.toString.call(arguments[0]) === "[object Boolean]") { |
| 28 |
deep = arguments[0]; |
| 29 |
i++; |
| 30 |
} |
| 31 |
let merge = function (obj) { |
| 32 |
for (let prop in obj) { |
| 33 |
if (Object.prototype.hasOwnProperty.call(obj, prop)) { |
| 34 |
/* If deep merge and property is an object, merge properties */ |
| 35 |
if (deep && Object.prototype.toString.call(obj[prop]) === "[object Object]") { |
| 36 |
extended[prop] = extend(true, extended[prop], obj[prop]); |
| 37 |
} else { |
| 38 |
extended[prop] = obj[prop]; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
}; |
| 43 |
for (; i < length; i++) { |
| 44 |
let obj = arguments[i]; |
| 45 |
merge(obj); |
| 46 |
} |
| 47 |
return extended; |
| 48 |
}; |
| 49 |
function LazyLoad(images, options) { |
| 50 |
this.settings = extend(defaults, options || {}); |
| 51 |
this.images = images || document.querySelectorAll(this.settings.selector); |
| 52 |
this.observer = null; |
| 53 |
this.init(); |
| 54 |
} |
| 55 |
LazyLoad.prototype = { |
| 56 |
init: function () { |
| 57 |
if (!root.IntersectionObserver) { |
| 58 |
this.loadImages(); |
| 59 |
return; |
| 60 |
} |
| 61 |
let self = this; |
| 62 |
let observerConfig = { |
| 63 |
root: this.settings.root, |
| 64 |
rootMargin: this.settings.rootMargin, |
| 65 |
threshold: [this.settings.threshold] |
| 66 |
}; |
| 67 |
this.observer = new IntersectionObserver(function (entries) { |
| 68 |
Array.prototype.forEach.call(entries, function (entry) { |
| 69 |
if (entry.isIntersecting) { |
| 70 |
self.observer.unobserve(entry.target); |
| 71 |
let src = entry.target.getAttribute(self.settings.src); |
| 72 |
let srcset = entry.target.getAttribute(self.settings.srcset); |
| 73 |
if ("img" === entry.target.tagName.toLowerCase()) { |
| 74 |
if (src) { |
| 75 |
entry.target.src = src; |
| 76 |
} |
| 77 |
if (srcset) { |
| 78 |
entry.target.srcset = srcset; |
| 79 |
} |
| 80 |
} else { |
| 81 |
entry.target.style.backgroundImage = "url(" + src + ")"; |
| 82 |
} |
| 83 |
} |
| 84 |
}); |
| 85 |
}, observerConfig); |
| 86 |
Array.prototype.forEach.call(this.images, function (image) { |
| 87 |
self.observer.observe(image); |
| 88 |
}); |
| 89 |
}, |
| 90 |
loadAndDestroy: function () { |
| 91 |
if (!this.settings) { |
| 92 |
return; |
| 93 |
} |
| 94 |
this.loadImages(); |
| 95 |
this.destroy(); |
| 96 |
}, |
| 97 |
loadImages: function () { |
| 98 |
if (!this.settings) { |
| 99 |
return; |
| 100 |
} |
| 101 |
let self = this; |
| 102 |
Array.prototype.forEach.call(this.images, function (image) { |
| 103 |
let src = image.getAttribute(self.settings.src); |
| 104 |
let srcset = image.getAttribute(self.settings.srcset); |
| 105 |
if ("img" === image.tagName.toLowerCase()) { |
| 106 |
if (src) { |
| 107 |
image.src = src; |
| 108 |
} |
| 109 |
if (srcset) { |
| 110 |
image.srcset = srcset; |
| 111 |
} |
| 112 |
} else { |
| 113 |
image.style.backgroundImage = "url('" + src + "')"; |
| 114 |
} |
| 115 |
}); |
| 116 |
}, |
| 117 |
destroy: function () { |
| 118 |
if (!this.settings) { |
| 119 |
return; |
| 120 |
} |
| 121 |
this.observer.disconnect(); |
| 122 |
this.settings = null; |
| 123 |
} |
| 124 |
}; |
| 125 |
root.lazyload = function (images, options) { |
| 126 |
return new LazyLoad(images, options); |
| 127 |
}; |
| 128 |
if (root.jQuery) { |
| 129 |
const $ = root.jQuery; |
| 130 |
$.fn.lazyload = function (options) { |
| 131 |
options = options || {}; |
| 132 |
options.attribute = options.attribute || "data-src"; |
| 133 |
new LazyLoad($.makeArray(this), options); |
| 134 |
return this; |
| 135 |
}; |
| 136 |
} |
| 137 |
return LazyLoad; |
| 138 |
}); |
| 139 |
|