Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Intersection Observer in JavaScript

Watch a card in JavaScript with IntersectionObserver at a 0.5 threshold. Stop observing once half is in view; geometric visibility does not prove reading.

IntermediateJavaScript Browser APIsExample 15 of 15

Save this as index.html and open it in a browser.

intersection-observer.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<p>Scroll down to the green card.</p><div style="height:110vh"></div><div id="card" style="height:150px;background:lightgreen">Reading checkpoint</div><p id="status">Waiting for the card.</p>
7<script>
8const card = document.querySelector('#card');
9const status = document.querySelector('#status');
10if (!('IntersectionObserver' in window)) status.textContent = 'Observer not supported.';
11else {
12 const observer = new IntersectionObserver(entries => {
13 for (const entry of entries) {
14 if (entry.isIntersecting && entry.intersectionRatio >= 0.5) {
15 status.textContent = 'At least half the card is in view.';
16 console.log(status.textContent);
17 observer.unobserve(card);
18 }
19 }
20 }, { threshold: 0.5 });
21 observer.observe(card);
22}
23</script>
24</body>
25</html>

Sample Output

At least half the card is in view.

The target is intersecting with a ratio of at least 0.5. It is then unobserved so the message logs once.

In this one

  • Watch an element during scrolling
  • Set a halfway visibility threshold
  • Check observer entries
  • Stop watching after a one-time result

How it works

  1. 1

    Open the HTML and find the card below the first screen.

  2. 2

    Create an observer with threshold 0.5.

  3. 3

    Observe the existing card element.

  4. 4

    When an entry is intersecting and at least half visible, update the message.

  5. 5

    Unobserve the card, so later scrolling does not repeat the success log.

Edge cases

Initial hidden entry

The observer can call back before the target enters view.

Check isIntersecting and the ratio instead of treating any callback as success.

Missing or oversized target

observe needs an element, and a very tall target may never become half visible.

Select the element after it exists. Choose thresholds that make sense for the target and screen size.

What's going on

Intersection Observer is a browser feature that reports when an element crosses a visibility boundary. It compares the element with a viewing area, normally the browser window. This is useful for loading an image near the screen or marking that a reading checkpoint has appeared.

The complete HTML creates space above a green card so you can test by scrolling. The observer watches the card with a threshold of 0.5. That means the callback can run when the visible share crosses the halfway point. A callback is a function the browser calls later when something happens.

The handler checks both isIntersecting and intersectionRatio before reporting success. Initial callbacks can describe an element that is not yet visible. unobserve stops watching after the first useful result. This measures geometric overlap, not attention; another element can cover the card, and seeing it does not prove someone read it.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does threshold 0.5 mean?

Pick an answer to check it.

Question 2

Why test isIntersecting?

Pick an answer to check it.

Questions people ask

Why is there a tall spacer?

It places the card below the initial screen, giving you something to scroll toward.

Does 0.5 mean 50 pixels?

No. It means half of the target area, not a fixed pixel distance.

What does unobserve do?

It stops watching this card. disconnect would stop watching all targets owned by the observer.