Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 2030cdd

Browse files
committed
blog post almost ready 🚀
1 parent 555c8fe commit 2030cdd

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

_drafts/2019-05-10-intersection-observer.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,42 @@ const lazyLoadImages = (selector, loadCompleted) => {
4747
}
4848
```
4949

50-
As you can see above from the snippet above, in the inserction callback I'm calling the `onIntersection` function. What does it do? ...
50+
As you can see above from the snippet above, in the inserction callback I'm calling the `onIntersection` function. What does it do? This function check the `IntersectionObserverEntry` received from the Intersection Observer as parameter: if a `target` `Element` is inside the viewport it would have the `intersectionRatio` > 0. When this happen I can remove the observer and start the load of the image with the `loadImage` function.
5151

52-
This is the final script.
52+
```javascript
53+
const onIntersection = (entries, observer, loadCompleted) => {
54+
entries.forEach(entry => {
55+
if (entry.intersectionRatio > 0) {
56+
observer.unobserve(entry.target)
57+
loadImage(entry.target, loadCompleted)
58+
}
59+
})
60+
}
61+
```
62+
63+
The `loadImage` function downloads the image using the [Image](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image) object. At the end of the download I remove the `lazy` css class, that I used to hide the image until it has been download. Then the `loadCompleted` function is called, where the caller can do anything it want with the image (for example I'm doing a custom animation in order to avoid the `flash` effect when the image is show).
64+
65+
```javascript
66+
const loadImage = (image, loadCompleted) => {
67+
const src = image.dataset.src
68+
fetchImage(src).then(() => {
69+
image.src = src
70+
removeCssClass(image, 'lazy')
71+
loadCompleted(image)
72+
})
73+
}
74+
75+
const fetchImage = (src) => {
76+
return new Promise((resolve, reject) => {
77+
const image = new Image()
78+
image.src = src
79+
image.onload = resolve
80+
image.onerror = reject
81+
})
82+
}
83+
```
84+
85+
This is the final script with the complete flow.
5386

5487
```javascript
5588
import 'intersection-observer'
@@ -93,9 +126,13 @@ const fetchImage = (src) => {
93126
export { lazyLoadImages }
94127
```
95128

96-
There's still one thing
129+
There's still one thing that I didn't discuss yet. How can we support this type of lazy loading for the browser that doesn't still have implemented the `IntersectionObserver` API? The answer is the [Interserction Observer Polyfill](https://github.com/w3c/IntersectionObserver/tree/master/polyfill). I installed as a dependency of my project.
130+
131+
```
132+
npm install --save intersection-observer
133+
```
97134

98-
[Interserction Observer Polyfill](https://github.com/w3c/IntersectionObserver/tree/master/polyfill)
135+
This [polyfill](https://en.wikipedia.org/wiki/Polyfill_(programming) "polyfill programming") exposes an ad hoc implementation in vanilla javascript of the Intersection Observer, or it exposes the current browser implementation if it is already in place for the browser that the user is using.
99136

100137
#### Conclusion
101138

0 commit comments

Comments
 (0)