Skip to content

Commit 544a507

Browse files
Use prefers-reduced-motion: no-preference in examples (#45)
1 parent e1481e5 commit 544a507

1 file changed

Lines changed: 101 additions & 87 deletions

File tree

Overview.bs

Lines changed: 101 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -157,53 +157,57 @@ alt="Use case: The picture-story show.">
157157
Using the CSS markup:
158158

159159
<pre class='lang-css'>
160-
div.circle {
161-
animation-duration: 1s;
162-
animation-timing-function: linear;
163-
animation-timeline: scroll(element(#container), vertical, "200px", "300px");
164-
}
165-
#left-circle {
166-
animation-name: left-circle;
167-
}
168-
#right-circle {
169-
animation-name: right-circle;
170-
}
171-
#union-circle {
172-
animation-name: union-circle;
173-
animation-timeline: scroll(element(#container), vertical, "250px", "300px");
174-
}
175-
@keyframes left-circle {
176-
to { transform: translate(300px) }
177-
}
178-
@keyframes right-circle {
179-
to { transform: translate(350px) }
180-
}
181-
@keyframes union-circle {
182-
to { opacity: 1 }
160+
@media (prefers-reduced-motion: no-preference) {
161+
div.circle {
162+
animation-duration: 1s;
163+
animation-timing-function: linear;
164+
animation-timeline: scroll(element(#container), vertical, "200px", "300px");
165+
}
166+
#left-circle {
167+
animation-name: left-circle;
168+
}
169+
#right-circle {
170+
animation-name: right-circle;
171+
}
172+
#union-circle {
173+
animation-name: union-circle;
174+
animation-timeline: scroll(element(#container), vertical, "250px", "300px");
175+
}
176+
@keyframes left-circle {
177+
to { transform: translate(300px) }
178+
}
179+
@keyframes right-circle {
180+
to { transform: translate(350px) }
181+
}
182+
@keyframes union-circle {
183+
to { opacity: 1 }
184+
}
183185
}
184186
</pre>
185187

186188
Using the programming interface, we might write this as:
187189

188190
<pre class='lang-javascript'>
189-
var circleTimeline = new ScrollTimeline({
190-
scrollSource: scrollableElement,
191-
scrollOffset: '200px',
192-
endScrollOffset: '300px'
193-
});
194-
195-
var left = leftCircle.animate({ transform: 'translate(300px)' }, 1000);
196-
left.timeline = circleTimeline;
197-
198-
var right = leftCircle.animate({ transform: 'translate(350px)' }, 1000);
199-
right.timeline = circleTimeline;
200-
201-
var union = unionCircle.animate({ opacity: 1 }, 1000);
202-
union.timeline = new ScrollTimeline({
203-
scrollSource: scrollableElement,
204-
startScrollOffset: '250px',
205-
endScrollOffset: '300px'
206-
});
191+
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
192+
var circleTimeline = new ScrollTimeline({
193+
scrollSource: scrollableElement,
194+
scrollOffset: '200px',
195+
endScrollOffset: '300px'
196+
});
197+
198+
var left = leftCircle.animate({ transform: 'translate(300px)' }, 1000);
199+
left.timeline = circleTimeline;
200+
201+
var right = leftCircle.animate({ transform: 'translate(350px)' }, 1000);
202+
right.timeline = circleTimeline;
203+
204+
var union = unionCircle.animate({ opacity: 1 }, 1000);
205+
union.timeline = new ScrollTimeline({
206+
scrollSource: scrollableElement,
207+
startScrollOffset: '250px',
208+
endScrollOffset: '300px'
209+
});
210+
}
207211
</pre>
208212

209213
### The content progress bar ### {#content-progress-bar-usecase}
@@ -231,25 +235,29 @@ Using the 'animation-timeline' property, this example could be written
231235
as follows:
232236

233237
<pre class='lang-css'>
234-
@keyframes progress {
235-
to { width: 100%; }
236-
}
237-
#progress {
238-
width: 0px;
239-
height: 30px;
240-
background: red;
241-
animation: progress 1s linear;
242-
animation-timeline: scroll(element(#body));
238+
@media (prefers-reduced-motion: no-preference) {
239+
@keyframes progress {
240+
to { width: 100%; }
241+
}
242+
#progress {
243+
width: 0px;
244+
height: 30px;
245+
background: red;
246+
animation: progress 1s linear;
247+
animation-timeline: scroll(element(#body));
248+
}
243249
}
244250
</pre>
245251

246252
If we use this API for this case, the example code will be as follow:
247253

248254
<pre class='lang-javascript'>
255+
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
249256
var animation = div.animate({ width: '100%' }, 1000);
250257
animation.timeline = new ScrollTimeline(
251258
{ startScrollOffset: '0px' }
252259
);
260+
}
253261
</pre>
254262

255263
## Combination scroll and time-base animations ## {#combination-scroll-and-time-base-animations-usecase}
@@ -276,8 +284,9 @@ alt="Use case 4: Scrollable slide show.">
276284

277285
This content can't build the CSS only.
278286
<pre class='lang-javascript'>
287+
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
279288
var animation = slideTarget.getAnimation()[0];
280-
var scrollTimeline = new ScrollTimeline({
289+
var scrollTimeline = new ScrollTimeline({
281290
scrollSource: scrollableElement,
282291
orientation: "vertical",
283292
scrollOffset: '0px',
@@ -293,6 +302,7 @@ This content can't build the CSS only.
293302
animation.timeline = scrollTimeline;
294303
}
295304
});
305+
}
296306
</pre>
297307
-->
298308

@@ -619,52 +629,56 @@ following effects:
619629
}
620630
</pre>
621631
<pre class="lang-javascript">
622-
let progress = document.getElementById("progress");
623-
let effect = new KeyframeEffect(
624-
progress,
625-
[
626-
{ width: "0vw" },
627-
{ width: "100vw" }
628-
],
629-
{
630-
duration: 1000,
631-
easing: "linear"
632+
if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
633+
let progress = document.getElementById("progress");
634+
let effect = new KeyframeEffect(
635+
progress,
636+
[
637+
{ width: "0vw" },
638+
{ width: "100vw" }
639+
],
640+
{
641+
duration: 1000,
642+
easing: "linear"
643+
});
644+
let timeline = new ScrollTimeline({
645+
trigger: new ScrollTrigger({
646+
scrollSource: document.documentElement,
647+
orientation: "vertical",
648+
kind: "range"
649+
});
632650
});
633-
let timeline = new ScrollTimeline({
634-
trigger: new ScrollTrigger({
635-
scrollSource: document.documentElement,
636-
orientation: "vertical",
637-
kind: "range"
638-
})
639-
});
640-
let animation = new Animation(effect, timeline);
641-
animation.play();
651+
let animation = new Animation(effect, timeline);
652+
animation.play();
653+
}
642654
</pre>
643655
</div>
644656

645657
<div class="example">
646658
The same thing with CSS, using 'animation-timeline'
647659
<pre class="lang-css">
648-
@keyframes progress {
649-
from {
650-
width: 0vw;
660+
@media (prefers-reduced-motion: no-preference) {
661+
@keyframes progress {
662+
from {
663+
width: 0vw;
664+
}
665+
to {
666+
width: 100vw;
667+
}
651668
}
652-
to {
653-
width: 100vw;
669+
#progress {
670+
position: fixed;
671+
top: 0;
672+
width: 0;
673+
height: 2px;
674+
background-color: red;
675+
animation-name: progress;
676+
animation-duration: 1s;
677+
animation-timing-function: linear;
678+
/* Assume the HTML element has id 'root' */
679+
animation-timeline: scroll(element(#root), vertical);
654680
}
655681
}
656-
#progress {
657-
position: fixed;
658-
top: 0;
659-
width: 0;
660-
height: 2px;
661-
background-color: red;
662-
animation-name: progress;
663-
animation-duration: 1s;
664-
animation-timing-function: linear;
665-
/* Assume the HTML element has id 'root' */
666-
animation-timeline: scroll(element(#root), vertical);
667-
}
668682
</pre>
669683
</div>
670684

0 commit comments

Comments
 (0)