What is this?
A tiny helper for staggered (or chained) animations: the children of an element enter one after the other. The trick is a CSS animation with a delay per child, like style="--d: 150ms;", style="--d: 300ms;", … Setting these by hand works, but is a pain. This script does it for you, in 45 lines of vanilla JS.
How to use it?
Give any container the class stagger and load the CSS and JS from this demo, the script at the end of the body.
<ul class="stagger">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<link rel="stylesheet" href="stagger.css" />
<script src="stagger.js"></script>
The timing (the delay before the first child and the step between two children) can be set in the top of the stagger.js file.
Features
The stagger is only for what is on the screen at load. All those children form one queue: when a page has two stagger containers, the second one starts where the first one left off, instead of at the same time. The children outside the screen are simply there, without animation: nothing is hidden, so nobody has to wait for content while scrolling. Once the visitor scrolls, the children that are still waiting all start at once. A scroll by the visitor is recognised by the input that causes it (wheel, touch, keys, the scroll bar), so a scroll by the browser does not count. A reload halfway down the page staggers what is on the screen there: the script restores the scroll position itself, before it looks at what is on the screen. Without JS, there is no animation and everything is simply there. Visitors who prefer reduced motion get no animation at all.
Want something more radical? This CSS-only version sets a number per child in the HTML and needs no JS at all.
License
This code is licensed WTFPL.