stagger
A composition node that starts each child animation a fixed offset after the previous one started.
A stagger node is a composition that starts its children one after another, each offset seconds after the previous child started. Children overlap when offset is shorter than their duration.
Example
import * as a from "@freestylejs/ani-core";
// 1. Define the animation for a single list item.
const itemAnimation = a.ani({ to: { opacity: 1, y: 0 }, duration: 0.5 });
// 2. Create a stagger animation for three items.
// Each subsequent animation will start 0.1s after the previous one.
const listEntrance = a.stagger(
[
itemAnimation, // for item 1
itemAnimation, // for item 2
itemAnimation, // for item 3
],
{ offset: 0.1 }
);
// 3. Create a timeline.
const myTimeline = a.rafTimeline(listEntrance);
// 4. Play the animation.
// All three children animate the same properties, so the most recently
// started child drives the value (see the overlap rule below).
// To animate separate elements, use one timeline per element.
myTimeline.play({ from: { opacity: 0, y: 20 } });Usage & Concepts
Overview
stagger is a specialized composition node for creating cascading or "domino" effects, typically used for lists of elements. Child i starts at i * offset. Unlike sequence, a child does not wait for the previous one to finish.
The total duration is the latest end among the children: child i ends at i * offset + duration. offset must be a finite number >= 0 (seconds): a number out of range throws a RangeError, a missing offset or one that is not a number (also a numeric string such as '0.1') a TypeError.
Children that animate the same property follow the timeline's overlap rule: the most recently started child wins, and its from is the value at its start time. Times within 1e-9 s of each other are one instant: sums taken in different orders (a loop's i * period, a sequence's durations added one by one) leave the same instant a few ulps apart, and the plan stores one value per instant. A zero-length segment applies at its start time, so a time sampled at that instant shows its value.
Best Practices
- Do use
staggerfor animating lists of items into or out of view. - Don't use
staggerif you need variable delays between animations; build a customsequencewithdelaynodes instead.
API Reference
| Name | Type | Description |
|---|---|---|
children | AnimationNode[] | Nodes started one offset after another. |
options.offset | number | Seconds between starts, finite and >= 0. Required. |
options.timing | TimingFunction | (Optional) Inherited by child segments without their own timing. |
options.id | string | (Optional) Identifier of the node. |
The second argument is an options object: a.stagger(children, { offset, timing, id }). A bare number or any extra argument throws a TypeError.