1. Introduction
This specification defines mechanisms for driving the progress of an animation based on the scroll progress of a scroll container. These scroll-driven animations use a timeline based on scroll position, rather than one based on clock time. Animations are linked to such a timeline using the CSS animation-timeline property or the Web Animations API. [WEB-ANIMATIONS-1]
There are two types of scroll-driven timelines:
1.1. Relationship to other specifications
Web Animations [WEB-ANIMATIONS-1] defines an abstract conceptual model for animations on the Web platform, with elements of the model including animations and their timelines, and associated programming interfaces.
This specification extends this model by defining a new type of animation timeline: a scroll progress timeline.
This specification defines both programming interfaces for interacting with these concepts, as well as CSS properties which apply these concepts to CSS Animations [CSS-ANIMATIONS-1].
The behavior of the CSS properties is described in terms of the programming interfaces. User agents that do not support scripting may still conform to this specification by implementing the CSS features to behave as if the underlying programming interfaces were in place.
1.2. Relationship to asynchronous scrolling
Some user agents support scrolling that is asynchronous with respect to layout or script. This specification is intended to be compatible with such an architecture.
Specifically, this specification allows expressing scroll-linked effects in a way that does not require script to run each time the effect is sampled. User agents that support asynchronous scrolling are allowed (but not required) to sample such effects asynchronously as well.
1.3. Value Definitions
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.
2. Scroll Progress Timelines
Scroll progress timelines are timelines linked to progress in the scroll position of a scroll container along a particular axis. The startmost scroll position represents 0% progress and the endmost scroll position represents 100% progress. If the 0% position and 100% position coincide (i.e. the scroll container has no overflow to scroll), the timeline is inactive.
Scroll progress timelines can be referenced in animation-timeline anonymously using the scroll() functional notation or by name (see § 4 Named Timeline Scoping)
after declaring them using the scroll-timeline properties.
In the Web Animations API,
they can be represented anonymously by a ScrollTimeline
object.
2.1. Anonymous Scroll Progress Timelines
2.1.1. The scroll() notation
The scroll() functional notation can be used as a value of animation-timeline and specifies a scroll progress timeline. Its syntax is
scroll() = scroll( <axis>? <scroller>? ) <axis> = block | inline | vertical | horizontal <scroller> = root | nearest
By default, scroll() references the block axis of the nearest ancestor scroll container. Its arguments modify this lookup as follows:
- block
- Specifies to use the measure of progress along the block axis of the scroll container. (Default.)
- inline
- Specifies to use the measure of progress along the inline axis of the scroll container.
- vertical
- Specifies to use the measure of progress along the vertical axis of the scroll container.
- horizontal
- Specifies to use the measure of progress along the horizontal axis of the scroll container.
- nearest
- Specifies to use the nearest ancestor scroll container. (Default.)
- root
- Specifies to use the document viewport as the scroll container.
2.1.2. The ScrollTimeline
Interface
enum {
ScrollAxis ,
"block" ,
"inline" ,
"horizontal" };
"vertical" dictionary {
ScrollTimelineOptions Element ?;
source ScrollAxis = "block"; }; [
axis Exposed =Window ]interface :
ScrollTimeline AnimationTimeline {constructor (optional ScrollTimelineOptions = {});
options readonly attribute Element ?source ;readonly attribute ScrollAxis axis ; };
A ScrollTimeline
is an AnimationTimeline
that represents a scroll progress timeline.
It can be passed to
the Animation
constructor or the animate()
method
to link the animation to a scroll progress timeline.
source
, of type Element, readonly, nullable-
The scroll container element whose scroll position drives the progress of the timeline.
axis
, of type ScrollAxis, readonly-
The axis of scrolling that drives the progress of the timeline. See value definitions for <axis>, above.
Inherited attributes:
currentTime
(inherited fromAnimationTimeline
)-
Represents the scroll progress of the scroll container as a percentage CSSUnitValue, with 0% representing its startmost scroll position (in the writing mode of the scroll container). Null when the timeline is inactive.
While 0% will usually represent the scroll container’s initial scroll position, it might not depending on its content distribution. See CSS Box Alignment 3 § 5.3 Overflow and Scroll Positions. Is this what we want?
Add a note about whether currentTime
can be negative or > 100%.
ScrollTimeline(options)
-
Creates a new
ScrollTimeline
object using the following procedure:-
Let timeline be the new
ScrollTimeline
object. -
Set the
source
of timeline to:- If the
source
member of options is present and not null, -
The
source
member of options. - Otherwise,
-
The
scrollingElement
of theDocument
associated with theWindow
that is the current global object.
- If the
-
Set the
axis
property of timeline to the corresponding value from options.
-
If the source
of a ScrollTimeline
is an element whose principal box does not exist
or is not a scroll container,
or if there is no scrollable overflow,
then the ScrollTimeline
is inactive.
A ScrollTimeline
’s duration
is 100%.
2.2. Named Scroll Progress Timelines
Scroll timelines can also be defined on the scroll container itself, and then referenced by name. See § 4 Named Timeline Scoping.
2.2.1. Naming a Scroll Progress Timeline: the scroll-timeline-name property
Name: | scroll-timeline-name |
---|---|
Value: | none | <custom-ident> |
Initial: | none |
Applies to: | scroll containers |
Inherited: | no |
Percentages: | n/a |
Computed value: | the specified keyword |
Canonical order: | per grammar |
Animation type: | not animatable |
Specifies a name for the scroll progress timeline associated with this scroll container. The axis for this timeline is given by scroll-timeline-axis.
2.2.2. Axis of a Scroll Progress Timeline: the scroll-timeline-axis property
Name: | scroll-timeline-axis |
---|---|
Value: | block | inline | vertical | horizontal |
Initial: | block |
Applies to: | scroll containers |
Inherited: | no |
Percentages: | n/a |
Computed value: | the specified keyword |
Canonical order: | per grammar |
Animation type: | not animatable |
Specifies an axis for the scroll progress timeline associated with this scroll container. The name for this timeline is given by scroll-timeline-name.
Values are as defined for scroll().
2.2.3. Scroll Timeline Shorthand: the scroll-timeline shorthand
Name: | scroll-timeline |
---|---|
Value: | <'scroll-timeline-name'> || <'scroll-timeline-axis'> |
Initial: | see individual properties |
Applies to: | scroll containers |
Inherited: | no |
Percentages: | see individual properties |
Computed value: | see individual properties |
Animation type: | not animatable |
Canonical order: | per grammar |
This property is a shorthand for setting scroll-timeline-name and scroll-timeline-axis in a single declaration.
3. View Progress Timelines
Often animations are desired to start and end during the portion of the scroll progress timeline that a particular element (the view progress subject element) is in view within the scrollport. View progress timelines are segments of a scroll progress timeline that are scoped to the scroll positions in which any part of the associated element’s principal box intersects its nearest ancestor scrollport (or more precisely, the relevant view progress visibility range of that scrollport). The startmost such scroll position represents 0% progress and the endmost such scroll position represents 100% progress.
Note: The 0% and 100% scroll positions are not always reachable, e.g. if the box is positioned at the start edge of the scrollable overflow rectangle, it might not be possible to scroll to < 32% progress.
View progress timelines can be referenced in animation-timeline by name
(see § 4 Named Timeline Scoping)
after declaring them using the view-timeline properties
on the view progress subject.
In the Web Animations API,
they can be represented anonymously by a ViewTimeline
object.
3.1. View Progress Timeline Ranges
View progress timelines define the following named timeline ranges:
- cover
-
Represents the full range of the view progress timeline:
-
0% progress represents the position at which the start border edge of the element’s principal box coincides with the end edge of its view progress visibility range.
-
100% progress represents the position at which the end border edge of the element’s principal box coincides with the start edge of its view progress visibility range.
-
- contain
-
Represents the range during which the principal box is either fully contained by, or fully covers,
its view progress visibility range within the scrollport.
-
0% progress represents the earlier position at which:
-
the start border edge of the element’s principal box coincides with the start edge of its view progress visibility range.
-
the end border edge of the element’s principal box coincides with the end edge of its view progress visibility range.
-
-
100% progress represents the later position at which:
-
the start border edge of the element’s principal box coincides with the start edge of its view progress visibility range.
-
the end border edge of the element’s principal box coincides with the end edge of its view progress visibility range.
-
-
- entry
-
Represents the range during which the principal box is entering the view progress visibility range.
Should this be entry or enter?
- exit
- Represents the range during which the principal box is exiting the view progress visibility range.
In all cases, the writing mode used to resolve the start and end sides is the writing mode of the relevant scroll container.
3.2. Anonymous View Progress Timelines
3.2.1. The ViewTimeline
Interface
dictionary {
ViewTimelineOptions Element ;
subject ScrollAxis = "block"; }; [
axis Exposed =Window ]interface :
ViewTimeline ScrollTimeline {constructor (optional ViewTimelineOptions = {});
options readonly attribute Element subject ;readonly attribute CSSNumericValue startOffset ;readonly attribute CSSNumericValue endOffset ; };
A ViewTimeline
is an AnimationTimeline
that specifies a view progress timeline.
It can be passed to
the Animation
constructor or the animate()
method
to link the animation to a view progress timeline.
subject
, of type Element, readonly-
The element whose principal box’s visibility in the scrollport defines the progress of the timeline.
startOffset
, of type CSSNumericValue, readonly-
Represents the starting (0% progress) scroll position of the view progress timeline as a length offset (in px) from the scroll origin. Null when the timeline is inactive.
endOffset
, of type CSSNumericValue, readonly-
Represents the ending (100% progress) scroll position of the view progress timeline as a length offset (in px) from the scroll origin. Null when the timeline is inactive.
Note: The value of startOffset
can be greater than endOffset
,
for example in the horizontal axis in a right-to-left (rtl) writing mode.
This makes the offsets compatible with the way scrollLeft and scrollTop work, which go negative from zero in RTL scroll containers, is this how we want it to work here?
Inherited attributes:
source
(inherited fromScrollTimeline
)-
The nearest ancestor of the
subject
whose principal box establishes a scroll container, whose scroll position drives the progress of the timeline. axis
(inherited fromScrollTimeline
)-
Specifies the axis of scrolling that drives the progress of the timeline. See <axis>, above.
currentTime
(inherited fromAnimationTimeline
)-
Represents the current progress of the view progress timeline as a percentage
CSSUnitValue
representing its scroll container’s scroll progress at that position. Null when the timeline is inactive.
ViewTimeline(options)
-
Creates a new
ViewTimeline
object using the following procedure:-
Let timeline be the new
ViewTimeline
object. -
Set the
subject
andaxis
properties of timeline to the corresponding values from options. -
Set the
source
of timeline to thesubject
’s nearest ancestor scroll container element.
-
If the source
or subject
of a ViewTimeline
is an element whose principal box does not exist,
or if its nearest ancestor scroll container has no scrollable overflow,
then the ViewTimeline
is inactive.
Figure out how to incorporate fit/inset abilities.
3.3. Named View Progress Timelines
View timelines can also be defined declaratively and then referenced by name. See § 4 Named Timeline Scoping.
3.3.1. Naming a View Progress Timeline: the view-timeline-name property
Name: | view-timeline-name |
---|---|
Value: | none | <custom-ident># |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Computed value: | the keyword none or a list of <custom-ident>s |
Canonical order: | per grammar |
Animation type: | not animatable |
Specifies names for any view progress timelines associated with this element’s principal box.
Note: The number of names listed here determines the number of view progress timelines associated with this element.
3.3.2. Axis of a View Progress Timeline: the view-timeline-axis property
Name: | view-timeline-axis |
---|---|
Value: | [ block | inline | vertical | horizontal ]# |
Initial: | block |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Computed value: | a list of the keywords specified |
Canonical order: | per grammar |
Animation type: | not animatable |
Specifies an axis for each named view progress timeline associated with this scroll container.
If view-timeline-name has more names than view-timeline-axis has specified axes, the excess timelines use the last view-timeline-axis value. If view-timeline-name has fewer names than view-timeline-axis has specified axes, the used view-timeline-axis list is truncated.
3.3.3. Inset of a View Progress Timeline: the view-timeline-inset property
Name: | view-timeline-inset |
---|---|
Value: | [ [ auto | <length-percentage> ]{1,2} ]# |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentages: | relative to the corresponding dimension of the relevant scrollport |
Computed value: | a list consisting of two-value pairs representing the start and end insets each as either the keyword auto or a computed <length-percentage> value |
Canonical order: | per grammar |
Animation type: | by computed value type |
Specifies an inset (positive) or outset (negative) adjustment of the scrollport when determining whether the box is in view when setting the bounds of the corresponding view progress timeline. The first value represents the start inset in the relevant axis; the second value represents the end inset. If the second value is omitted, it is set to the first. The resulting range of the scrollport is the view progress visibility range.
- auto
- Indicates to use the value of scroll-padding.
- <length-percentage>
- Like scroll-padding, defines an inward offset from the corresponding edge of the scrollport.
If view-timeline-name has more names than view-timeline-inset has specified inset pairs, the excess timelines use the last view-timeline-inset pair. If view-timeline-name has fewer names than view-timeline-inset has specified inset pairs the used view-timeline-inset list is truncated.
3.3.4. View Timeline Shorthand: the view-timeline shorthand
Name: | view-timeline |
---|---|
Value: | [ <'view-timeline-name'> || <'view-timeline-axis'> ]# |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | see individual properties |
Percentages: | see individual properties |
Computed value: | see individual properties |
Animation type: | see individual properties |
Canonical order: | per grammar |
This property is a shorthand for setting view-timeline-name and view-timeline-axis in a single declaration. It does not set view-timeline-inset.
Should it reset view-timeline-inset also?
4. Named Timeline Scoping
A named scroll progress timeline or view progress timeline is referenceable in animation-timeline by:
-
the declaring element itself
-
that element’s descendants
-
that element’s following siblings and their descendants
If multiple elements have declared the same timeline name, the matching timeline is the one declared on the nearest element in tree order, which considers siblings closer than parents. In case of a name conflict on the same element, scroll progress timelines take precedence over view progress timelines.
Note: Like most operations in CSS besides selector matching, timeline scoping occurs over the flattened element tree.
Do we want to expand this scope to preceding siblings and/or distant cousins?
5. Avoiding cycles with layout
The ability for scrolling to drive the progress of an animation, gives rise to the possibility of layout cycles, where a change to a scroll offset causes an animation’s effect to update, which in turn causes a new change to the scroll offset.
To avoid such layout cycles,
animations with a scroll progress timeline are sampled once per frame,
after scrolling in response to input events has taken place,
but before requestAnimationFrame()
callbacks are run.
If the sampling of such an animation causes a change to a scroll offset,
the animation will not be re-sampled to reflect the new offset
until the next frame.
The implication of this is that in some situations, in a given frame,
the rendered scroll offset of a scroll container might not be consistent
with the state of an animation driven by scrolling that scroll container.
However, this will only occur in situations where the animation’s effect
changes the scroll offset of that same scroll container
(in other words, in situations where the animation’s author is asking for trouble).
In normal situations, including—importantly—
User agents that composite frames asynchronously with respect to layout and/or script may, at their discretion, sample scroll-driven animations once per composited frame, rather than (or in addition to) once per full layout cycle. Again, if sampling such an animation causes a change to a scroll offset, the animation will not be re-sampled to reflect the new offset until the next frame.
Nothing in this section is intended to require
that scrolling block on layout or script.
If a user agent normally composites frames where scrolling has occurred
but the consequences of scrolling have not been fully propagated in layout or script
(for example, scroll
event listeners have not yet run),
the user agent may likewise choose not to sample scroll-driven animations
for that composited frame.
In such cases, the rendered scroll offset
and the state of a scroll-driven animation
may be inconsistent in the composited frame.
Appendix A: Named Timeline Ranges
This section should move to CSS-ANIMATIONS-2 and WEB-ANIMATIONS-2.
This specification introduces the concept of named timeline ranges to CSS Animations and Web Animations.
A named timeline range is a named segment of an animation timeline. The start of the segment is represented as 0% progress through the range; the end of the segment is represented as 100% progress through the range. Multiple named timeline ranges can be attached to a given timeline, and multiple such ranges can overlap. For example, the contain range of a view progress timeline overlaps with its cover range. Named timeline ranges are represented by the <timeline-range-name> value type, which indicates a CSS identifier representing one of the predefined named timeline ranges.
Note: In this specification, named timeline ranges must be defined to exist by a specification such as [SCROLL-ANIMATIONS-1]. A future level may introduce APIs for authors to declare their own custom named timeline ranges.
Named Timeline Range Keyframe Selectors
Named timeline range names and percentages can be used to attach keyframes to specific progress points within the named timeline range. The CSS @keyframes rule is extended thus:
<keyframe-selector> = from | to | <percentage> | <timeline-range-name> <percentage>
where <
Keyframes are attached to the specified point in the timeline. If the timeline does not have a corresponding named timeline range, then any keyframes attached to points on that named timeline range are ignored.
Attaching Animations to Timeline Ranges
A set of animation keyframes can be attached in reference to named timeline ranges by adjusting the duration of the animation.
Name: | animation-delay, animation-range |
---|---|
Value: | [ <'animation-delay-start'> <'animation-delay-end'>? | <timeline-range-name> ]# |
Initial: | see individual properties |
Applies to: | see individual properties |
Inherited: | see individual properties |
Percentages: | see individual properties |
Computed value: | see individual properties |
Animation type: | see individual properties |
Canonical order: | per grammar |
The animation-delay and animation-range properties are both shorthands that set animation-delay-start and animation-delay-end together in a single declaration. If the <'animation-delay-end'> value is omitted, it is set to zero. If a <timeline-range-name> alone is specified, animation-delay-start is set to that name plus 0% and animation-delay-end is set to that name plus 100%.
Name: | animation-delay-start |
---|---|
Value: | [ <time> | <timeline-range-name> <percentage> ] |
Initial: | 0s |
Applies to: | all elements |
Inherited: | no |
Percentages: | relative to the specified named timeline range |
Computed value: | list, each item either a duration or a timeline range and progress percentage |
Canonical order: | per grammar |
Animatable: | no |
Defines a delay between the start of the animation and when it begins executing (i.e. when keyframes attached to 0% progress are mapped).
- <time>
- Defines the delay as a duration. See [CSS-ANIMATIONS-1].
- <timeline-range-name> <percentage>
- Calculates the delay as the difference between the start of the animation and the specified point on the timeline.
Name: | animation-delay-end |
---|---|
Value: | [ <time> | <timeline-range-name> <percentage> ] |
Initial: | 0s |
Applies to: | all elements |
Inherited: | no |
Percentages: | relative to the specified named timeline range |
Computed value: | list, each item either a duration or a timeline range and progress percentage |
Canonical order: | per grammar |
Animatable: | no |
Defines a delay between the end of the animation and when it ends executing (i.e. when keyframes attached to 100% progress are mapped). Values are as for animation-delay-start, but calculating backwards from the end, i.e. a 2s delay effectively shortens the animation duration, whereas -2s lengthens it.
Reporting Timeline Range Progress: the getTime() method
Progress through named ranges is exposed on the AnimationTimeline
object
by the getCurrentTime()
method:
[Exposed =Window ]interface {
AnimationTimeline readonly attribute CSSNumberish ?;
currentTime CSSNumericValue ?getCurrentTime (optional CSSOMString ); };
rangeName
getCurrentTime(optional rangeName)
method steps are:
-
If this is an inactive timeline, return null.
-
If rangeName is provided:
-
If rangeName is a valid named timeline range on this, let progress be the current progress through that range, expressed as a percentage value between 0 and 100.
Create a new unit value from (progress, "percent") and return it.
-
Otherwise, return null.
-
-
Let current time be the value of this’s
currentTime
internal slot.Create a new unit value from (current time, "ms") and return it.