This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Overview.bs
829 lines (647 loc) · 29.5 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
<link href='web-animations.css' rel='stylesheet' type='text/css'>
<pre class='metadata'>
Title: Scroll-linked Animations
Group: CSSWG
Status: UD
Work Status: exploring
ED: https://wicg.github.io/scroll-animations/
Shortname: scroll-animations
Abstract: Defines an API and markup for creating animations that are either
triggered by or tied to the scroll offset of a scroll container.
Editor: Brian Birtles <bbirtles@mozilla.com>
Editor: Botond Ballo <botond@mozilla.com>
Editor: Stephen McGruer <smcgruer@google.com>
Editor: Antoine Quint <graouts@apple.com>
Former editor: Mantaroh Yoshinaga
!Issue Tracking: <a href="https://github.com/WICG/scroll-animations/issues">GitHub Issues</a>
</pre>
<pre class=anchors>
urlPrefix: https://w3c.github.io/web-animations/; type: dfn; spec: web-animations
text: animation; url: concept-animation
text: current time
text: default document timeline
text: duration
text: inactive timeline
text: start delay
text: target effect end
text: timeline
urlPrefix: https://drafts.csswg.org/cssom-view/; type: dfn; spec: cssom-view-1
text: CSS layout box
urlPrefix: https://html.spec.whatwg.org/multipage/browsers.html; type: dfn; spec: html
text: document associated with a window; url: concept-document-window
</pre>
<pre class=link-defaults>
spec:html; type:dfn; for:/; text:browsing context
</pre>
# Introduction # {#intro}
This specification defines mechanisms for
[[#scroll-driven-animations|driving the progress of an animation]] based
on the scroll progress of a scroll container.
## Relationship to other specifications ## {#other-specs}
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 timeline=].
This specification defines both programming interfaces for interacting with these
concepts, as well as CSS markup which applies these concepts to CSS Animations
[[CSS3-ANIMATIONS]].
The behavior of the CSS markup is described in terms of the programming interfaces.
User agents that do not support script may still implement the CSS markup
provided it behaves as if the underlying programming interfaces were in place.
## Relationship to asynchronous scrolling ## {#async-scrolling}
Some user agents support scrolling that's 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.
# Use cases # {#use-cases}
<em>This section is non-normative</em>
Note: Based on this <a
href="https://github.com/WICG/scroll-animations/blob/master/Use%20cases.md">curated
list of use cases</a>.
## Scroll-triggered animations ## {#scroll-triggered-animations-usecases}
### Navigation bar shrinking effect ### {#navigation-bar-shrinking-effect}
It is common to trigger an animation to run when the scroll position
reaches a certain point. For example, a navigation bar may shrink once
the user begins to scroll a page.
<figure>
<img src="img/usecase1-2.svg" width="600"
alt="Use case: Shrinking navigation bar">
<figcaption>
Shrinking navigation bar<br>
The left figure shows the navigation bar before scrolling with
a large menu bar.<br>
The right figure shows the shrunken navigation bar after scrolling.
</figcaption>
</figure>
Advisement: The proposal does not yet define CSS markup or programming
interfaces to express this use case.
### Navigation highlight effect ### {#navigation-highlight-effect}
Similarly, it is common to trigger an animation at certain fixed points
in a element's scroll range. For example, a navigation bar that changes
highlight based on the reader's position within the document.
<figure>
<img src="img/usecase1-1.svg" width="600"
alt="Use case: A navigation highlight effect.">
<figcaption>
A navigation highlight effect<br>
On the left, the “Abstract” section is scrolled into view
and hence the abstract menu item is highlighted.<br>
After scrolling down to the “Background” section (right),
the background menu item fades in while the abstract menu item fades
out.
</figcaption>
</figure>
Advisement: The proposal does not yet define CSS markup or programming
interfaces to express this use case.
## Scroll-triggered style changes ## {#scroll-triggered-style-changes-usecase}
Advisement: The proposal does not yet define CSS markup or programming
interfaces to express this use case.
## Scroll-linked animations ## {#scroll-linked-animations-usecase}
### Scrollable picture-story show ### {#scrollable-animation-usecase}
Another pattern is an animation that tells a story where the user
controls the progress of the animation by scrolling or some other
gesture. This may be because the animation contains a lot of textual
information which the user may wish to peruse more slowly, it may be for
accessibility considerations to accommodate users who are uncomfortable
with rapid animation, or it may simply be to allow the user to easily
return to previous parts of the story such as a story that introduces
a product where the user wishes to review previous information.
The following (simplified) example shows two balls colliding. The
animation is controlled by scroll position allowing the user to easily
rewind and replay the interaction.
<figure>
<img src="img/usecase3-1.svg" width="600"
alt="Use case: The picture-story show.">
<figcaption>
A scrollable movie.<br>
The left figure shows the initial position of the balls<br>
The right figure shows them after they have collided.
</figcaption>
</figure>
Using the CSS markup:
<pre class='lang-css'>
@media (prefers-reduced-motion: no-preference) {
div.circle {
animation-duration: 1s;
animation-timing-function: linear;
animation-timeline: scroll(element(#container), vertical, "200px", "300px");
}
#left-circle {
animation-name: left-circle;
}
#right-circle {
animation-name: right-circle;
}
#union-circle {
animation-name: union-circle;
animation-timeline: scroll(element(#container), vertical, "250px", "300px");
}
@keyframes left-circle {
to { transform: translate(300px) }
}
@keyframes right-circle {
to { transform: translate(350px) }
}
@keyframes union-circle {
to { opacity: 1 }
}
}
</pre>
Using the programming interface, we might write this as:
<pre class='lang-javascript'>
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
var circleTimeline = new ScrollTimeline({
scrollSource: scrollableElement,
scrollOffset: '200px',
endScrollOffset: '300px'
});
var left = leftCircle.animate({ transform: 'translate(300px)' }, 1000);
left.timeline = circleTimeline;
var right = leftCircle.animate({ transform: 'translate(350px)' }, 1000);
right.timeline = circleTimeline;
var union = unionCircle.animate({ opacity: 1 }, 1000);
union.timeline = new ScrollTimeline({
scrollSource: scrollableElement,
startScrollOffset: '250px',
endScrollOffset: '300px'
});
}
</pre>
### The content progress bar ### {#content-progress-bar-usecase}
Another common example of an animation that tracks scroll position is a
progress bar that is used to indicate the reader's position in a long
article.
<figure>
<img src="img/usecase3-2.svg" width="600"
alt="Use case: Scroll based styling">
<figcaption>
Content progress bar.<br>
The left figure shows the initial state before scrolling.<br>
The right figure shows the progress bar is half-filled in since the
user has scrolled half way through the article.
</figcaption>
</figure>
Typically, the scroll bar provides this visual indication but
applications may wish to hide the scroll bar for aesthetic or useability
reasons.
Using the 'animation-timeline' property, this example could be written
as follows:
<pre class='lang-css'>
@media (prefers-reduced-motion: no-preference) {
@keyframes progress {
to { width: 100%; }
}
#progress {
width: 0px;
height: 30px;
background: red;
animation: progress 1s linear;
animation-timeline: scroll(element(#body));
}
}
</pre>
If we use this API for this case, the example code will be as follow:
<pre class='lang-javascript'>
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
var animation = div.animate({ width: '100%' }, 1000);
animation.timeline = new ScrollTimeline(
{ startScrollOffset: '0px' }
);
}
</pre>
## Combination scroll and time-base animations ## {#combination-scroll-and-time-base-animations-usecase}
### Photo viewer ### {#movie-show-case-usecase}
Advisement: We are currently reworking this use case
<!--
Maybe the developer will want to use the scroll based timeline and the time-based timeline.
Here's an example content which showing the photos.
If scroll position is out of specified range, the animation of the slideshow will start. The progress of this slideshow is related to scroll volume. And if scroll position is within the specified range, the animation of the slideshow will continue automatically.
<figure>
<img src="img/usecase4.svg" width="600"
alt="Use case 4: Scrollable slide show.">
<figcaption>
Use case 4: Scrollable slide show.<br>
The left figure is before scroll, the slide show will start as scroll-linked animation.<br>
The right figure is after scroll, the slide show will start as related to the time animation.
</figcaption>
</figure>
This content can't build the CSS only.
<pre class='lang-javascript'>
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
var animation = slideTarget.getAnimation()[0];
var scrollTimeline = new ScrollTimeline({
scrollSource: scrollableElement,
orientation: "vertical",
scrollOffset: '0px',
endScrollOffset: '200px'
});
animation.timeline = scrollTimeline;
// We use scroll event in order to change the timeline.
scrollableElement.addEventListener("scroll", function(evt) {
if ((scrollableElement.scrollTop > 200) && animation.timeline != document.timeline) {
animation.timeline = document.timeline;
} else if ((scrollableElement.scrollTop < 200) && animation.timeline == document.timeline) {
animation.timeline = scrollTimeline;
}
});
}
</pre>
-->
</div>
# Scroll-driven animations # {#scroll-driven-animations}
## Scroll timelines ## {#scroll-timelines}
### The {{ScrollDirection}} enumeration ### {#scrolldirection-enumeration}
<pre class="idl">
enum ScrollDirection {
"block",
"inline",
"horizontal",
"vertical"
};
</pre>
The {{ScrollDirection}} enumeration specifies a direction of scroll of a
scrollable element.
: <code>block</code>
:: Selects the direction along the [=block axis=], conforming to writing mode
and directionality.
: <code>inline</code>
:: Selects the direction along the [=inline axis=], confirming to writing mode
and directionality.
: <code>horizontal</code>
:: Selects the physical horizontal direction (ignoring writing mode and
directionality).
: <code>vertical</code>
:: Selects the physical vertical direction (ignoring writing mode and
directionality).
Note: Having both logical (block/inline) and physical (vertical/horizontal)
directions allows web developers to animate both logical (e.g.
margin-inline-start) and physical (e.g. transform) properties with good
behavior under different directionalities and writing modes.
### The {{ScrollTimeline}} interface ### {#scrolltimeline-interface}
<pre class="idl">
enum ScrollTimelineAutoKeyword { "auto" };
dictionary ScrollTimelineOptions {
Element? scrollSource = null;
ScrollDirection orientation = "block";
DOMString startScrollOffset = "auto";
DOMString endScrollOffset = "auto";
(double or ScrollTimelineAutoKeyword) timeRange = "auto";
FillMode fill = "none";
};
[Exposed=Window,
Constructor(optional ScrollTimelineOptions options = {})]
interface ScrollTimeline : AnimationTimeline {
readonly attribute Element scrollSource;
readonly attribute ScrollDirection orientation;
readonly attribute DOMString startScrollOffset;
readonly attribute DOMString endScrollOffset;
readonly attribute (double or ScrollTimelineAutoKeyword) timeRange;
readonly attribute FillMode fill;
};
</pre>
A <dfn>scroll timeline</dfn> is an {{AnimationTimeline}} whose time values are determined
not by wall-clock time, but by the progress of scrolling in a [=scroll container=].
<div link-for-hint="ScrollTimeline">
<div class="constructors">
: <dfn constructor for=ScrollTimeline lt="ScrollTimeline(options)">ScrollTimeline(options)</dfn>
:: Creates a new {{ScrollTimeline}} object using the following procedure:
1. Let |timeline| be a new {{ScrollTimeline}} object.
1. Let |source| be the result corresponding to the first matching condition from below.
: If the |scrollSource| value of |options| is non-null,
:: Let |source| be |scrollSource|
: Otherwise (|scrollSource| is null):
:: Let |source| be the {{scrollingElement}} of the {{Document}} <a lt="document associated with a window">associated</a> with the {{Window}} that is the <a>current global object</a>.
Note: |source| may still be null after this step, e.g. if the {{Document}} has no {{scrollingElement}}.
1. Set the {{ScrollTimeline/scrollSource}} of |timeline| to |source|.
1. Assign the {{ScrollTimeline/orientation}}, {{ScrollTimeline/startScrollOffset}}, {{ScrollTimeline/endScrollOffset}}, {{ScrollTimeline/timeRange}}, and {{ScrollTimeline/fill}} properties of |timeline| to the corresponding value from |options|.
</div>
<div class="attributes">
: <dfn attribute for=ScrollTimeline>scrollSource</dfn>
:: The scrollable element whose scrolling triggers the activation and drives the
progress of the timeline.
: <dfn attribute for=ScrollTimeline>orientation</dfn>
:: Determines the direction of scrolling which triggers the activation and drives
the progress of the trigger.
: <dfn attribute for=ScrollTimeline>startScrollOffset</dfn>
:: A scroll offset, in the direction specified by {{orientation}}, which constitutes
the beginning of the range in which the timeline is active.
Recognized values are defined by the following grammar:
<blockquote>
<pre class="prod">auto | <<length>> | <<percentage>></pre>
</blockquote>
The meaning of each value is as follows:
: auto
:: The beginning of {{scrollSource}}'s scroll range in {{orientation}}.
: <<length>>
:: An absolute distance along {{scrollSource}}'s scroll range in {{orientation}}.
: <<percentage>>
:: A percentage distance along {{scrollSource}}'s scroll range in {{orientation}}.
: <dfn attribute for=ScrollTimeline>endScrollOffset</dfn>
:: A scroll offset, in the direction specified by {{orientation}}, which constitutes
the end of the range in which the trigger is activated.
Recognized values are defined by the following grammar:
<blockquote>
<pre class="prod">auto | <<length>> | <<percentage>></pre>
</blockquote>
The meaning of each value is as follows:
: auto
:: The end of {{scrollSource}}'s scroll range in {{orientation}}.
: <<length>>
:: An absolute distance along {{scrollSource}}'s scroll range in {{orientation}}.
: <<percentage>>
:: A percentage distance along {{scrollSource}}'s scroll range in {{orientation}}.
: <dfn attribute for=ScrollTimeline>timeRange</dfn>
:: A time duration that allows mapping between a distance scrolled, and
quantities specified in time units, such as an animation's [=duration=] and
[=start delay=].
Conceptually, {{timeRange}} represents the number of milliseconds to map to the
scroll range defined by {{startScrollOffset}} and {{endScrollOffset}}. As a
result, this value does not have a correspondence to wall-clock time.
This value is used to compute the timeline's [=effective time range=], and
the mapping is then defined by mapping the scroll distance from
{{startScrollOffset}} to {{endScrollOffset}}, to the [=effective time range=].
: <dfn attribute for=ScrollTimeline>fill</dfn>
:: Determines whether the timeline is active even when the scroll offset is outside
the range defined by [{{startScrollOffset}}, {{endScrollOffset}}].
Possible values are:
: none
:: The timeline is inactive when the scroll offset is less than {{startScrollOffset}}
or greater than or equal to {{endScrollOffset}}.
: forwards
:: When the scroll offset is less than {{startScrollOffset}}, the
timeline is inactive.
When the scroll offset is greater than or equal to the
{{endScrollOffset}}, the timeline's [=current time=] is its
[=effective time range=].
: backwards
:: When the scroll offset is less than {{startScrollOffset}}, the
timeline's [=current time=] is 0.
When the scroll offset is greater than or equal to the
{{endScrollOffset}}, the timeline is inactive.
: both
:: When the scroll offset is less than {{startScrollOffset}}, the
timeline's [=current time=] is 0.
When the scroll offset is greater than or equal to the
{{endScrollOffset}}, the timeline's [=current time=] is its
[=effective time range=].
: auto
:: Behaves the same as <code>both</code>.
</div>
### The effective time range of a {{ScrollTimeline}} ### {#efffective-time-range-algorithm}
The <dfn>effective time range</dfn> of a {{ScrollTimeline}} is calculated as follows:
<div class="switch">
: If the {{timeRange}} has the value <code>"auto"</code>,
:: The [=effective time range=] is the maximum value of the
[=target effect end=] of all animations
directly associated with this timeline.
If any animation directly associated with the timeline has a
[=target effect end=] of infinity, the [=effective time range=]
is zero.
: Otherwise,
:: The [=effective time range=] is the {{ScrollTimeline}}'s
{{timeRange}}.
</div>
### The current time of a {{ScrollTimeline}} ### {#current-time-algorithm}
The [=current time=] of a {{ScrollTimeline}} is calculated
as follows:
1. If {{scrollSource}} is null, does not currently have a [=CSS layout box=], or
if its layout box is not a [=scroll container=], return an unresolved time value.
1. Otherwise, let <var>current scroll offset</var> be the current scroll offset of {{scrollSource}}
in the direction specified by {{orientation}}.
1. If <var>current scroll offset</var> is less than {{startScrollOffset}}, return an unresolved
time value if {{fill}} is <code>none</code> or <code>forwards</code>, or 0 otherwise.
1. If <var>current scroll offset</var> is greater than or equal to {{endScrollOffset}} then:
<div class="switch">
: If {{endScrollOffset}} is less than the maximum scroll offset of {{scrollSource}} in
{{orientation}} and {{fill}} is <code>none</code> or <code>backwards</code>,
:: return an unresolved time value.
: Otherwise,
:: return the [=effective time range=].
</div>
Note: Checking for {{endScrollOffset}} being the maximum scroll offset ensures that the common
case of a 'whole scroller' ScrollTimeline does not become inactive when you scroll to the end.
1. Return the result of evaluating the following expression:
<blockquote>
<code>(<var>current scroll offset</var> - {{startScrollOffset}}) / ({{endScrollOffset}} - {{startScrollOffset}}) × [=effective time range=]</code>
</blockquote>
</div> <!-- link-for-hint="ScrollTimeline" -->
## The 'animation-timeline' property ## {#animation-timeline}
A {{ScrollTimeline}} may be applied to a CSS Animation [[CSS3-ANIMATIONS]] using
the 'animation-timeline' property.
<pre class='propdef'>
Name: animation-timeline
Value: <<single-animation-timeline>>#
Initial: auto
Applies to: all elements, ::before and ::after pseudo-elements
Inherited: none
Animatable: no
Percentages: N/A
Media: interactive
Computed value: As specified
Canonical order: per grammar
</pre>
<dfn><single-animation-timeline></dfn> = auto | scroll([element(<<id-selector>>)[, <<scroll-direction>>[, <<scroll-offset>>[, <<scroll-offset>>[, <<time>>[, <<single-animation-fill-mode>>]]]]]])
<dfn><scroll-direction></dfn> = auto | block | inline | horizontal | vertical
<dfn><scroll-offset></dfn> = <<length>> | <<percentage>> | auto
The 'animation-timeline' property is similar to properties like 'animation-duration' and
'animation-timing-function' in that it can have one or more values, each one imparting
additional behavior to a corresponding [=animation=] on the
element, with the timelines matched up with animations as described
[[css-animations-1#animation-name|here]].
<div link-for-hint="ScrollTimeline">
Each value has type <<single-animation-timeline>>, whose possible values have the
following effects:
: auto
:: The animation's [=timeline=] is a {{DocumentTimeline}}, more specifically the
<a>default document timeline</a>.
: scroll([element(<<id-selector>>)[, <<scroll-direction>>[, <<scroll-offset>>[, <<scroll-offset>>[, <<time>>[, <<single-animation-fill-mode>>]]]]]])
:: The animation's [=timeline=] is a {{ScrollTimeline}}.
The timeline's {{scrollSource}} is the [=scroll container=] identified
by the <<id-selector>>, defaulting to the element's nearest scrollable ancestor.
The <<scroll-direction>>, if provided, determines the timeline's {{orientation}}.
The first <<scroll-offset>>, if provided, determines the timeline's {{startScrollOffset}}.
The second <<scroll-offset>>, if provided, determines the timeline's {{endScrollOffset}}.
The <<time>> value, if specified, determines the timeline's {{timeRange}}.
The <<single-animation-fill-mode>> value, if specified, determines the timeline's {{fill}}.
</div> <!-- link-for-hint="ScrollTimeline" -->
## Examples ## {#timeline-examples}
<div class="example">
Draw a reading progress bar along the top of the page as the user scrolls
<pre class="lang-css">
#progress {
position: fixed;
top: 0;
width: 0;
height: 2px;
background-color: red;
}
</pre>
<pre class="lang-javascript">
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
let progress = document.getElementById("progress");
let effect = new KeyframeEffect(
progress,
[
{ width: "0vw" },
{ width: "100vw" }
],
{
duration: 1000,
easing: "linear"
});
let timeline = new ScrollTimeline({
trigger: new ScrollTrigger({
scrollSource: document.documentElement,
orientation: "vertical",
kind: "range"
});
});
let animation = new Animation(effect, timeline);
animation.play();
}
</pre>
</div>
<div class="example">
The same thing with CSS, using 'animation-timeline'
<pre class="lang-css">
@media (prefers-reduced-motion: no-preference) {
@keyframes progress {
from {
width: 0vw;
}
to {
width: 100vw;
}
}
#progress {
position: fixed;
top: 0;
width: 0;
height: 2px;
background-color: red;
animation-name: progress;
animation-duration: 1s;
animation-timing-function: linear;
/* Assume the HTML element has id 'root' */
animation-timeline: scroll(element(#root), vertical);
}
}
</pre>
</div>
# Avoiding cycles with layout # {#avoiding-cycles}
The ability for scrolling to drive the progress of an animation, gives rise to
the possibility of <dfn>layout cycles</dfn>, 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 cycles, animations with a {{ScrollTimeline}} 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 may 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 -
when scrolling happens in response to input events, the rendered scroll offset
and the state of scroll-driven animations will be consistent in each frame.
User agents that composite frames asynchronously with respect to layout and/or
script may, at their discretion, sample scroll-driven animations once per
<em>composited</em> 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, <code>scroll</code> 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.
# Scroll-triggered (but time-driven) animations # {#scroll-triggered-animations}
An earlier draft of this proposal also provided for animations whose progress
was driven by time (as with existing animations), but whose activation was
triggered by scrolling past a certain scroll offset or into a given scroll range.
The main objective was to allow triggering the animation from the compositor
thread. (The objective of scroll-linked animations is to make sure that
the animation is in sync with the scroll position on each composited frame.
If the triggering doesn't happen on the compositor thread, then it's possible
that for a few frames the visual scroll position is such that the animation
should have started, but it has not in fact started yet because the main thread,
which is doing the triggering, is lagging behind.)
However, we found that in the vast majority of cases where a web author would
want to do this, they would want to do it for a CSS transition (as opposed to
a CSS animation). Unfortunately, it's not possible to trigger CSS transitions from
the compositor thread (because triggering a transition requires style resolution,
which cannot be performed on the compositor thread). Given the extent to which
triggering complicated the API, we decided it wasn't worth it if you can't use
it for transitions, so this feature was removed.
The design space for triggering animations is still open. We welcome input
on this subject.
<h2 id="appendix-a-considerations-for-security-and-privacy">Appendix A. Considerations for Security and Privacy</h2>
This appendix is <em>informative</em>.
There are no known security or privacy impacts of this feature.
The W3C TAG is developing a
<a href="https://www.w3.org/TR/security-privacy-questionnaire/">Self-Review Questionnaire: Security and Privacy</a>
for editors of specifications to informatively answer.
Per the <a href="https://www.w3.org/TR/security-privacy-questionnaire/#questions">Questions to Consider</a>
<ol>
<li>Does this specification deal with personally-identifiable information?
<p>No.</p>
</li>
<li>Does this specification deal with high-value data?
<p>No.</p>
</li>
<li>Does this specification introduce new state for an origin that persists across browsing sessions?
<p>No.</p>
</li>
<li>Does this specification expose persistent, cross-origin state to the web?
<p>No.</p>
</li>
<li>Does this specification expose any other data to an origin that it doesn’t currently have access to?
<p>No.</p>
</li>
<li>Does this specification enable new script execution/loading mechanisms?
<p>No.</p>
</li>
<li>Does this specification allow an origin access to a user’s location?
<p>No.</p>
</li>
<li>Does this specification allow an origin access to sensors on a user’s device?
<p>No.</p>
</li>
<li>Does this specification allow an origin access to aspects of a user’s local computing environment?
<p>No.</p>
</li>
<li>Does this specification allow an origin access to other devices?
<p>No.</p>
</li>
<li>Does this specification allow an origin some measure of control over a user agent’s native UI?
<p>No.</p>
</li>
<li>Does this specification expose temporary identifiers to the web?
<p>No.</p>
</li>
<li>Does this specification distinguish between behavior in first-party and third-party contexts?
<p>No.</p>
</li>
<li>How should this specification work in the context of a user agent’s "incognito" mode?
<p>No differently. The website should not be able to determine that the user is
in an "incognito" mode using scroll-linked animations.</p>
</li>
<li>Does this specification persist data to a user’s local device?
<p>No.</p>
</li>
<li>Does this specification have a "Security Considerations" and "Privacy Considerations" section?
<p>Yes.</p>
</li>
<li>Does this specification allow downgrading default security characteristics?
<p>No.</p>
</li>
</ol>