Documentation

Component: Watcher IntersectionObserver helper for scroll animations and active navigation

The Watcher component is a universal “observer” you can use for many tasks: animate elements on scroll, highlight the active menu item (used by ScrollTo), trigger lazy effects, and more. It works by adding a helper class to elements when they enter the viewport.

How it works

When an element becomes visible in the viewport (on scroll or on initial page load), the watcher adds the class --watcher-view. When the element leaves the viewport, the class is removed — unless “once” mode is enabled.

Typical use cases
Scroll-in animations • “active section” navigation highlighting • triggering effects when an element appears

Basic usage

To start observing an element, add the attribute data-anim-watcher.

Enable watcher on an element
<div data-anim-watcher class="block">...</div>

Then you can target the state class in CSS/SCSS:

Example: animate when visible

.block {
	opacity: 0;
	transform: translateY(16px);
	transition: opacity 0.4s ease, transform 0.4s ease;
}

.block.--watcher-view {
	opacity: 1;
	transform: translateY(0);
}

Additional options

The watcher supports a few extra attributes that map directly to observer configuration.

data-anim-watcher-root

Selector of the “root” element used as viewport for the observer. Default is <body> / browser viewport.

Example: data-anim-watcher-root=".scroll-area"

data-anim-watcher-margin

Root margin (offset) as px or %. Useful when you want to trigger earlier/later.

Example: data-anim-watcher-margin="100px"

data-anim-watcher-threshold

How much of the element must be visible to trigger. 1 means 100% visible. Default is 0. You can pass a comma-separated list.

Example: data-anim-watcher-threshold="0.25,0.5,0.75"

data-anim-watcher-once

Observe only once. The class --watcher-view will be added one time and won’t be removed when the element leaves the viewport.

Example: trigger once at 50% visibility

<div
	data-anim-watcher
	data-anim-watcher-threshold="0.5"
	data-anim-watcher-once
	class="block"
>...</div>

Events

After each watcher trigger, an event watcherCallback is dispatched. You can listen for it anywhere in your code.

Listen to watcher events

document.addEventListener("watcherCallback", function (e) {
	// Full observer entry data:
	const entry = e.detail.entry;

	// Observed element:
	const targetElement = entry.target;
});
Tip
Use the event when you need JS-only behavior (for example, initialize a chart only when it becomes visible).

Download

Build scroll animations, active navigation, and visibility-based effects without wiring observers manually. Download the template and get the Watcher component out of the box.