|
1 | 1 | # Responsively Lazy |
2 | 2 |
|
3 | | -**The best lazy loading implementation available.** |
| 3 | +**Probably the best lazy loading implementation available.** |
4 | 4 |
|
5 | | -- Handles responsive images |
| 5 | +- Perfect for responsive images |
6 | 6 | - Truly lazy (absolutely no unnecessary requests) |
7 | 7 | - **SEO friendly** (valid HTML) |
8 | | -- Supports [WebP](https://en.wikipedia.org/wiki/WebP) |
| 8 | +- Supports [WebP](https://en.wikipedia.org/wiki/WebP) and [AVIF](https://en.wikipedia.org/wiki/AVIF) |
| 9 | +- Customizable load threshold |
| 10 | +- Lazy load HTML |
9 | 11 |
|
10 | 12 | <p align="center"> |
11 | | -<img src="http://ivopetkov.github.io/responsivelyLazy/poster.jpg" style="max-width:100%;"> |
| 13 | +<img src="https://ivopetkov.github.io/responsively-lazy-v3/poster.jpg" style="max-width:100%;"> |
12 | 14 | </p> |
13 | 15 |
|
14 | | -You can find a demo at [http://ivopetkov.github.io/responsivelyLazy/](http://ivopetkov.github.io/responsivelyLazy/) and learn how the magic works at [http://ivopetkov.com/b/lazy-load-responsive-images/](http://ivopetkov.com/b/lazy-load-responsive-images/) |
| 16 | +You can find a demo at [ivopetkov.github.io/responsively-lazy-v3/](https://ivopetkov.github.io/responsively-lazy-v3/) and learn how the magic works at [ivopetkov.com/responsively-lazy/](https://ivopetkov.com/responsively-lazy/) |
15 | 17 |
|
16 | 18 | ## Download and install |
17 | 19 |
|
18 | | -Download the minified [css](https://raw.githubusercontent.com/ivopetkov/responsively-lazy/master/responsivelyLazy.min.css) and [js](https://raw.githubusercontent.com/ivopetkov/responsively-lazy/master/responsivelyLazy.min.js) files or install through [npm](https://www.npmjs.com/) and [bower](http://bower.io/) |
| 20 | +Download the minified [JS file](https://raw.githubusercontent.com/ivopetkov/responsively-lazy/master/responsively-lazy.min.js) or install via [npm](https://www.npmjs.com/). |
19 | 21 | ``` |
20 | 22 | npm install responsively-lazy |
21 | 23 | ``` |
| 24 | + |
| 25 | +The library does not have any dependencies, and it's just 2.5kb when gzipped and minified. |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +* Include the JS file in the head of the document |
| 30 | +```html |
| 31 | +<script async src="responsively-lazy.min.js"></script> |
22 | 32 | ``` |
23 | | -bower install responsively-lazy |
| 33 | + |
| 34 | +* Add the following code for each image |
| 35 | +```html |
| 36 | +<img src="images/2500.jpg" style="width:100%;aspect-ratio:400/274;" srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-responsively-lazy="images/400.jpg 400w, images/600.jpg 600w, images/800.jpg 800w, images/1000.jpg 1000w" /> |
24 | 37 | ``` |
| 38 | +Values to customize: |
25 | 39 |
|
26 | | -The library does not have any dependencies, and it's just 1.1kb gzipped and minified. |
| 40 | +**aspect-ratio** |
27 | 41 |
|
28 | | -## Usage |
| 42 | +The aspect ratio of the image (width/height) to reserve space. Skip if you don't know it. |
| 43 | + |
| 44 | +**src** |
| 45 | + |
| 46 | +The default (largest) image size. Will be used by very old browsers, search engines, social networks, etc. |
| 47 | + |
| 48 | +**data-responsively-lazy** (the star of the show) |
| 49 | + |
| 50 | +Must contain a list of image versions separated by commas. Each version must contain a path to the image and its size. Optionaly you can specify the file type (webp or avif). The first supported format for the selected width is used. |
29 | 51 |
|
30 | | -* Include the css file in the head tag |
31 | 52 | ```html |
32 | | -<link rel="stylesheet" href="responsivelyLazy.min.css"> |
| 53 | +data-responsively-lazy="images/400.avif 400w avif, images/400.webp 400w webp, images/400.jpg 400w, ..." |
33 | 54 | ``` |
34 | 55 |
|
35 | | -* Include the js file right before the end of the body tag |
| 56 | +**data-responsively-lazy-threshold** |
| 57 | + |
| 58 | +Specify how close to the viewport an image should be to start loading. |
36 | 59 | ```html |
37 | | -<script async src="responsivelyLazy.min.js"></script> |
| 60 | +data-responsively-lazy-threshold="500px" // can use percents too |
38 | 61 | ``` |
39 | 62 |
|
40 | | -* Add the following code for each image |
| 63 | +**data-responsively-lazy-type=html** |
| 64 | + |
| 65 | +Lazy load HTML. Can contain JavaScript. |
| 66 | + |
41 | 67 | ```html |
42 | | -<div class="responsively-lazy" style="padding-bottom:68.44%;"> |
43 | | - <img alt="" src="images/2500.jpg" data-srcset="images/400.jpg 400w, images/400.webp 400w, images/600.jpg 600w, images/1000.jpg 1000w" srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" /> |
44 | | -</div> |
| 68 | +<div data-responsively-lazy-type="html" data-responsively-lazy="hello world"></div> |
45 | 69 | ``` |
46 | | -The things to customize are the **padding-bottom** style, and the values of the **src** and **data-srcset** attributes. If you don't know the image aspect ratio you can skip the div tag and move the responsively-lazy class to te img tag: |
| 70 | + |
| 71 | +**data-responsively-lazy-type=background** |
| 72 | + |
| 73 | +Lazy load image as a background image. |
| 74 | + |
47 | 75 | ```html |
48 | | -<img class="responsively-lazy" alt="" src="images/2500.jpg" data-srcset="images/400.jpg 400w, images/400.webp 400w, images/600.jpg 600w, images/1000.jpg 1000w" srcset="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" /> |
| 76 | +<div data-responsively-lazy-type="background" data-responsively-lazy="images/400.jpg 400w, images/600.jpg 600w, images/800.jpg 800w, images/1000.jpg 1000w"></div> |
49 | 77 | ``` |
50 | | -You can list image versions in the [WebP format](https://en.wikipedia.org/wiki/WebP) which will be used if the browser supports it. |
51 | 78 |
|
52 | 79 | ## A new concept |
53 | 80 |
|
54 | | -Responsively Lazy is very different from the other lazy loading libraries. They make you break your HTML by removing the `src` attribute, or make you put tiny version there or make you use `<noscript>` to make your images appear in Google Images. The following code has worked for ages: |
| 81 | +Responsively Lazy is very different from the other lazy loading libraries. Others make you break your HTML by removing the `src` attribute, or make you put some tiny version there, or make you use `<noscript>` to make your images appear in search engines (like Google Images). The following code has worked for ages: |
55 | 82 | ```html |
56 | 83 | <img src="image.jpg" /> |
57 | 84 | ``` |
58 | 85 | Let's not break it when we can enhance it. |
59 | 86 | ```html |
60 | | -<img src="image.jpg" data-src="image-200.jpg 200w, image-400.jpg 400w" srcset="..." /> |
| 87 | +<img src="image.jpg" data-responsively-lazy="image-200.jpg 200w, image-400.jpg 400w" srcset="..." /> |
61 | 88 | ``` |
62 | 89 |
|
63 | 90 | ## Browser support |
64 | 91 |
|
65 | | -The lazy loading works in browsers supporting the srcset attribute. As of December 2017 that's [86.78%](http://caniuse.com/#feat=srcset). Unsupported browsers will load the image in the src attribute. That's the image search engines and social networks will find, so it's better to make it high resolution. |
| 92 | +The lazy loading works in browsers supporting the srcset attribute and JavaScript. As of December 2023 that's [97.6%](https://caniuse.com/#feat=srcset). Unsupported browsers will load the image in the src attribute. That's the image search engines and social networks will find, so it's better to make it high resolution. |
66 | 93 |
|
67 | 94 | ## DOM changes |
68 | 95 |
|
69 | 96 | The library will listen for DOM changes and you can also trigger visible images loading by calling `responsivelyLazy.run()`. |
70 | 97 |
|
71 | 98 | ## License |
72 | | -Free to use under the [MIT license](http://opensource.org/licenses/MIT). |
| 99 | +Free to use under the [MIT license](https://opensource.org/licenses/MIT). |
73 | 100 |
|
74 | 101 | ## Got questions? |
75 | | -You can find me at [@IvoPetkovCom](https://twitter.com/IvoPetkovCom) and [ivopetkov.com](http://ivopetkov.com) |
| 102 | +You can find me at [@IvoPetkovCom](https://twitter.com/IvoPetkovCom) and [ivopetkov.com](https://ivopetkov.com) |
0 commit comments