> For the complete documentation index, see [llms.txt](https://jsoverflow.gitbook.io/books/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jsoverflow.gitbook.io/books/lazy-loading-images-and-video.md).

# Lazy Loading Images and Video

## **What is lazy loading ?**

Lazy loading is technique that defers loading of non-critical resources at page load time. Instead, these non-critical resources are loaded at the moment of need.<br>

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExj3GNPbI9LlRmBt25%2F1.PNG?alt=media\&token=d0da3023-8210-4a35-8676-e67e7ad1fd9d)

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExjVvkVtouOk2ndOG6%2F2.PNG?alt=media\&token=5e381d40-1d23-4fb2-877e-a660f9845a66)

## **Why lazy load images, video instead of just loading them?**

1. Because it’s possible you’re loading stuff the user may never see:

   * It wastes data. Be a waste of their money.
   * It wastes processing time, battery, and other system resources.

2. When we lazy load images and video, we reduce initial page load time, initial page weight, and system resource usage, all of which have positive impacts on performance.

## **Lazy loading images.**

### Inline Images

* The most common lazy loading candidates are images as used in \<img> elements.
* When we lazy load \<img> elements, we use JavaScript to check if they're in the viewport.
* If they are, their src (and sometimes srcset) attributes are populated with URLs to the desired image content.

#### **Using intersection observer:**

* If you've written lazy loading code before, you may have accomplished your task by using event handlers such as scroll or resize.
* Modern browsers offer a more performant and efficient way to do the work of checking element visibility via [the intersection observer API](https://developers.google.com/web/updates/2016/04/intersectionobserver).
* Intersection observer is easier to user and read but it’s not [universal](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Browser_compatibility).

#### Example

![Using intersection observer](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExl2DBqWb-L3N2nHTP%2F3.PNG?alt=media\&token=8a9a8461-6d63-4eb5-82c0-6eb8f50bc196)

![Using intersection observer](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExmg3NHvNI2e_dPI5W%2F4.PNG?alt=media\&token=08fce0cb-4c54-40eb-af87-5a6d88090114)

#### **Using event handlers (the most compatible way):**

![Using event handlers](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExnCTyxqaUlxEFcubJ%2F5.PNG?alt=media\&token=44abb464-5bee-4b6f-b615-a8724d4b77f8)

Best way to use lazy loading with Inline images is: **Intersection + event handlers**.

### Image in CSS

* The url is declared in css: background-image.
* When resource in screen we’ll add image to contents by add css class.

![CSS](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExonmplXaPbsmWlP1U%2F6.PNG?alt=media\&token=09102dea-1403-4cb0-8972-f56f396b74b9)

![HTML](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExpA8FYBqM3HzWYGdt%2F7.PNG?alt=media\&token=a3fd75a8-f741-40b1-9455-5adb45d5c55f)

![Javascript](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LExflCgLQb21D0lbJIg%2F-LExpIKmuYFvjnyZ5Tcd%2F8.PNG?alt=media\&token=b0ee5279-6329-49ca-a05c-87407ac5ca8c)

## **Lazy Loading Video**

### Does not autoplay

1.For videos where playback is initiated by the user (i.e., videos that don't autoplay).  Use [preload attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-preload).

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LF1A6PV9B2c4ANiVj16%2F-LF1AEwLH-4Wm46vW7ne%2F1.PNG?alt=media\&token=4ea385c8-07de-4ac7-ac11-245035c6469f)

2\. Use poster attribute which lets you specify a placeholder.

### Like animated GIF

**For video acting as an animated GIF replacement.**

1. They play automatically when loaded.
2. They loop continuously.
3. They don't have an audio track.

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LF1A6PV9B2c4ANiVj16%2F-LF1Ak_XwZmEYIb345Vr%2F2.PNG?alt=media\&token=5610695c-b5d3-40a9-8e84-f73b41768184)

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LF1A6PV9B2c4ANiVj16%2F-LF1COveMvU9bVobxr38%2F3.PNG?alt=media\&token=e2a3a258-a684-4c7d-9154-3ce8490cbe46)

## **Lazy Loading libraries**

* [Lazysizes](https://github.com/aFarkas/lazysizes)
* [Lozad.js](https://github.com/ApoorvSaxena/lozad.js)
* [Blazy](https://github.com/dinbror/blazy)
* [Yall.js](https://github.com/malchata/yall.js)
* [React-lazyload](https://github.com/jasonslyvia/react-lazyload) (only react)

## **What can go wrong ?**

* So much benefits but not easy.
* If you get it wrong, there could be unintended consequences.
* I will show you some note.

**Some note:**

* Layout shifting and placeholders.
* Image decoding delays.
* When stuff doesn't load.
* JavaScript availability.
* [Mind the fold.](https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/#mind_the_fold)

**Layout shifting and placeholders:**

What happen if shifting in the layout if placeholders aren't used ?

* Trigger expensive DOM layout operations that consume system resources.

For **\<img>** tags, **src** should initially **point** to a **placeholder** until that attribute is updated with the final image URL.\
The **poster** attribute in a **\<video>** element to **point** to a **placeholder image**.\
Additionally, use **width** and **height** attributes on both **\<img>** and **\<video>** tags.

**Image decoding delays:**

* Loading large images in JavaScript and dropping them into the DOM can tie up the main thread. Block screen.\
  \=> Asynchronously decoding images using the decode method.

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LF1A6PV9B2c4ANiVj16%2F-LF1CyUOKEkZhsrtcpO8%2F4.png?alt=media\&token=7fe57be5-b5d0-473e-939e-bbf5799e979a)

**When stuff doesn't load :**

Sometimes media resources will fail to load for one reason or another and errors occur.

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LF1A6PV9B2c4ANiVj16%2F-LF1DH0cEJqmfjji_m6P%2F5.png?alt=media\&token=b7c8e085-ec14-48db-a8bc-d653fcaec107)

**JavaScript availability :**

It should not be assumed that JavaScript is always available.

![](https://1620588615-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEx__frh5rhJHikGydq%2F-LF1A6PV9B2c4ANiVj16%2F-LF1Dk3hLbw03MDu7rho%2F6.png?alt=media\&token=7320b6fd-1891-4010-932f-7abba4042acd)

{% content-ref url="/pages/-LExifgzUdKQAQZiewih" %}
[Lazy Loading Images and Video](/books/lazy-loading-images-and-video.md)
{% endcontent-ref %}

Source: [Google Developer](https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/#mind_the_fold)
