CSS Scroll-Driven Animations with animation-timeline in WordPress Themes

Scroll-driven animations with animation-timeline in a WordPress theme

Fade-in effects for sections and a reading progress bar used to need JavaScript: an Intersection Observer or a scroll listener that sets classes or styles. With animation-timeline, a normal CSS @keyframes animation runs along the scroll position instead of along time. The browser does it off the main thread, and there is no script to load.

Problem: A landing page built with blocks has twelve sections that fade in while scrolling and a progress bar at the top. A 4 KB script with an Intersection Observer and a scroll listener does that, and on slow phones the effect stutters.

Solution: Use animation-timeline: view() for the sections and animation-timeline: scroll() for the progress bar inside @supports, keep the content visible in browsers without support, respect prefers-reduced-motion, and add the classes to the blocks with the Additional CSS class field.

Sections that fade in

@keyframes ha-fade-up {
    from { opacity: 0; transform: translateY(2rem); }
    to   { opacity: 1; transform: none; }
}

@supports (animation-timeline: view()) {
    @media (prefers-reduced-motion: no-preference) {
        .ha-reveal {
            animation: ha-fade-up linear both;
            animation-timeline: view();
            animation-range: entry 0% entry 60%;
        }
    }
}

view() is a timeline that tracks the element itself while it crosses the viewport. animation-range: entry 0% entry 60% runs the whole animation during the entry phase: it starts when the top edge of the section appears at the bottom of the screen and is finished when 60% of the entry is done. Scrolling back reverses it, which is the nature of a scroll timeline.

The animation-timeline line must come after the animation shorthand: the shorthand resets the timeline to the default time-based one. Without support, nothing is applied and the sections are simply visible; the content never depends on the animation.

In the block editor, add ha-reveal under Advanced → Additional CSS class(es) of a Group, Columns or Cover block.

Staggered cards

A time-based animation-delay has no meaning on a scroll timeline. The offset goes into the range instead:

@supports (animation-timeline: view()) {
    @media (prefers-reduced-motion: no-preference) {
        .ha-cards > * {
            animation: ha-fade-up linear both;
            animation-timeline: view();
            animation-range: entry 10% entry 70%;
        }
        .ha-cards > :nth-child(3n + 2) { animation-range: entry 20% entry 80%; }
        .ha-cards > :nth-child(3n + 3) { animation-range: entry 30% entry 90%; }
    }
}

In a three-column grid, the second and third card of each row start a little later than the first.

A reading progress bar

@keyframes ha-progress {
    from { transform: scaleX(0); }
    to   { transform: scaleX(1); }
}

.ha-progress {
    position: fixed;
    inset-block-start: 0;
    inset-inline: 0;
    block-size: 3px;
    background: var(--wp--preset--color--primary, #1a56db);
    transform-origin: left;
    transform: scaleX(0);
}

@supports (animation-timeline: scroll()) {
    .ha-progress {
        animation: ha-progress linear both;
        animation-timeline: scroll(root block);
    }
}

scroll(root block) is the scroll position of the whole document in the block direction (vertical). scaleX() is used instead of animating width: a transform does not trigger layout and stays smooth. In an RTL theme use transform-origin: right under [dir="rtl"]. The bar itself goes into the header template part as a Custom HTML block or into wp_body_open:

add_action( 'wp_body_open', function (): void {
    if ( is_singular( 'post' ) ) {
        echo '<div class="ha-progress" aria-hidden="true"></div>';
    }
} );

Without support the bar stays at scaleX(0), so it is invisible instead of wrong.

Browser support and the fallback

Scroll-driven animations shipped in Chrome and Edge 115 (July 2023). Firefox has them behind a flag and Safari does not support them yet (mid-2024). For these browsers the sections are just there without an effect, which is a perfectly good result. If the effect is important, keep the Intersection Observer script, but load it only where it is needed:

if ( ! CSS.supports( 'animation-timeline: view()' ) ) {
    import( './reveal-observer.js' );
}

The observer version of the same effect: Intersection Observer for scroll animations.

NOTE: Animate only opacity and transform. Properties like height, top or box-shadow force layout or paint on every scrolled pixel, and the effect stutters again. The basics of @keyframes and the animation properties: CSS animations and keyframes.

Update (2025): Safari 26 (September 2025) supports scroll-driven animations as well.