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 if (srcset) { 122 var back_url = '' 123 for (var i=0;i<srcset.split(',').length;i++) { 124 back_url += ((i>0)?',':'') + "url(" + srcset.split(',')[i] + ")" 125 } 126 entry.target.style.backgroundImage = back_url 127 entry.target.classList.add("fade"); 128 } else 129 if (src) { 130 entry.target.style.backgroundImage = "url(" + src + ")"; 131 entry.target.classList.add("fade"); 132 } 133 } 134 //console.log(entry.target); 135 } 136 }); 137 }, observerConfig); 138 139 Array.prototype.forEach.call(this.images, function (image) { 140 self.observer.observe(image); 141 }); 142 }, 143 144 loadAndDestroy: function () { 145 if (!this.settings) { return; } 146 this.loadImages(); 147 this.destroy(); 148 }, 149 150 loadImages: function () { 151 if (!this.settings) { return; } 152 153 let self = this; 154 Array.prototype.forEach.call(this.images, function (image) { 155 let src = image.getAttribute(self.settings.src); 156 let srcset = image.getAttribute(self.settings.srcset); 157 if ("img" === image.tagName.toLowerCase()) { 158 if (src) { 159 image.src = src; 160 } 161 if (srcset) { 162 image.srcset = srcset; 163 } 164 } else { 165 if (srcset) { 166 var back_url = '' 167 for (var i=0;i<srcset.split(',').length;i++) { 168 back_url += ((i>0)?',':'') + "url(" + srcset.split(',')[i] + ")" 169 } 170 image.style.backgroundImage = back_url 171 } else 172 if (src) { 173 image.style.backgroundImage = "url(" + src + ")"; 174 } 175 } 176 //console.log(entry.target); 177 }); 178 }, 179 180 destroy: function () { 181 if (!this.settings) { return; } 182 this.observer.disconnect(); 183 this.settings = null; 184 } 185 }; 186 187 root.lazyload = function(images, options) {
188 return new LazyLoad(images, options); 189 }; 190 191 if (root.jQuery) { 192 const $ = root.jQuery; 193 $.fn.lazyload = function (options) { 194 options = options || {}; 195 options.attribute = options.attribute || "data-src"; 196 new LazyLoad($.makeArray(this), options); 197 return this; 198 }; 199 } 200 201 return LazyLoad; 202});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.