You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@
17
17
It is written with an aim to lazy load images, iframes, ads, videos or any other element using the recently added [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) and [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) with tremendous performance benefits.
18
18
19
19
## Featured in:
20
+
20
21
-[Web | Google Developers](https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/)
@@ -34,7 +36,6 @@ It is written with an aim to lazy load images, iframes, ads, videos or any other
34
36
35
37
and many more...
36
38
37
-
38
39
## Table of Contents
39
40
40
41
-[Demo](https://apoorv.pro/lozad.js/demo/)
@@ -49,6 +50,7 @@ and many more...
49
50
-[License](#license)
50
51
51
52
## Yet another Lazy Loading JavaScript library, why?
53
+
52
54
Existing lazy loading libraries hook up to the scroll event or use a periodic timer and call `getBoundingClientRect()` on elements that need to be lazy loaded. This approach, however, is painfully slow as each call to `getBoundingClientRect()` forces the browser to re-layout the entire page and will introduce considerable jank to your website.
53
55
54
56
Making this more efficient and performant is what [IntersectionObserver](https://developers.google.com/web/updates/2016/04/intersectionobserver) is designed for, and it’s landed in Chrome 51. IntersectionObservers let you know when an observed element enters or exits the browser’s viewport.
@@ -94,7 +96,7 @@ When loading from CDN, you can find the library on `window.lozad`.
94
96
95
97
In HTML, add an identifier to the element (default selector identified is `lozad` class):
96
98
```html
97
-
<imgclass="lozad"data-src="image.png" />
99
+
<imgclass="lozad"data-src="image.png">
98
100
```
99
101
100
102
All you need to do now is just instantiate Lozad as follows:
@@ -112,7 +114,7 @@ or with custom options:
112
114
```js
113
115
constobserver=lozad('.lozad', {
114
116
rootMargin:'10px 0px', // syntax similar to that of CSS Margin
115
-
threshold:0.1// ratio of element convergence
117
+
threshold:0.1,// ratio of element convergence
116
118
enableAutoReload:true// it will reload the new image when validating attributes changes
117
119
});
118
120
observer.observe();
@@ -160,7 +162,7 @@ for use with responsive images
0 commit comments