Skip to content

Commit 633e041

Browse files
committed
Demo: Scroll-Driven View Transitions: Optimize some bits
1 parent c196021 commit 633e041

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

demo/scroll-driven-view-transition/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<meta name="viewport" content="width=device-width, initial-scale=1.0">
1212
<title>Scroll-Driven View Transition</title>
1313
<link rel="stylesheet" href="styles.css">
14-
<script type="module" src="scripts.js"></script>
14+
<script type="module" src="scripts.js" blocking="render" async></script>
1515
</head>
1616

1717
<body>

demo/scroll-driven-view-transition/scripts.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { trackActiveViewTransition } from "../js/track-active-view-transition.js";
7-
import { getAnimations } from "../js/animations.js";
8-
import { pause, scrub } from "../js/playback-control.js";
9-
10-
trackActiveViewTransition();
11-
126
// This demo is a scroll-driven view transition demo, which starts a View Transition
137
// to adjust the size of the card when scrolling up and down.
148

159
// It starts a VT that remains active and updates the VT’s animations based on the scroll position via scrollTop.
1610
// It re-initializes everything on resize, as ongoing VTs get cancelled on resize.
1711
// It only creates the VT when in range.
12+
13+
import { trackActiveViewTransition } from "../js/track-active-view-transition.js";
14+
import { getAnimations } from "../js/animations.js";
15+
import { pause, scrub } from "../js/playback-control.js";
16+
17+
// Make sure document.activeViewTransition is tracked
18+
trackActiveViewTransition();
19+
1820
const go = () => {
19-
let activeAnimations = [];
21+
let isReverse = false; // Boolean that keeps track if the animation are playing forwards or in reverse.
2022

2123
const $card = document.querySelector(".card");
2224
const $tracks = document.querySelector(".tracks");
@@ -47,22 +49,25 @@ const go = () => {
4749
// Method that starts the View Transition
4850
const startViewTransition = async () => {
4951
// Determine if we are going back or not
50-
const isReverse = document.querySelector(".small") ? true : false;
52+
isReverse = document.querySelector(".small") ? true : false;
5153

5254
// Start the View Transition
5355
document.startViewTransition(() => {
5456
document.querySelector(".card").classList.toggle("small");
5557
});
5658
await document.activeViewTransition.ready;
5759

58-
// Immediately pause all animations linked to it
59-
activeAnimations = getAnimations(document.activeViewTransition);
60-
for (const anim of activeAnimations) {
61-
if (isReverse) anim.reverse();
60+
// Reverse the individual animations if needed
61+
if (isReverse) {
62+
for (const anim of getAnimations(document.activeViewTransition)) {
63+
anim.reverse();
64+
}
6265
}
66+
67+
// Immediately pause all animations linked to the View Transition
6368
pause(document.activeViewTransition);
6469

65-
// Make sure animations their currentTime is up-to-date
70+
// Make sure all animations their currentTime is up-to-date
6671
updateAnimations();
6772

6873
// The VT finishes when all the animations have reached 100%,
@@ -80,15 +85,14 @@ const go = () => {
8085
// Method that updates the tracked animations
8186
const updateAnimations = () => {
8287
// No need to do anything when there are no animations being tracked
83-
if (!activeAnimations.length || !document.activeViewTransition) return;
88+
if (!document.activeViewTransition) return;
8489

90+
// Determine scroll Progress (ranging from 0 to 1)
8591
const scrollProgress =
8692
(document.documentElement.scrollTop - scrollTimelineStart) /
8793
scrollDistance;
8894

89-
// Some animations might be playing backwards (when scrolling back up)
90-
const isReverse = activeAnimations[0]?.playbackRate === -1;
91-
95+
// Scrub The View Transition
9296
scrub(
9397
document.activeViewTransition,
9498
isReverse ? 1 - scrollProgress : scrollProgress,

0 commit comments

Comments
 (0)