vanilla-lazyload
Version:
A fast, lightweight script to load images as they enter the viewport. SEO friendly, it supports responsive images (both srcset + sizes and picture) and progressive JPEG
33 lines (31 loc) • 968 B
JavaScript
/* Creates instance and notifies it through the window element */
const createInstance = function(classObj, options) {
var event;
let eventString = "LazyLoad::Initialized";
let instance = new classObj(options);
try {
// Works in modern browsers
event = new CustomEvent(eventString, { detail: { instance } });
} catch (err) {
// Works in Internet Explorer (all versions)
event = document.createEvent("CustomEvent");
event.initCustomEvent(eventString, false, false, { instance });
}
window.dispatchEvent(event);
};
/* Auto initialization of one or more instances of lazyload, depending on the
options passed in (plain object or an array) */
export default function(classObj, options) {
if (!options) {
return;
}
if (!options.length) {
// Plain object
createInstance(classObj, options);
} else {
// Array of objects
for (let i = 0, optionsItem; (optionsItem = options[i]); i += 1) {
createInstance(classObj, optionsItem);
}
}
}