Lazyload image test
by Sreeraj Saseendran
HTML
<div class="asd lazy-container">
<img class="lazy" src="https://www.jpl.nasa.gov/spaceimages/images/largesize/PIA23689_hires.jpg" data-src="https://www.jpl.nasa.gov/spaceimages/images/largesize/PIA23689_hires.jpg" alt="Alternative text to describe image.">
</div>
CSS
.asd {
max-width:300px;
transition:all 0.5s;
}
.lazy-container {
min-height:120px;
background:#F1F1F1;
}
img {
width:100%;
}
.lazy {
background: red;
height:100%;
wdth:100%;
}
JavaScript
'use strict';
function yall (options) {
options = options || {};
// Options
const lazyClass = options.lazyClass || "lazy";
const lazyBackgroundClass = options.lazyBackgroundClass || "lazy-bg";
const idleLoadTimeout = "idleLoadTimeout" in options ? options.idleLoadTimeout : 200;
const observeChanges = options.observeChanges || false;
const events = options.events || {};
const noPolyfill = options.noPolyfill || false;
// Shorthands (saves more than a few bytes!)
const win = window;
const ric = "requestIdleCallback";
const io = "IntersectionObserver";
const ioSupport = io in win && `${io}Entry` in win;
// App stuff
const crawler = /baidu|(?:google|bing|yandex|duckduck)bot/i.test(navigator.userAgent);
const dataAttrs = ["srcset", "src", "poster"];
const arr = [];
const queryDOM = (selector, root) => arr.slice.call((root || document).querySelectorAll(selector || `img.${lazyClass},video.${lazyClass},iframe.${lazyClass},.${lazyBackgroundClass}`));
// This function handles lazy loading of elements.
const yallLoad = element => {
const parentNode = element.parentNode;
if (parentNode.nodeName == "PICTURE") {
yallApplyFn(queryDOM("source", parentNode), yallFlipDataAttrs);
}
if (element.nodeName == "VIDEO") {
yallApplyFn(queryDOM("source", element), yallFlipDataAttrs);
}
yallFlipDataAttrs(element);
const classList = element.classList;
// Lazy load CSS background images
if (classList.contains(lazyBackgroundClass)) {
classList.remove(lazyBackgroundClass);
classList.add(options.lazyBackgroundLoaded || "lazy-bg-loaded");
}
};
const yallBindEvents = element => {
for (let eventIndex in events) {
element.addEventListener(eventIndex, events[eventIndex].listener || events[eventIndex], events[eventIndex].options || undefined);
}
};
// Added because there was a number of patterns like this peppered throughout
// the code. This flips necessary data-...