Newshot

CSS timeline-scope: One Scroll, Multiple Carousels

Summary

When an element tracks a timeline, like a scroll timeline or view timeline, that timeline can drive an animation in any of the parent element’s children or descendants because they’re all in scope. One scroll, many elements. CSS timeline-scope: One Scroll, Multiple Carousels originally handwritten and published with love on CSS-Tricks . You should really get the newsletter as well.

Original text(via CSS-Tricks)

An animation timeline can only be of service to the element that defines it, and to that element’s descendants. However, with the timeline-scope CSS property, a named animation timeline is within reach of any element sharing an ancestor with the element that defines it, once that ancestor is fitted with a timeline scope for the same.

Put another way: When an element tracks a timeline, like a scroll timeline or view timeline, that timeline can drive an animation in any of the parent element’s children or descendants because they’re all in scope.

This is typically useful in creating user interfaces — like progress indicators for scrollable sections of a page — to provide visual effects as elements come and go out of view. Another example: highlighting position markers as different parts of a page are scrolled to.

In this article, we’re going to look at an example where scrolling through one carousel triggers an animation on another element with timeline-scope doing all the lifting. As you’ll see, It’s a nice animation effect. But it can also be useful for browsing through multiple visual focal points with a single scrolling by the user.

Some examples where you might have seen similar effects: In a racing game, when you select a car, its specifications roll through as you browse through the cars. In a TV match, player profiles are rotated through, along with their statistics. Essentially, multiple carousels are displayed in sync with a primary one.

Video game screenshot showing a carousel of sports cars with a red corvette selected and enlarged next to specifications related to the car.
Video game screenshot showing a carousel of sports cars with a red corvette selected and enlarged next to specifications related to the car.

Note: Since timeline-scope is not yet Baseline, I recommend its use for parts of a page that are decorative rather than important functionality.

The Carousels

First, we lay down a typical horizontal carousel, and a secondary one where scrolling is not needed. They’re in reverse order in the markup because of how we want them positioned.


        <!-- secondary carousel -->
<div class="cs">
  <ul class="cs-wrapper">
    <li class="cs-slide"></li>
    <li class="cs-slide"></li>
    <li class="cs-slide"></li>
  </ul>
</div>

<!-- primary carousel -->
<ul class="cp">
  <li class="cp-slide">
    <p>€19.90 / ID 485770</p>
  </li>
  <li class="cp-slide">
    <p>€19.90 / ID 485768</p>
  </li>
  <li class="cp-slide">
    <p>€19.90 / ID 485756</p>
  </li>
</ul>
      

Notice what we’re doing next in the CSS: allowing the primary carousel’s content to overflow and scroll in the horizontal direction. We’ll sprinkle in a little scroll snapping to make sure each carousel item doesn’t get skipped. Meanwhile, we’re clipping the secondary carousel’s overflow to prevent it from scrolling at all.


        /* Primary carousel */
.cp {
  /* Allows horizontal scrolling */
  overflow-x: auto;
  /* Snaps slides into place during scrolling */
  scroll-snap-type: x mandatory;
}

.cp-slide {
  /* Sets the snap position to the center of each slide */
  scroll-snap-align: center;
}

/* Secondary carousel */
.cs {
  /* Prevents content from overflowing.
      Disables all scrolling/scrollbars */
  overflow: clip;
}
      

So far, we have two carousels: one that scrolls and one that does not.

The Animation and Its Timeline

Next up, we need to define the keyframe animation that we’re going to apply to the secondary carousel. This is the timeline that’ll be in its scope, allowing the element to move from right to left in a way where one carousel item is displayed at a time as scrolling happens on the primary carousel.


        @keyframes cs-scroll {
  to {
    /* Moves to the left based on the number of siblings */
    transform: translateX(calc(-100% * (sibling-count() - 1)));
  }
}
      

Note: We’re using the very new CSS sibling-count() function. It does what it says and counts the number of direct children inside an element. Its support is nearing Baseline, but I want to call it out because it’s still rolling out and not prime time for production. Another way to go about it is to use CSS variables to index items directly in the markup — which is definitely more cumbersome and why we really need sibling-count() (not to mention sibling-index()).

OK, let’s apply that animation to the secondary carousel:

The animation is then applied to each slide in the secondary carousel. Since this animation needs to reflect the scrolling in the primary carousel, a scroll timeline, --carousel-scroll, is defined over the horizontal axis in the primary carousel, and the same timeline is assigned as the animation timeline in the secondary carousel.


        /* ... */

/* Secondary carousel */
.cs-slide {
  /* Applies the 'cs-scroll' animation to the slides */
  animation: cs-scroll linear forwards;
}
      

You might be used to setting a duration or delay on the animation to control its speed. But that’s unnecessary when we’re dealing with scroll animations because the animation’s progress is based on the scroll position, not a standard timeline. So, let’s register that scroll timeline on the primary carousel. All it takes is the scroll-timeline property with a custom ident as the value that we’ll reference on the secondary carousel with the animation-timeline property.


        /* Primary carousel */
.cp {
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  /* Defines a scroll timeline based on the x-axis */
  scroll-timeline: --carousel-scroll x;
}

/* Secondary carousel */
.cs-slide {
  /* Applies the 'cs-scroll' animation to the slides */
  animation: cs-scroll linear forwards;
  /* Links the animation progress to the scroll position of #cp */
  animation-timeline: --carousel-scroll;
}
      

Scoping the Timeline to Both Carousels

Now we’re tracking scrolling on the primary carousel. And just as I promised earlier, we can use the primary carousel’s scrolling progress to trigger the animation we defined on the secondary carousel.

All we have to do is hoist up the primary carousel’s scroll timeline to the <body> element. Why the <body>? Because that’s the parent the two carousels have in common. If we scope the timeline to the body, then we’re essentially passing it to both carousels since they are both direct children.


        body {
  /* Lifts the scope of '--carousel-scroll' timeline to the body element
     so it can be shared between #cp and #cs */
  timeline-scope: --carousel-scroll;
}
      

But, hey, we could literally do the same thing if the carousels were children of another container if that’s how the markup is structured, say .carousels:


        /* Parent container */
.carousels {
  /* Shares the scroll time with any descendant */
  timeline-scope: --carousel-scroll;
}

/* Primary carousel */
#cp {
  /* ... */
  scroll-timeline: --carousel-scroll x;
}

/* Secondary carousel */
.cs-slide {
  animation: cs-scroll linear forwards;
  animation-timeline: --carousel-scroll;
}
      

And we could technically do this same thing for as many children as we want! And they don’t have to all use the same animation either. It’s a wild idea that we can trigger one or many animations from a user’s scroll position in one or many elements. All in vanilla CSS.

Here’s That Demo Again

The text above was extracted automatically from the source page and may miss images, tables or code blocks. The original is authoritative.

Published by CSS-Tricks
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗