Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

IntersectionObserverEntry: intersection state and observed element

`isIntersecting` tells you whether the element intersects the root at the time of measurement, and `target` tells you which observed element the entry describes.

Version: Current web platform; runtime support varies

Syntax

entry.isIntersecting // boolean
entry.target         // Element

IntersectionObserverEntry.isIntersecting/target refers to two read-only instance properties. entry.isIntersecting is a boolean: true if the element intersects the root, false otherwise. entry.target is the same DOM Element passed to observe(), not the root or a selector. The root can be, for example, a scrolling container. When root is null (the default), the reference area is the top-level document’s viewport. For a target inside an iframe, the calculation of intersectionRect also clips to the frame’s viewport.

An entry records a measurement; it does not update live. After observe(), the initial report arrives asynchronously even if the element hasn't moved. Later entries can report a change in intersection state or a crossing of a configured ratio threshold. So true on its own does not mean the element just entered. Process the whole entries array: one callback can deliver several entries.

Edge contact can count as intersection even when intersectionRatio is 0. To check for positive geometric intersection area, require both entry.intersectionRect.width > 0 and entry.intersectionRect.height > 0. This still doesn't prove the user can see the element: another element may cover it.

This is a web platform API, not part of ECMAScript 2024. Check that the target runtime provides IntersectionObserver. Use unobserve(element) to stop watching one element or disconnect() to stop watching all elements for that observer.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>IntersectionObserverEntry example</title>
</head>
<body>
<script>
(() => {
  const root = document.createElement('div');
  root.style.cssText = 'position:fixed;top:0;left:0;width:120px;height:60px;overflow:hidden';

  const makeTarget = (left) => {
    const element = document.createElement('div');
    element.style.cssText = `position:absolute;top:8px;left:${left}px;width:16px;height:16px`;
    root.append(element);
    return element;
  };

  const inside = makeTarget(8);
  const outside = makeTarget(150);
  document.body.append(root);

  const states = new Map();
  const observer = new IntersectionObserver((entries, currentObserver) => {
    for (const entry of entries) {
      states.set(entry.target, entry.isIntersecting);
    }
    if (!states.has(inside) || !states.has(outside)) return;

    currentObserver.disconnect();
    root.remove();
    console.log(`inside: ${states.get(inside)}\noutside: ${states.get(outside)}`);
  }, { root });

  observer.observe(inside);
  observer.observe(outside);
})();
</script>
</body>
</html>

Result

inside: true
outside: false

Save the code as an HTML file, open it in a browser, and check the developer console for the output. The first element is inside the root container and the second is entirely outside it. The code waits for both initial entries, prints their states in a fixed order, and cleans up.

Related lessons

  • JavaScript · Lesson 30 · Intermediate · Version: 3

Sources