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.

Breakfast from 7am
Lunch from noon
Dinner from 6pm
<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>

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

NameTypeDefaultDescription
data-revealflagnoneMarks an element to reveal
data-reveal-thresholdnumber0.1Visible fraction that starts the reveal For every instance, Scrollreveal.defaults.threshold.
data-reveal-root-marginstring0pxGrows or shrinks the viewport used to detect the element For every instance, Scrollreveal.defaults.rootMargin.
data-reveal-oncebooleantrueStay revealed; false hides the element again when it leaves the viewport For every instance, Scrollreveal.defaults.once.
data-reveal-delaynumber0Milliseconds to wait before revealing For every instance, Scrollreveal.defaults.delay.
data-reveal-staggernumber100Milliseconds between elements revealed together For every instance, Scrollreveal.defaults.stagger.
data-reveal-initialhidden | visiblehiddenvisible leaves the element showing until it is revealed For every instance, Scrollreveal.defaults.initialState.
data-reveal-classstringnoneClasses to animate the reveal with instead of the stylesheet
data-reveal-exit-classstringnoneClasses to animate hiding with
data-reveal-stateset by the componenthidden | revealing | visible | hiding | errornoneReveal progress
data-reveal-enhancedset by the componentflagnoneSet once the element is watched

Events

NameDetailDescription
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

NameDefaultDescription
--reveal-distancenoneHow far elements slide as they reveal
--reveal-transition-durationnoneLength of the reveal
--reveal-transition-easingnoneEasing of the reveal