Industry observers note that 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.
Sources indicate that 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.
Note: Since timeline-scope is not yet Baseline, I recommend its use for parts of a page that are decorative rather than important functionality.
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.
According to recent reports, 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.
So far, we have two carousels: one that scrolls and one that does not.
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.
Note: We’re using the very newly introduced CSS sibling-count() function. It does what it states 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:.
Recent findings suggest that 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.
Sources indicate that 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.
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.
Sources indicate that all we have to do is hoist up the primary carousel’s scroll timeline to the element. Why the ? 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.
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:.
Recent findings suggest that 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.
