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+
1820const 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