LazyLoad is a lightweight (2.4 kB) and flexible script that **speeds up your web application** by deferring the loading of your below-the-fold images, animated SVGs, videos and iframes to **when they will 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 supports [responsive images](https://alistapart.com/article/responsive-images-in-practice), it optimizes your website for slower connections, and can enable native lazy loading. See [all features](#-all-features-compared) for more. [![vanilla-lazyload (latest)](https://img.shields.io/npm/v/vanilla-lazyload/latest.svg)](https://www.npmjs.com/package/vanilla-lazyload) [![vanilla-lazyload (downloads)](https://img.shields.io/npm/dy/vanilla-lazyload.svg)](https://www.npmjs.com/package/vanilla-lazyload) [![](https://data.jsdelivr.com/v1/package/npm/vanilla-lazyload/badge)](https://www.jsdelivr.com/package/npm/vanilla-lazyload) ➡️ Jump to: [👨‍💻 Getting started - HTML](#-getting-started---html) - [👩‍💻 Getting started - Script](#-getting-started---script) - [🥧 Recipes](#-recipes) - [📺 Demos](#-demos) - [😋 Tips & tricks](#-tips--tricks) - [🔌 API](#-api) - [😯 All features compared](#-all-features-compared) --- **Love this project? 😍 [Buy me a coffee!](https://ko-fi.com/verlok)** --- ## 👨‍💻 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 ⚠ **IMPORTANT NOTE**: To display content images on your pages, always use the `img` tag. This would benefit the SEO and the accessibility of your website. To understand if your images are content or background, ask yourself: "would my website user like to see those images when printing out the page?". If the answer is "yes", then your images are content images and you should avoid using background images to display them. #### Single background image: ```html
``` #### Single background, with HiDPI screen support: ```html
``` #### Multiple backgrounds: ```html
...
``` #### Multiple backgrounds, HiDPI screen support: ```html
...
``` #### Backgrounds with `image-set`: ```html
...
``` #### Multiple backgrounds with `image-set`: ```html
...
``` ### Lazy animated SVG ```html ``` ### Lazy video ```html ``` Please note that the video poster can be lazily loaded too. ### Lazy iframe ```html ``` --- **Love this project? 😍 [Buy me a coffee!](https://ko-fi.com/verlok)** --- ## 👩‍💻 Getting started - Script The latest, recommended version of LazyLoad is **17.8.3**. Quickly understand how to upgrade from a previous version reading the [practical upgrade guide](UPGRADE.md). ### The simple, easiest way The easiest way to use LazyLoad is to include the script from a CDN: ```html ``` Then, in your javascript code: ```js var lazyLoadInstance = new LazyLoad({ // Your custom settings go here }); ``` 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 lazyLoadInstance.update(); ``` ### 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 lazyLoadInstance.update(); ``` Note about Internet Explorer: because this technique uses a `CustomEvent` to trigger the `LazyLoad::Initialized` event, you might want to add [this polyfill](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill) to make it work on Internet Explorer. [DEMO](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/async.html) - [SOURCE](https://github.com/verlok/vanilla-lazyload/blob/master/demos/async.html) ← for a single LazyLoad instance [DEMO](https://www.andreaverlicchi.eu/vanilla-lazyload/demos/async_multiple.html) - [SOURCE](https://github.com/verlok/vanilla-lazyload/blob/master/demos/async_multiple.html) ← for multiple LazyLoad instances ### 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/vanilla-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 `