<cs-intersection-observer>
StableHelpersSince 0.1

Tracks immediate child elements and fires events as they move in and out of view. Useful for lazy loading, scroll-triggered animations, and viewport-aware interactions.

This component uses the IntersectionObserver API to track when its direct children enter or leave a designated root element. The cs-intersect event fires whenever elements cross the visibility threshold.

Scroll to see the element intersect at 100% visibility

Only direct children of the host are monitored.
Nested elements won’t trigger intersection events.

Examples

Root Element

You can observe intersections within a specific container by assigning the root attribute to the root element’s ID. Apply rootMargin with the root-margin attribute to expand or contract the observation area.

<div id="scroll-container">
  <cs-intersection-observer root="scroll-container" root-margin="50px 0px"> ... </cs-intersection-observer>
</div>

Thresholds

Track different visibility percentages by providing multiple threshold values as a space-separated list.

<cs-intersection-observer threshold="0 0.25 0.5 0.75 1"> ... </cs-intersection-observer>

Intersect Class

The intersect-class attribute automatically toggles the specified class on direct children when they become visible. This enables pure CSS styling without JavaScript event handlers.

Fade In
Slide In
Scale & Rotate
Bounce
Scroll to see elements transition at 50% visibility

Reacting to Intersections

The intersection observer tracks only its direct children. The component uses display: contents styling, so it integrates cleanly with flex and grid layouts from a parent container.

<div class="cs-stack cs-gap-0">
  <cs-intersection-observer>
    <div class="box">Box 1</div>
    <div class="box">Box 2</div>
    <div class="box">Box 3</div>
  </cs-intersection-observer>
</div>

The component tracks elements as they enter and exit the root element (viewport by default) and emits the cs-intersect event on state changes. The event provides event.detail.entry, an IntersectionObserverEntry object with intersection details.

You can identify the triggering element through entry.target. Check entry.isIntersecting to determine if an element is entering or exiting the viewport.

observer.addEventListener('cs-intersect', event => {
  const entry = event.detail.entry;

  if (entry.isIntersecting) {
    console.log('Element entered viewport:', entry.target);
  } else {
    console.log('Element left viewport:', entry.target);
  }
});

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/intersection-observer/intersection-observer.js';
import './cornerstone/components/intersection-observer/intersection-observer.js';
import CsIntersectionObserver from '@cruglobal/cornerstone-components/react/intersection-observer/index.js';

Slots

Learn more about using slots.

NameDescription
(default)Elements to track. Only immediate children of the host are monitored.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
disableddisabledDeactivates the intersection observer functionality.booleanfalseYes
intersectClassintersect-classCSS class applied to elements during intersection. Automatically removed when elements leave the viewport, enabling pure CSS styling based on visibility state.string''
onceonceIf enabled, observation ceases after initial intersection.booleanfalseYes
rootrootElement ID to define the viewport boundaries for tracked targets.string | nullnull
rootMarginroot-marginOffset space around the root boundary. Accepts values like CSS margin syntax.string'0px'
thresholdthresholdOne or more space-separated values representing visibility percentages that trigger the observer callback.string'0'

Events

Learn more about events.

NameDescription
cs-intersectFired when a tracked element begins or ceases intersecting.