# lazyload **Repository Path**: Project0ne/lazyload ## Basic Information - **Project Name**: lazyload - **Description**: LazyLoad is a fast, lightweight and flexible script that speeds up your web application by loading your content images, videos and iframes only as they enter the viewport. It's written in plain "vanilla" JavaScript, it leverages the IntersectionObserver API, it works with responsive images and it supports native lazy loading. - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-08 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README LazyLoad is a fast, lightweight and flexible script that **speeds up your web application** by loading your content images, videos and iframes only **as they enter the viewport**. It's written in plain "vanilla" JavaScript, it leverages the [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) API, it works with [responsive images](https://alistapart.com/article/responsive-images-in-practice) and it supports native lazy loading. See [notable features](#-notable-features) for more. ➡️ Jump to: [👨‍💻 Getting started - HTML](#-getting-started---html) - [👩‍💻 Getting started - Script](#-getting-started---script) - [🥧 Recipes](#-recipes) - [📺 Demos](#-demos) - [😋 Tips & tricks](#-tips--tricks) - [🔌 API](#-api) - [😯 Notable features](#-notable-features) --- ## 👨‍💻 Getting started - HTML In order to make your content be loaded by LazyLoad, you must use some `data-` attributes instead of the actual attributes. Examples below. #### Lazy image: ```html A lazy image ``` #### Lazy image with low quality placeholder: ```html A lazy image ``` #### Lazy responsive image with `srcset` and `sizes`: ```html A lazy image ``` To have a low quality placeholder, add the `src` attribute pointing to a very small version of the image. E.g. `src="lazy_10.jpg"`. #### Lazy responsive image with hi-dpi support using the `picture` tag: ```html A lazy image ``` To have a low quality placeholder, add the `src` attribute pointing to a very small version of the image to the `img` tag. E.g. `src="lazy_10.jpg"`. #### Lazy responsive image with automatic _WebP_ format selection, using the `picture` tag: ```html A lazy image ``` To have a low quality placeholder, add the `src` attribute pointing to a very small version of the image to the `img` tag. E.g. `src="lazy_10.jpg"`. #### Lazy background image Single background ```html
``` Multiple backgrounds ```html
...
``` Notes: - you need to use `url()` in the value of your `data-bg` attribute, also for single background - you shouldn't use background images to load content images, they're bad for SEO and for accessibility - on background images, `callback_loaded` won't be called and the `class_loaded` class won't be added #### Lazy video ```html ``` #### Lazy iframe ```html ``` ## 👩‍💻 Getting started - Script The latest, recommended version of LazyLoad is **12.0.0**. ### To polyfill or not to polyfill IntersectionObserver? On browser NOT supporting IntersectionObserver such as Internet explorer and older versions of Safari **you can choose whether or not to add a javascript polyfill** for it. If you **don't use a polyfill**, LazyLoad will **load all the images** as soon as it's downloaded and executed. The number of impacted users would be [relatively small](https://caniuse.com/intersectionobserver), so this is a completely acceptable choice. If you prefer to load a polyfill, the regular LazyLoad behaviour is granted. ### The simple, easiest way The easiest way to use LazyLoad is to include the script from a CDN: ```html ``` Or, with the IntersectionObserver polyfill: ```html ``` Then, in your javascript code: ```js var lazyLoadInstance = new LazyLoad({ elements_selector: ".lazy" // ... more custom settings? }); ``` To be sure that DOM for your lazy content is ready when you instantiate LazyLoad, **place the script tag right before the closing `` tag**. If more DOM arrives later, e.g. via an AJAX call, you'll need to call `lazyLoadInstance.update();` to make LazyLoad check the DOM again. ```js if (lazyLoadInstance) { lazyLoadInstance.update(); } ``` ### Include via RequireJS You can use [RequireJS](https://requirejs.org) to dynamically and asynchronously load modules in your website. You can also find the original W3C'S [IntersectionObserver Polyfill packed in AMD](https://github.com/verlok/IntersectionObserverAMD) so you can `require` it conditionally, along with LazyLoad. Include RequireJS: ```html ``` Then `require` the AMD version of LazyLoad, like this: ```js var lazyLoadAmdUrl = "https://cdn.jsdelivr.net/npm/vanilla-lazyload@12.0.0/dist/lazyload.amd.min.js"; var polyfillAmdUrl = "https://cdn.jsdelivr.net/npm/intersection-observer-amd@2.0.1/intersection-observer-amd.js"; /// Dynamically define the dependencies var dependencies = [ "IntersectionObserver" in window ? null // <- Doesn't require the polyfill : polyfillAmdUrl, lazyLoadAmdUrl ]; // Initialize LazyLoad inside the callback require(dependencies, function(_, LazyLoad) { var lazyLoadInstance = new LazyLoad({ elements_selector: ".lazy" // ... more custom settings? }); } ``` [DEMO](https://verlok.github.io/lazyload/demos/amd_polyfill.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/amd_polyfill.html) ### Using an `async` script If you prefer, it's possible to include LazyLoad's script using `async` script and initialize it as soon as it's loaded. To do so, **you must define the options before including the script**. You can pass: - `{}` an object to get a single instance of LazyLoad - `[{}, {}]` an array of objects to get multiple instances of LazyLoad, each one with different options. ```html ``` Then include the script. ```html ``` **Possibly place the script tag right before the closing `` tag**. If you can't do that, LazyLoad could be executed before the browser has loaded all the DOM, and you'll need to call its `update()` method to make it check the DOM again. ### Using an `async` script + getting the instance reference Same as above, but you must put the `addEventListener` code shown below before including the `async` script. ```html ``` Then include the script. ```html ``` Now you'll be able to call its methods, like: ```js if (lazyLoadInstance) { lazyLoadInstance.update(); } ``` Note about Internet Explorer: because this technique uses a `CustomEvent` ([learn more](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent)) to trigger the `LazyLoad::Initialized` event, you might want to add this micro polyfill to make it work on Internet Explorer. ```html ``` ### Local install If you prefer to install LazyLoad locally in your project, you can! #### Using npm ``` npm install vanilla-lazyload ``` #### Using bower ``` bower install vanilla-lazyload ``` #### Manual download Download one the latest [releases](https://github.com/verlok/lazyload/releases/). The files you need are inside the `dist` folder. If you don't know which one to pick, use `lazyload.min.js`, or read [about bundles](#bundles). ### Local usage Should you install LazyLoad locally, you can import it as ES module like the following: ```js import LazyLoad from "vanilla-lazyload"; ``` It's also possible (but unadvised) to use the `require` commonJS syntax. More information about bundling LazyLoad with WebPack are available on [this specific repo](https://github.com/verlok/lazyload-es2015-webpack-test). ### Usage with React Take a look at this example of [usage of React with LazyLoad](https://codesandbox.io/s/20306yk96p) on Sandbox. This implementation takes the same props that you would normally pass to the `img` tag, but it renders a lazy image. Feel free to fork and improve it! ### Bundles Inside the `dist` folder you will find different bundles. | Filename | Module Type | Advantages | | ---------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | `lazyload.min.js` | UMD (Universal Module Definition) | Works pretty much everywhere, even in common-js contexts | | `lazyload.iife.min.js` | IIFE (Immediately Invoked Function Expression) | Works as in-page `