The property animation-timeline allows you to associate an animation with something other than time.
By default, an animation is a change in the value of a property over time, starting from the page load (time 0). With animation-timeline,
it is possible to vary the value of a property based on something other than time, most often the scrolling of an element.
For example: the opacity of the element below is linked to its scrolling. ⚠ Warning! This does not work with all browsers yet.
As you scroll through the content of this element to read more, you will notice that it becomes increasingly difficult to see the characters.
Indeed, the opacity of the element has been linked to its scrolling.
Fortunately, the opacity does not go down to 0, otherwise it would have been impossible for you to read the end of this text.
Other properties to know.
Here are the properties and functions needed to implement scroll-driven animations.
The animation is synchronized with the timeline of the document.
animation-timeline: none;
The animation is not associated with any timeline, not even the default document one: it no longer runs.
animation-timeline: --xxx;
--xxx is an identifier preceded by a double dash. It must have been defined with the property scroll-timeline-name
or the shorthand property scroll-timeline.
animation-timeline: scroll(block self);
x s
The scroll() function accepts two arguments: the axis to consider, and the element whose scrolling will be used to synchronize the animation.
The x parameter can take one of the following values:
block
inline
x
y
The values horizontal and vertical are recognized by Firefox but are not standardized.
The s parameter can take one of the following values:
root: the document as a whole.
nearest: the nearest scrollable element among its parents.
self: the element itself: the one to which the animation is applied.
animation-timeline: view(block 50% 10%);
a b c
With view(), the timeline is aligned with the visibility of an element. That element must therefore be part of the content that scrolls.
a: the axis to consider (block, inline, x, or y).
This parameter is optional: the default value is block.
b: end of the animation (the to side of the @keyframes directive). It is a percentage or length,
positive or negative. If it is a percentage, it is calculated relative to the dimension of the scrolling element, in the direction of
the scroll axis. In the case of a negative value, the animation starts while the element is not yet visible.
c: start of the animation (on the from side of the @keyframes rule).
A percentage or a length, positive or negative. These two values are calculated according to the drawing below.
Here is a small fun application of the ability to synchronize the value of a property with scrolling. Here, it is the rotation angle
of an image that is linked to the scrolling of an element. This results in a speedometer-shaped indicator.
This indicator is made up of two images: the scale, which is fixed, and the gauge needle, whose rotation angle is associated with the
scrolling of the element, in other words, with the position of the vertical scrollbar.
These two images are positioned as sticky so as not to scroll with the rest of the content.
The different elements that make up this example are identified as follows:
container: the container with scrolling.
graduation: the image showing the graduation.
indicator: the image depicting the needle.
content: the element containing the text.
Warning! This code does not work on certain browsers, such as Firefox, for example.
This element has been set as a scrolling container, a necessary condition for it to be able to drive an animation.
(overflow-y:scroll;).
The actual code needed to make the indicator work comes down to defining an animation with @keyframes and the few
properties below. But the complete CSS includes many other rules to size and position the elements in this example.
The example below is built with view(): it displays part of the text on an orange background. The animation runs when this part is visible.
This is text long enough to require scrolling. Use the scrollbar to scroll this text and trigger the animation.
This is the part of the text where the animation is applied
Animation of the animation-timeline property.
Being a property that defines animations itself, animation-timeline cannot be animated.
Manipulating the animation-timeline property programmatically.
Changes to the animation-timeline property take effect immediately. If the animation has not yet started, the effect of the change will
be visible the next time it starts. If the animation is already running, changing animation-timeline usually causes a jump in the
progress of animation.
Change the value of animation-timeline in JavaScript.
In JavaScript, here’s how to change the value of animation-timeline. We can see that JavaScript offers a syntax with the classic CSS
kebab-case notation (a dash to separate words), and another syntax with the camel-case notation (a capital letter to separate words).
let el = document.getElementById('id');
el.style['animation-timeline'] = 'scroll(block self)';
// or
let el = document.getElementById('id');
el.style.animationTimeline = 'scroll(block self)';
Read the value of animation-timeline in JavaScript.
The code below returns the value of animation-timeline when it has been set through the HTML style attribute.
The value set in the stylesheet via a CSS selector is not taken into account.
let el = document.getElementById('id');
let value = el.style['animation-timeline'];
// or
let el = document.getElementById('id');
let value = el.style.animationTimeline;
Read the computed value of animation-timeline in JavaScript.
The computed value is the one that results from evaluating the cascade of inheritances: it will primarily be the value possibly assigned
via the HTML style attribute, then the value assigned via a CSS selector (after resolving conflicts according to selector
priorities), a inherited value, or finally the initial value of animation-timeline.
In the case of animation-timeline, the value is returned in seconds, regardless of the unit used to set it.
If multiple animations are associated with the element, the different values of animation-timeline are returned separated by commas.
let el = document.getElementById('id');
let value = window.getComputedStyle(el).getPropertyValue('animation-timeline');
Change the value of the animation-timeline property with JQuery.
$('#id').css('animation-timeline', 'scroll(block self)'); // or $('#id').css('animationTimeline', 'scroll(block self)');
Read the computed value of the animation-timeline property with JQuery.
As in JavaScript, the value or values of animation-timeline are returned in seconds.
let value = $('#id').css('animation-timeline');
Try it yourself.
The two buttons below apply a value to the animation-timeline property. They then read back either the assigned value for the first button
or the computed value for the second. This second button allows you to see how the different values for the property are stored.
If your browser does not support scroll-driven animations, the calculated value will be empty.
Simulator.
The element below is defined as a container with scrolling. Use the scroll bar to activate the animation: background color change.
animation-timeline :
This element has been set as a scroll container, a prerequisite for it to be able to drive an animation.
Use the scroll bar to scroll through this container and to trigger the animation: the background should gradually change from white
to black.
Browsers compatibility with animation-timeline.
On Firefox , the features of the animation-timeline property are disabled by default. They can be enabled by setting the
layout.css.scroll-driven-animation.enabled option to true (2024). See access flags on Firefox.
Column 1
Support for the animation-timeline property. On Firefox, this feature must be enabled.
Column 2
Support for the view() function used with the animation-timeline property.
Column 3
Support for the scroll() function used with the animation-timeline property.
Notes:
(1) Disabled by default but can be enabled by setting the layout.css.scroll-driven-animation.enabled flag to true .
1
animation-timeline property
2
view() function
3
scroll() function
Estimated overall support.
74%
74%
74%
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :
Internet Explorer
UC Browser pour Androïd
Opéra Mobile
QQ Browser
Baidu Browser
Opéra
Samsung Internet
Firefox pour Androïd
Edge
Firefox
1
1
1
Chrome pour Androïd
Androïd Brower
Safari
Safari sur IOS
Chrome
KaiOS Browser
Opéra mini
See Caniuse.com for more compatibility information.
All definitions regarding CSS animations are grouped in the specification CSS Animations.
The property animation-timeline, as well as the following ones, are therefore included in this specification.