DocsSearch & loading
Get started

Search Subtly documentation

Search by a task, helper name or framework.

Search & loading

nearViewport()

Load content as it nears the screen.

Try the example

Scroll inside the panel until the deferred report comes into view. It initializes once, when it reaches the visible area. The rest of the page stays usable throughout.

Project overview

Keep scrolling inside this panel to load the secondary report.

Report not initialized

Waiting for the report to enter view.

Actual npm package · local demonstration

Add it to your project

  1. Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
  2. Connect the behavior. Copy the JavaScript tab and call its exported mount(element) after your component’s elements exist. Replace simulated delays and data operations with your own application code.
  3. Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.

API and lifecycle

Observe an element and run initialize() once it approaches the viewport. rootMargin defaults to 200px. The observer disconnects after initialization; when IntersectionObserver is unavailable, initialization runs immediately.

Interface
element, initialize, rootMargin; returns cleanup
Lifecycle
The returned cleanup disconnects observation before initialization. If initialize creates listeners or widgets, manage those resources with their own cleanup.

Accessibility and safety

Implementation notes

Essential content must not depend on intersection observation.

Use for secondary content. Essential text, navigation and keyboard access must not depend on the observer running.

What to check

Scroll the element into view and verify initialization happens once. Also test removing the element before intersection and the observer-free fallback.