Enhancement
Scrollreveal
Reveal elements as they scroll into view, one after another
- Import
@parallelogram-js/core/components/Scrollreveal- Selector
[data-reveal]- Stylesheet
@parallelogram-js/core/styles/reveal.css
Playground
Staggered cards
Scroll the cards out of view and back to see them reveal in order.
<div class="card-grid">
<article class="card" data-reveal>Breakfast from 7am</article>
<article class="card" data-reveal>Lunch from noon</article>
<article class="card" data-reveal>Dinner from 6pm</article>
</div>
Use the example to see the events it sends.
About
Each element moves through data-reveal-state and the stylesheet draws the fade and slide, so nothing is written to its inline styles. Elements that come into view together are revealed in order, the stagger apart. data-reveal-class animates with a class instead. Under reduced motion elements are shown at once. While scripts run, the stylesheet hides elements until Scrollreveal mounts, and shows them again if it fails to load; with scripting off it never hides them. Register Scrollreveal on every page that uses the stylesheet, because an element whose component is never registered stays hidden.
Usage
Register the component and the framework loads it the first time a page contains [data-reveal].
import { Parallelogram } from '@parallelogram-js/core';
const app = Parallelogram.create();
app.components.add('[data-reveal]', () => import('@parallelogram-js/core/components/Scrollreveal'));
app.run();
The framework is optional. Scrollreveal.enhanceAll() mounts the component on every matching element by itself, for a page that wants one enhancement rather than a framework:
import Scrollreveal from '@parallelogram-js/core/components/Scrollreveal';
Scrollreveal.enhanceAll();
It takes a selector and the component's options, and returns the instance it mounted them with. What it leaves out is what the framework adds: loading a component only on the pages that use it, mounting again after a page swap, and ordering with dependsOn.
Its styles come from the package stylesheet:
@import '@parallelogram-js/core/styles/reveal.css';
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
data-reveal | flag | none | Marks an element to reveal |
data-reveal-threshold | number | 0.1 | Visible fraction that starts the reveal For every instance, Scrollreveal.defaults.threshold. |
data-reveal-root-margin | string | 0px | Grows or shrinks the viewport used to detect the element For every instance, Scrollreveal.defaults.rootMargin. |
data-reveal-once | boolean | true | Stay revealed; false hides the element again when it leaves the viewport For every instance, Scrollreveal.defaults.once. |
data-reveal-delay | number | 0 | Milliseconds to wait before revealing For every instance, Scrollreveal.defaults.delay. |
data-reveal-stagger | number | 100 | Milliseconds between elements revealed together For every instance, Scrollreveal.defaults.stagger. |
data-reveal-initial | hidden | visible | hidden | visible leaves the element showing until it is revealed For every instance, Scrollreveal.defaults.initialState. |
data-reveal-class | string | none | Classes to animate the reveal with instead of the stylesheet |
data-reveal-exit-class | string | none | Classes to animate hiding with |
data-reveal-stateset by the component | hidden | revealing | visible | hiding | error | none | Reveal progress |
data-reveal-enhancedset by the component | flag | none | Set once the element is watched |
Events
| Name | Detail | Description |
|---|---|---|
scrollreveal:mount | { element: HTMLElement; threshold: number; stagger: number; timestamp: number } | An element was set up Event bus |
scrollreveal:reveal-start | { element: HTMLElement; timestamp: number } | An element started revealing Event bus |
scrollreveal:reveal-complete | { element: HTMLElement; timestamp: number } | An element finished revealing Event bus |
scrollreveal:reveal-error | { element: HTMLElement; error: unknown; timestamp: number } | Revealing an element failed Event bus |
scrollreveal:hide-complete | { element: HTMLElement; timestamp: number } | An element finished hiding again Event bus |
CSS custom properties
| Name | Default | Description |
|---|---|---|
--reveal-distance | none | How far elements slide as they reveal |
--reveal-transition-duration | none | Length of the reveal |
--reveal-transition-easing | none | Easing of the reveal |