viewmotion

initMotion()

viewmotion/core

Initializes the global IntersectionObserver and scans the DOM for animated elements. This is the entry point of the library.

Signature

function initMotion(options?: InitMotionOptions): Promise<MotionCleanup>;

Parameters

ParameterTypeDescription
optionsInitMotionOptionsOptional configuration object

Return value

Returns a MotionCleanup handle:

PropertyTypeDescription
scrollLenis | nullThe Lenis instance (if smoothScroll is enabled), otherwise null
destroy() => voidTeardown function — unobserves all elements and destroys the Lenis instance. Call on SPA route teardown to avoid memory leaks.

Options

PropertyTypeDefaultDescription
rootMarginstring"0px"Margin around root (CSS margin syntax)
thresholdnumber0.1Visibility ratio to trigger animation
defaultDurationnumberDefault animation duration in ms for all elements
defaultDelaynumberDefault animation delay in ms for all elements
smoothScrollbooleantrueEnable Lenis smooth scroll (requires lenis peer dep)
scrollLenisOptionsLenis configuration object (used when smoothScroll is enabled)

Usage

// Basic
const { destroy } = await initMotion();

// With options
const { scroll, destroy } = await initMotion({
  rootMargin: "0px 0px -80px 0px",
  threshold: 0.15,
  defaultDuration: 700,
  defaultDelay: 50,
  smoothScroll: true,
  scroll: {
    lerp: 0.08,
    wheelMultiplier: 1,
  },
});

// SPA teardown — call on route change to avoid memory leaks
destroy();

Call after DOM is ready

Call initMotion() after the DOM has parsed. In frameworks with client-side navigation like Astro, call it on each astro:page-load event to handle route changes.