๐Ÿ“ฆ leonardomso / 33-js-concepts

๐Ÿ“„ intersection-observer.mdx ยท 1042 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042---
title: "Intersection Observer in JavaScript"
sidebarTitle: "Intersection Observer"
description: "Learn the Intersection Observer API in JavaScript. Detect element visibility, implement lazy loading, infinite scroll, and scroll animations efficiently without scroll events."
---

How do you know when an element scrolls into view? How can you lazy-load images only when they're about to be seen? How do infinite-scroll feeds know when to load more content? And how can you trigger animations at just the right moment as users scroll through your page?

```javascript
// Lazy load images when they come into view
const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const img = entry.target;
      img.src = img.dataset.src;
      observer.unobserve(img);
    }
  });
});

document.querySelectorAll('img[data-src]').forEach(img => observer.observe(img));
```

The **[Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)** provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or the viewport. It's the modern, performant solution for detecting element visibility, replacing expensive scroll event listeners with browser-optimized callbacks.

<Info>
**What you'll learn in this guide:**
- What Intersection Observer is and why it's better than scroll events
- How to create and configure observers with options
- Understanding thresholds, root, and rootMargin
- Implementing lazy loading for images and content
- Building infinite scroll functionality
- Creating scroll-triggered animations
- Common mistakes and best practices
</Info>

---

## What is Intersection Observer in JavaScript?

The **[Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)** is a browser API that lets you detect when an element enters, exits, or crosses a certain visibility threshold within a viewport or container element. Instead of constantly checking element positions during scroll events, the browser efficiently notifies your code only when visibility actually changes. **In short: Intersection Observer tells you when elements become visible or hidden, without the performance cost of scroll listeners.**

---

## Why Not Just Use Scroll Events?

Before Intersection Observer, developers used scroll event listeners with `getBoundingClientRect()` to detect element visibility:

```javascript
// The OLD way: scroll events (DON'T do this!)
window.addEventListener('scroll', () => {
  const elements = document.querySelectorAll('.lazy-image');
  elements.forEach(el => {
    const rect = el.getBoundingClientRect();
    if (rect.top < window.innerHeight && rect.bottom > 0) {
      // Element is visible - load it
      el.src = el.dataset.src;
    }
  });
});
```

**Problems with this approach:**

| Issue | Why It's Bad |
|-------|--------------|
| **Main thread blocking** | Scroll fires 60+ times per second, blocking other JavaScript |
| **Layout thrashing** | `getBoundingClientRect()` forces browser to recalculate layout |
| **Battery drain** | Constant calculations drain mobile device batteries |
| **No throttling built-in** | You must manually debounce/throttle |
| **iframe limitations** | Can't detect visibility in cross-origin iframes |

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              SCROLL EVENTS vs INTERSECTION OBSERVER                          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                              โ”‚
โ”‚  SCROLL EVENTS (Old Way)              INTERSECTION OBSERVER (Modern Way)    โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€            โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€     โ”‚
โ”‚                                                                              โ”‚
โ”‚  User scrolls                         User scrolls                          โ”‚
โ”‚       โ”‚                                    โ”‚                                โ”‚
โ”‚       โ–ผ                                    โ–ผ                                โ”‚
โ”‚  scroll event fires                   Browser calculates                    โ”‚
โ”‚  (60+ times/sec)                      intersections internally              โ”‚
โ”‚       โ”‚                                    โ”‚                                โ”‚
โ”‚       โ–ผ                                    โ–ผ                                โ”‚
โ”‚  YOUR CODE runs                       Callback fires ONLY when              โ”‚
โ”‚  on EVERY scroll                      visibility ACTUALLY changes           โ”‚
โ”‚       โ”‚                                    โ”‚                                โ”‚
โ”‚       โ–ผ                                    โ–ผ                                โ”‚
โ”‚  getBoundingClientRect()              Entry object with all                 โ”‚
โ”‚  forces layout                        data pre-calculated                   โ”‚
โ”‚       โ”‚                                    โ”‚                                โ”‚
โ”‚       โ–ผ                                    โ–ผ                                โ”‚
โ”‚  ๐ŸŒ SLOW, janky scrolling             ๐Ÿš€ SMOOTH, 60fps scrolling           โ”‚
โ”‚                                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

**Intersection Observer runs off the main thread** and is optimized by the browser, making it dramatically more efficient.

---

## How to Create an Intersection Observer

Creating an observer involves two steps: instantiate the observer with a callback, then tell it what elements to observe.

### Basic Syntax

```javascript
// Step 1: Create the observer with a callback
const observer = new IntersectionObserver((entries, observer) => {
  entries.forEach(entry => {
    console.log(entry.target, 'isIntersecting:', entry.isIntersecting);
  });
});

// Step 2: Tell it what to observe
const element = document.querySelector('.my-element');
observer.observe(element);
```

The **[`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver)** constructor takes two arguments:

1. **Callback function** โ€” Called whenever observed elements cross visibility thresholds
2. **Options object** (optional) โ€” Configures when and how intersections are detected

### The Callback Function

The callback receives two parameters:

```javascript
const callback = (entries, observer) => {
  // entries: Array of IntersectionObserverEntry objects
  // observer: The IntersectionObserver instance (useful for unobserving)
  
  entries.forEach(entry => {
    // entry.target โ€” The observed element
    // entry.isIntersecting โ€” Is it currently visible?
    // entry.intersectionRatio โ€” How much is visible (0 to 1)
    // entry.boundingClientRect โ€” Element's size and position
    // entry.intersectionRect โ€” The visible portion's rectangle
    // entry.rootBounds โ€” The root element's rectangle
    // entry.time โ€” Timestamp when intersection was recorded
  });
};
```

<Note>
**Important:** The callback fires once immediately when you call `observe()` on an element, reporting its current intersection state. This is intentional, so you know the initial visibility.
</Note>

### IntersectionObserverEntry Properties

Each entry in the callback provides detailed intersection data:

| Property | Type | Description |
|----------|------|-------------|
| `target` | Element | The element being observed |
| `isIntersecting` | boolean | `true` if element is currently intersecting root |
| `intersectionRatio` | number | Percentage visible (0.0 to 1.0) |
| `boundingClientRect` | DOMRect | Target element's bounding rectangle |
| `intersectionRect` | DOMRect | The visible portion's rectangle |
| `rootBounds` | DOMRect | Root element's bounding rectangle (or viewport) |
| `time` | number | Timestamp when intersection change was recorded |

```javascript
const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    console.log('Element:', entry.target.id);
    console.log('Is visible:', entry.isIntersecting);
    console.log('Visibility %:', Math.round(entry.intersectionRatio * 100) + '%');
    console.log('Position:', entry.boundingClientRect.top, 'px from top');
  });
});
```

---

## Intersection Observer Options

The options object customizes when the callback fires:

```javascript
const options = {
  root: null,           // The viewport (or a specific container element)
  rootMargin: '0px',    // Margin around the root
  threshold: 0          // When to trigger (0 = any pixel, 1 = fully visible)
};

const observer = new IntersectionObserver(callback, options);
```

### The `root` Option

The **root** defines the container used for checking visibility. It defaults to `null` (the browser viewport).

```javascript
// Observe visibility relative to the viewport (default)
const observer1 = new IntersectionObserver(callback, { root: null });

// Observe visibility relative to a scrollable container
const scrollContainer = document.querySelector('.scroll-container');
const observer2 = new IntersectionObserver(callback, { root: scrollContainer });
```

<Warning>
When using a custom root, the observed elements **must be descendants** of that root element. Otherwise, the observer won't detect intersections.
</Warning>

### The `rootMargin` Option

The **rootMargin** grows or shrinks the root's detection area. It works like CSS margins:

```javascript
// Start detecting 100px BEFORE element enters viewport (for preloading)
const observer = new IntersectionObserver(callback, {
  rootMargin: '100px 0px'  // top/bottom: 100px, left/right: 0px
});

// Shrink the detection area (element must be 50px inside viewport)
const observer2 = new IntersectionObserver(callback, {
  rootMargin: '-50px'  // All sides shrink by 50px
});
```

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         rootMargin EXPLAINED                                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                                              โ”‚
โ”‚  rootMargin: '100px 0px 100px 0px'                                          โ”‚
โ”‚                                                                              โ”‚
โ”‚            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                          โ”‚
โ”‚            โ”‚  +100px margin (top) โ”‚  โ† Elements detected HERE               โ”‚
โ”‚            โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค                                          โ”‚
โ”‚            โ”‚                      โ”‚                                          โ”‚
โ”‚            โ”‚      VIEWPORT        โ”‚  โ† Actual visible area                  โ”‚
โ”‚            โ”‚                      โ”‚                                          โ”‚
โ”‚            โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค                                          โ”‚
โ”‚            โ”‚ +100px margin (bottom)โ”‚  โ† Elements detected HERE              โ”‚
โ”‚            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                          โ”‚
โ”‚                                                                              โ”‚
โ”‚  Use positive margins for PRELOADING (lazy load before visible)             โ”‚
โ”‚  Use negative margins for DELAYING (wait until fully in view)               โ”‚
โ”‚                                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

**Common rootMargin patterns:**

| Value | Use Case |
|-------|----------|
| `'100px'` | Preload images 100px before they enter viewport |
| `'-50px'` | Wait until element is 50px inside viewport |
| `'0px 0px -50%'` | Trigger when top half of element is visible |
| `'200px 0px 200px 0px'` | Large buffer for slow networks |

### The `threshold` Option

The **threshold** determines at what visibility percentage the callback fires. It can be a single number or an array:

```javascript
// Fire when ANY pixel becomes visible (default)
const observer1 = new IntersectionObserver(callback, { threshold: 0 });

// Fire when element is 50% visible
const observer2 = new IntersectionObserver(callback, { threshold: 0.5 });

// Fire when element is FULLY visible
const observer3 = new IntersectionObserver(callback, { threshold: 1.0 });

// Fire at multiple points (0%, 25%, 50%, 75%, 100%)
const observer4 = new IntersectionObserver(callback, { 
  threshold: [0, 0.25, 0.5, 0.75, 1.0] 
});
```

```javascript
// Practical example: Track how much of an ad is viewed
const adObserver = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    const percentVisible = Math.round(entry.intersectionRatio * 100);
    console.log(`Ad is ${percentVisible}% visible`);
    
    if (entry.intersectionRatio >= 0.5) {
      trackAdImpression(entry.target);  // Count as "viewed" when 50%+ visible
    }
  });
}, { threshold: [0, 0.25, 0.5, 0.75, 1.0] });
```

---

## Observer Methods

The **[`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver)** instance has four methods:

### observe(element)

Start observing an element:

```javascript
const element = document.querySelector('.target');
observer.observe(element);

// Observe multiple elements
document.querySelectorAll('.lazy-image').forEach(img => {
  observer.observe(img);
});
```

### unobserve(element)

Stop observing a specific element (useful after lazy loading):

```javascript
const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      loadImage(entry.target);
      observer.unobserve(entry.target);  // Stop watching after loading
    }
  });
});
```

### disconnect()

Stop observing ALL elements:

```javascript
// Stop everything
observer.disconnect();

// Common pattern: cleanup when component unmounts
class LazyLoader {
  constructor() {
    this.observer = new IntersectionObserver(this.handleIntersect);
  }
  
  destroy() {
    this.observer.disconnect();
  }
}
```

### takeRecords()

Get any pending intersection records without waiting for the callback:

```javascript
// Rarely needed, but useful for synchronous access
const pendingEntries = observer.takeRecords();
pendingEntries.forEach(entry => {
  // Process immediately
});
```

---

## Implementing Lazy Loading Images

Lazy loading is the most common use case for Intersection Observer. Here's a complete implementation:

### HTML Setup

```html
<!-- Use data-src instead of src for lazy images -->
<img class="lazy" data-src="hero-image.jpg" alt="Hero image">
<img class="lazy" data-src="product-1.jpg" alt="Product 1">
<img class="lazy" data-src="product-2.jpg" alt="Product 2">

<!-- Optional: Add a placeholder or low-quality preview -->
<img class="lazy" 
     src="placeholder.svg" 
     data-src="high-res-image.jpg" 
     alt="High resolution image">
```

### JavaScript Implementation

```javascript
// Create the lazy loading observer
const lazyImageObserver = new IntersectionObserver((entries, observer) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const img = entry.target;
      
      // Swap data-src to src
      img.src = img.dataset.src;
      
      // Optional: Handle srcset for responsive images
      if (img.dataset.srcset) {
        img.srcset = img.dataset.srcset;
      }
      
      // Remove lazy class (for CSS transitions)
      img.classList.remove('lazy');
      img.classList.add('loaded');
      
      // Stop observing this image
      observer.unobserve(img);
    }
  });
}, {
  // Start loading 100px before image enters viewport
  rootMargin: '100px 0px',
  threshold: 0
});

// Observe all lazy images
document.querySelectorAll('img.lazy').forEach(img => {
  lazyImageObserver.observe(img);
});
```

### CSS for Smooth Loading

```css
.lazy {
  opacity: 0;
  transition: opacity 0.3s ease-in;
}

.lazy.loaded {
  opacity: 1;
}

/* Optional: Blur-up effect */
.lazy {
  filter: blur(10px);
  transition: filter 0.3s ease-in, opacity 0.3s ease-in;
}

.lazy.loaded {
  filter: blur(0);
  opacity: 1;
}
```

<Tip>
**Native lazy loading:** Modern browsers support `<img loading="lazy">` which handles basic lazy loading automatically. Use Intersection Observer when you need more control (custom thresholds, animations, or loading indicators).
</Tip>

---

## Building Infinite Scroll

Infinite scroll loads more content as the user approaches the bottom of the page:

```javascript
// The sentinel element sits at the bottom of your content
// <div id="sentinel"></div>

const sentinel = document.querySelector('#sentinel');
const contentContainer = document.querySelector('#content');

let page = 1;
let isLoading = false;

const infiniteScrollObserver = new IntersectionObserver(async (entries) => {
  const entry = entries[0];
  
  if (entry.isIntersecting && !isLoading) {
    isLoading = true;
    
    try {
      // Fetch more content
      const response = await fetch(`/api/posts?page=${++page}`);
      const posts = await response.json();
      
      if (posts.length === 0) {
        // No more content - stop observing
        infiniteScrollObserver.unobserve(sentinel);
        sentinel.textContent = 'No more posts';
        return;
      }
      
      // Append new content
      posts.forEach(post => {
        const article = createPostElement(post);
        contentContainer.appendChild(article);
      });
    } catch (error) {
      console.error('Failed to load more posts:', error);
    } finally {
      isLoading = false;
    }
  }
}, {
  // Load more when sentinel is 200px from viewport
  rootMargin: '200px'
});

infiniteScrollObserver.observe(sentinel);

function createPostElement(post) {
  const article = document.createElement('article');
  article.innerHTML = `
    <h2>${post.title}</h2>
    <p>${post.excerpt}</p>
  `;
  return article;
}
```

### Infinite Scroll HTML Structure

```html
<div id="content">
  <!-- Initial posts loaded here -->
  <article>...</article>
  <article>...</article>
</div>

<!-- Sentinel must be AFTER all content -->
<div id="sentinel">Loading more...</div>
```

---

## Scroll-Triggered Animations

Trigger animations when elements scroll into view:

```javascript
const animatedElements = document.querySelectorAll('.animate-on-scroll');

const animationObserver = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      // Add animation class
      entry.target.classList.add('animated');
      
      // Optional: Only animate once
      animationObserver.unobserve(entry.target);
    }
  });
}, {
  threshold: 0.2,  // Trigger when 20% visible
  rootMargin: '0px 0px -50px 0px'  // Trigger slightly before fully in view
});

animatedElements.forEach(el => animationObserver.observe(el));
```

### CSS Animations

```css
.animate-on-scroll {
  opacity: 0;
  transform: translateY(30px);
  transition: opacity 0.6s ease-out, transform 0.6s ease-out;
}

.animate-on-scroll.animated {
  opacity: 1;
  transform: translateY(0);
}

/* Staggered animations */
.animate-on-scroll:nth-child(1) { transition-delay: 0.1s; }
.animate-on-scroll:nth-child(2) { transition-delay: 0.2s; }
.animate-on-scroll:nth-child(3) { transition-delay: 0.3s; }
```

### Reusable Animation Observer

```javascript
function createScrollAnimator(options = {}) {
  const {
    threshold = 0.2,
    rootMargin = '0px',
    animateOnce = true,
    animatedClass = 'animated'
  } = options;
  
  return new IntersectionObserver((entries, observer) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.classList.add(animatedClass);
        
        if (animateOnce) {
          observer.unobserve(entry.target);
        }
      } else if (!animateOnce) {
        entry.target.classList.remove(animatedClass);
      }
    });
  }, { threshold, rootMargin });
}

// Usage
const animator = createScrollAnimator({ threshold: 0.3, animateOnce: false });
document.querySelectorAll('[data-animate]').forEach(el => animator.observe(el));
```

---

## Sticky Header Detection

Detect when a header becomes sticky:

```javascript
// CSS: header { position: sticky; top: 0; }
const header = document.querySelector('header');

// Create a sentinel element just before the header
const sentinel = document.createElement('div');
sentinel.style.height = '1px';
header.before(sentinel);

const stickyObserver = new IntersectionObserver(([entry]) => {
  // When sentinel is NOT intersecting, header is stuck
  header.classList.toggle('is-stuck', !entry.isIntersecting);
}, {
  threshold: 0,
  rootMargin: '-1px 0px 0px 0px'  // Trigger right at the top
});

stickyObserver.observe(sentinel);
```

```css
header {
  position: sticky;
  top: 0;
  background: white;
  transition: box-shadow 0.3s ease;
}

header.is-stuck {
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
```

---

## Section-Based Navigation

Highlight navigation links based on which section is visible:

```javascript
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('nav a');

const sectionObserver = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      // Remove active from all links
      navLinks.forEach(link => link.classList.remove('active'));
      
      // Add active to corresponding link
      const activeLink = document.querySelector(`nav a[href="#${entry.target.id}"]`);
      if (activeLink) {
        activeLink.classList.add('active');
      }
    }
  });
}, {
  threshold: 0.5,  // Section is "active" when 50% visible
  rootMargin: '-20% 0px -20% 0px'  // Focus on center of viewport
});

sections.forEach(section => sectionObserver.observe(section));
```

---

## The #1 Intersection Observer Mistake: Not Cleaning Up

The most common mistake is forgetting to disconnect observers, leading to memory leaks:

```javascript
// โŒ BAD: Observer keeps running forever
function setupLazyLoading() {
  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.src = entry.target.dataset.src;
        // Forgot to unobserve!
      }
    });
  });
  
  document.querySelectorAll('.lazy').forEach(img => observer.observe(img));
}

// โœ… GOOD: Unobserve after loading
function setupLazyLoading() {
  const observer = new IntersectionObserver((entries, obs) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        entry.target.src = entry.target.dataset.src;
        obs.unobserve(entry.target);  // Stop watching after load
      }
    });
  });
  
  document.querySelectorAll('.lazy').forEach(img => observer.observe(img));
  
  // Return cleanup function for frameworks
  return () => observer.disconnect();
}
```

### Framework Cleanup Patterns

```javascript
// React
useEffect(() => {
  const observer = new IntersectionObserver(callback);
  observer.observe(elementRef.current);
  
  return () => observer.disconnect();  // Cleanup on unmount
}, []);

// Vue 3 Composition API
onMounted(() => {
  observer = new IntersectionObserver(callback);
  observer.observe(element.value);
});

onUnmounted(() => {
  observer?.disconnect();
});
```

---

## Common Mistakes

<AccordionGroup>
  <Accordion title="Mistake 1: Using scroll events for visibility detection">
    ```javascript
    // โŒ WRONG: Scroll events are expensive
    window.addEventListener('scroll', () => {
      const rect = element.getBoundingClientRect();
      if (rect.top < window.innerHeight) {
        loadContent();
      }
    });
    
    // โœ… RIGHT: Use Intersection Observer
    const observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        loadContent();
        observer.unobserve(entries[0].target);
      }
    });
    observer.observe(element);
    ```
    
    Scroll events fire constantly and block the main thread. Intersection Observer is optimized by the browser.
  </Accordion>
  
  <Accordion title="Mistake 2: Creating multiple observers for the same options">
    ```javascript
    // โŒ WRONG: Creating a new observer for each element
    images.forEach(img => {
      const observer = new IntersectionObserver(callback);
      observer.observe(img);
    });
    
    // โœ… RIGHT: One observer can watch many elements
    const observer = new IntersectionObserver(callback);
    images.forEach(img => observer.observe(img));
    ```
    
    A single observer can efficiently track many elements with the same options.
  </Accordion>
  
  <Accordion title="Mistake 3: Forgetting the callback fires immediately">
    ```javascript
    // โŒ WRONG: Assuming callback only fires on scroll
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        // This fires IMMEDIATELY for current state!
        loadImage(entry.target);
      });
    });
    
    // โœ… RIGHT: Check isIntersecting before acting
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          loadImage(entry.target);
        }
      });
    });
    ```
    
    The callback fires once immediately when you call `observe()` to report current state.
  </Accordion>
  
  <Accordion title="Mistake 4: Using threshold: 1 without accounting for partial visibility">
    ```javascript
    // โŒ WRONG: threshold: 1 may never trigger for tall elements
    const observer = new IntersectionObserver(callback, {
      threshold: 1.0  // Requires 100% visibility
    });
    
    // If element is taller than viewport, it can NEVER be 100% visible!
    
    // โœ… RIGHT: Use appropriate threshold or check intersectionRatio
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        // Use intersectionRatio for flexible visibility checking
        if (entry.intersectionRatio >= 0.8 || entry.isIntersecting) {
          handleVisibility(entry.target);
        }
      });
    }, { threshold: [0, 0.25, 0.5, 0.75, 1.0] });
    ```
  </Accordion>
  
  <Accordion title="Mistake 5: Not handling the root element requirement">
    ```javascript
    // โŒ WRONG: Observed element must be a descendant of root
    const container = document.querySelector('.sidebar');
    const observer = new IntersectionObserver(callback, { root: container });
    
    // This element is NOT inside .sidebar - won't work!
    observer.observe(document.querySelector('.main-content .item'));
    
    // โœ… RIGHT: Observe elements inside the root
    observer.observe(container.querySelector('.sidebar-item'));
    ```
  </Accordion>
</AccordionGroup>

---

## Browser Support and Polyfill

Intersection Observer has excellent browser support (available since March 2019 in all major browsers):

```javascript
// Feature detection
if ('IntersectionObserver' in window) {
  // Use Intersection Observer
  const observer = new IntersectionObserver(callback);
} else {
  // Fallback for very old browsers
  // Load polyfill or use scroll events
}
```

For legacy browser support, use the [official polyfill](https://github.com/w3c/IntersectionObserver/tree/main/polyfill):

```html
<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>
```

---

## Key Takeaways

<Info>
**The key things to remember:**

1. **Intersection Observer replaces scroll events** โ€” It's more performant and runs off the main thread

2. **The callback fires immediately** โ€” When you call `observe()`, it reports current visibility state

3. **Use `isIntersecting` to check visibility** โ€” Don't assume the callback means "now visible"

4. **One observer, many elements** โ€” A single observer can efficiently watch multiple targets

5. **Clean up with `unobserve()` or `disconnect()`** โ€” Prevent memory leaks, especially after lazy loading

6. **`rootMargin` enables preloading** โ€” Use positive margins to detect elements before they're visible

7. **`threshold` controls precision** โ€” Use arrays for fine-grained visibility tracking

8. **Always handle the null root** โ€” Defaults to viewport, but custom roots must contain observed elements

9. **Combine with CSS for smooth animations** โ€” Observer triggers classes, CSS handles transitions

10. **Consider native `loading="lazy"`** โ€” For simple image lazy loading, the native attribute may suffice
</Info>

---

## Test Your Knowledge

<AccordionGroup>
  <Accordion title="Question 1: Why is Intersection Observer better than scroll events for visibility detection?">
    **Answer:** Intersection Observer is better because:
    
    1. **Runs off the main thread** โ€” Doesn't block JavaScript execution
    2. **Browser-optimized** โ€” Efficiently batches calculations
    3. **No layout thrashing** โ€” Doesn't force `getBoundingClientRect()` recalculations
    4. **Built-in throttling** โ€” Fires only when visibility actually changes
    5. **Works with iframes** โ€” Can detect visibility in cross-origin contexts
    
    Scroll events fire 60+ times per second and require manual throttling, while Intersection Observer only fires when relevant visibility changes occur.
  </Accordion>
  
  <Accordion title="Question 2: What does rootMargin: '-50px' do?">
    **Answer:** `rootMargin: '-50px'` shrinks the detection area by 50px on all sides.
    
    This means an element must be at least 50px inside the viewport before it's considered "intersecting." It's useful for:
    
    - Triggering animations when elements are fully in view
    - Ensuring content is clearly visible before acting
    - Avoiding edge-case flickering near viewport boundaries
    
    ```javascript
    // Element must be 50px inside viewport to trigger
    const observer = new IntersectionObserver(callback, {
      rootMargin: '-50px'
    });
    ```
  </Accordion>
  
  <Accordion title="Question 3: When would you use threshold: [0, 0.25, 0.5, 0.75, 1]?">
    **Answer:** Use multiple thresholds when you need to track progressive visibility, such as:
    
    - **Ad viewability tracking** โ€” Count impressions at different visibility levels
    - **Video playback** โ€” Pause at 25% visible, play at 75% visible
    - **Progress indicators** โ€” Show how much of an article has been read
    - **Parallax effects** โ€” Adjust animations based on scroll position
    
    ```javascript
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        const percent = Math.round(entry.intersectionRatio * 100);
        updateProgressBar(percent);
      });
    }, { threshold: [0, 0.25, 0.5, 0.75, 1] });
    ```
  </Accordion>
  
  <Accordion title="Question 4: Why should you call unobserve() after lazy loading an image?">
    **Answer:** You should call `unobserve()` because:
    
    1. **Memory efficiency** โ€” The observer no longer needs to track this element
    2. **Performance** โ€” Fewer elements to check means faster intersection calculations
    3. **Prevents double-loading** โ€” Without unobserving, the image could be "loaded" multiple times
    4. **Clean architecture** โ€” Once lazy loading is complete, the observer's job is done
    
    ```javascript
    const observer = new IntersectionObserver((entries, obs) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          entry.target.src = entry.target.dataset.src;
          obs.unobserve(entry.target);  // Clean up!
        }
      });
    });
    ```
  </Accordion>
  
  <Accordion title="Question 5: What happens if you use a custom root that doesn't contain the observed element?">
    **Answer:** The observer **won't detect any intersections**. The observed element must be a descendant of the root element.
    
    ```javascript
    const sidebar = document.querySelector('.sidebar');
    const observer = new IntersectionObserver(callback, { root: sidebar });
    
    // โŒ This won't work - element is outside sidebar
    observer.observe(document.querySelector('.main .card'));
    
    // โœ… This works - element is inside sidebar
    observer.observe(sidebar.querySelector('.sidebar-item'));
    ```
    
    Always ensure observed elements are descendants of the root, or use `root: null` for viewport detection.
  </Accordion>
</AccordionGroup>

---

## Related Concepts

<CardGroup cols={2}>
  <Card title="Mutation Observer" icon="eye" href="/beyond/concepts/mutation-observer">
    Watch for DOM changes like added/removed elements and attribute modifications.
  </Card>
  <Card title="Resize Observer" icon="expand" href="/beyond/concepts/resize-observer">
    Detect when elements change size without polling or resize events.
  </Card>
  <Card title="Performance Observer" icon="gauge" href="/beyond/concepts/performance-observer">
    Monitor performance metrics like Long Tasks, layout shifts, and resource timing.
  </Card>
  <Card title="Event Loop" icon="arrows-rotate" href="/concepts/event-loop">
    Understand how JavaScript handles async operations and when callbacks fire.
  </Card>
</CardGroup>

---

## Resources

### Reference

<CardGroup cols={2}>
  <Card title="Intersection Observer API โ€” MDN" icon="book" href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">
    Complete API documentation covering all options, methods, and the IntersectionObserverEntry interface. The authoritative reference for browser behavior and edge cases.
  </Card>
  <Card title="IntersectionObserver Interface โ€” MDN" icon="book" href="https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver">
    Detailed reference for the IntersectionObserver constructor, properties (root, rootMargin, thresholds), and methods (observe, unobserve, disconnect, takeRecords).
  </Card>
</CardGroup>

### Articles

<CardGroup cols={2}>
  <Card title="A Few Functional Uses for Intersection Observer โ€” CSS-Tricks" icon="newspaper" href="https://css-tricks.com/a-few-functional-uses-for-intersection-observer-to-know-when-an-element-is-in-view/">
    Practical walkthrough of real-world Intersection Observer use cases including lazy loading, content visibility tracking, and auto-pausing videos. Great code examples with detailed explanations.
  </Card>
  <Card title="Intersection Observer v2: Trust is good, observation is better โ€” web.dev" icon="newspaper" href="https://web.dev/articles/intersectionobserver-v2">
    Covers the advanced Intersection Observer v2 API for tracking actual visibility (not just intersection). Essential reading for ad viewability and fraud prevention use cases.
  </Card>
  <Card title="Scroll Animations with Intersection Observer โ€” freeCodeCamp" icon="newspaper" href="https://www.freecodecamp.org/news/scroll-animations-with-javascript-intersection-observer-api/">
    Step-by-step guide to implementing scroll-triggered animations. Covers reveal-on-scroll effects, CSS transitions, and best practices for performant animations.
  </Card>
  <Card title="The Complete Guide to Lazy Loading Images โ€” CSS-Tricks" icon="newspaper" href="https://css-tricks.com/the-complete-guide-to-lazy-loading-images/">
    Comprehensive guide covering all lazy loading approaches including Intersection Observer, native loading="lazy", and fallback strategies. Includes performance considerations.
  </Card>
</CardGroup>

### Videos

<CardGroup cols={2}>
  <Card title="Learn Intersection Observer In 15 Minutes โ€” Web Dev Simplified" icon="video" href="https://www.youtube.com/watch?v=2IbRtjez6ag">
    Clear, beginner-friendly introduction covering observer basics, all configuration options, and a practical infinite scroll implementation. Perfect starting point.
  </Card>
  <Card title="Introduction to Intersection Observer โ€” Kevin Powell" icon="video" href="https://www.youtube.com/watch?v=T8EYosX4NOo">
    Explains why Intersection Observer is better than scroll events, with visual demonstrations of how intersection detection works. Great for understanding the fundamentals.
  </Card>
  <Card title="Lazy Load Images with Intersection Observer โ€” Fireship" icon="video" href="https://www.youtube.com/watch?v=aUjBvuUdkhg">
    Quick, practical tutorial showing how to implement lazy-loaded images. Covers data attributes, viewport detection, and unobserving after load.
  </Card>
  <Card title="How to Lazy Load Images โ€” Kevin Powell" icon="video" href="https://www.youtube.com/watch?v=mC93zsEsSrg">
    Detailed lazy loading implementation with rootMargin for pre-loading and practical tips for production use. Great follow-up after learning the basics.
  </Card>
</CardGroup>