Email or username:

Password:

Forgot your password?
Bramus

Wow, look at this AMAZING Scroll-Driven Animations demo by Paul Noble!

codepen.io/paulnoble/pen/gOVPe

Go check it out (and hit the ❤️ icon).

16 comments
Bramus

The first clever thing Paul is doing there is using an invisible scroller laid on top of the stack of cards.

This scroller is used as the scroll-timeline for the .card-stack.

Screenshot of the page with DevTools open, inspecting the .scroller element.
Bramus

To make the .card-stack see the scroll-timeline, it gets hoisted using`timeline-scope` on the body. Any child of the body - including .card-stack – can now use that `--scroll-timeline`.

Screenshot of DevTools with the body element selected. It has a `timeline-scope` declaration on it.
Bramus

To make the cards show at the center, Paul relies on scroll snapping, which brings me to the second clever thing he did there: use the new `scrollsnapchange` event to change the `data-active-index` attribute on the `main` element.

developer.chrome.com/blog/scro

Bramus

(The logic relies on doing some pixel calculations which could be reworked to use `event.snapTargetInline` instead, but that’s me nitpicking)

Screenshot of the JavaScript code as found in CodePen
Bramus

And finally here’s the third clever thing Paul did: use that `data-active-index` to attach different animations to the cards.

This is done for each possible index and is auto-generated using Sass.

Screenshot of the generated CSS, showing how the animations get changed based on the data-active-index.
Bramus

The keyframes are also generated and built in such a way that you get the directionality effect.

For example, here are the keyframes for the 4th card being active. Upon inspecting the other keyframes you’ll see the keyframe here at 75% move around.

Screenshot of the `aniamte4-active` keyframes
Bramus

(I believe this per-card keyframes approach can be simplified by either leveraging `animation-range` or by relying on a ViewTimeline per scroll-item [which I think would also bypass the need for `scrollsnapchange`], but again: that’s me nitpicking here)

Bramus

Thank you for building this WONDERFUL scroll-driven animations demo, Paul. It was very delightful to see. Stellar work!

(Is Paul on Mastodon? Couldn’t find a matching profile here.)

Bramus

The logic/math for the card stack itself was done by Nate Smith a while ago.

Find all the details on X: x.com/nasm423/status/179513345

Bramus

Here’s my take on this demo: codepen.io/bramus/pen/xxvgzoL

- No Sass
- Reuses the same active and inactive keyframes on all cards, powered by View-Timeline + animation-range
- Reworked snapChange logic to use event.snapTargetInline, falling back to IntersectionObserver if no support

Andreas Schantl

@bramus Sadly it's only working in Chrome for me. Neither Firefox nor Safari seem to work.

Bramus

@andjsch That’s because those browsers don’t support Scroll-Driven Animations.

They are working on it though … and there is a polyfill you can use in the meantime.

Go Up