1class lazyload { 2 static #instance; 3 imgs = new Set(); 4 options = null; 5 defaults = { 6 selector: 'img[data-src]', 7 updateEvent: 'load orientationchange resize scroll touchmove focus', 8 forceEvent: 'lazyloadall' 9 }; 10 11 static init() { 12 if (!lazyload.#instance) { 13 lazyload.#instance = new lazyload(); 14 } 15 } 16 17 constructor(options) { 18 const _this = this; 19 20 this.options = Object.assign({}, this.defaults, options); 21 22 this.options.updateEvent.split(" ").forEach(function (eventType) { 23 window.addEventListener(eventType, function () { 24 _this.updateEventHandler(); 25 }, {passive: true}); 26 }); 27 28 window.addEventListener('load', function () { 29 _this.refresh(); 30 _this.checkImgs(false); 31 }); 32 33 window.addEventListener(this.options.forceEvent, function () { 34 _this.refresh(); 35 _this.checkImgs(true); 36 }); 37 } 38 39 refresh() { 40 const imgs = document.querySelectorAll(this.options.selector); 41 for (const img of imgs) { 42 this.imgs.add(img); 43 } 44 } 45 46 updateEventHandler() { 47 this.checkImgs(false); 48 } 49 50 checkImgs(force) { 51 for (const img of this.imgs) { 52 if (force || (!img.classList.contains('lazy-loaded') && this.isInViewport(img))) { 53 img.classList.add('lazy-loaded'); 54 55 let src = img.dataset.src; 56 const h = img.getBoundingClientRect().height; 57 const w = img.getBoundingClientRect().width; 58 if (w == 0 || h == 0) { 59 src = src + "?lazy=1"; 60 } else { 61 // TODO: is this needed 62// var scale = 1; 63// if (typeof (window.devicePixelRatio) !== 'undefined') { 64// scale = window.devicePixelRatio; 65// if (img.parents('.retina').length !== 0) { 66// h = h * scale; 67// w = w * scale; 68// } 69// } 70 71 src = src + "?lazy=1&w=" + w + "&h=" + h; 72 } 73 74 img.src = src; 75 } 76 } 77 } 78 79 isInViewport(element) { 80 // this is super lazy ;) we only look at the vertical dimension. 81 const rect = element.getBoundingClientRect(); 82 const viewportHeight = (window.innerHeight || document.documentElement.clientHeight); 83 return (rect.bottom > 0 && rect.top < viewportHeight); 84 } 85 86}
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.