--- layout: "@/layouts/ExplainerLayout.astro" demoTitle: "View Transition Example: Multiple View Transitions, Page 1 of 2" demoText: "Demo of a cross-document view transition with multiple animations." title: "How does it work?" --- import { Steps } from "@astrojs/starlight/components"; ### Goal When the `Next` button is pressed, it should slide out to the left and the next instance should slide in from the right. ### Changes We built on the [former example showing one custom animation](/demo/BasicAC1/). The extensions are CSS only. #### View Transition Names As you might have guessed, the new animation needs a new view transition name. ```html ins={1-3} .nav-button { view-transition-name: nav; } ``` This also has to be done on the second page for the nav button there. #### Custom Animations The key is to imagine a canvas where the new image of the button starts one screen width to the right, outside the viewport. Then, both images, the old one and the new one, are slid to the left. This moves the old image off the screen while the new one slides into view. ```css ins={1-6} ::view-transition-old(nav) { animation-name: slideOut; animation-duration: 1s; animation-timing-function: ease-in-out; } ::view-transition-new(nav) { animation-name: slideIn; animation-duration: 1s; animation-timing-function: ease-in-out; } ``` We want the animation to take up some speed and than getting slower while falling into place. #### Keyframes The sliding animation is handled by two keyframes: The old image moves from its current position to a point that is 100% of the viewport's width to the left. Simultaneously, the new image slides in from a point that is 100% of the viewport's width to the right. ```css ins={1-10} @keyframes slideOut { to { transform: translateX(-100vw); } } @keyframes slideIn { from { transform: translateX(100vw); } } ``` ## Inspection Chamber Insights Does the button slide in front of or behind the picture? View transition groups stack according to the drawing order of their elements. Groups from the old page are rendered first, followed by those defined only on the new page. ### Examine the Stacking Order of View Transition Groups You can check the order by examining the pseudo elements of the view transition in the browser's DevTools. These elements are only visible during the typically very short execution of the view transition. However, you can use the inspection chamber to freeze the animation and examine the pseudo elements for as long as necessary. 1. Start the view transition in the inspection Chamber in Full Control mode 2. Click on any name in the name list 3. Paste to the console window of the browser's DevTools and press Enter. 4. Now in the "Elements" view of the DevTools, expand the `` element and the `::view-transition` pseudo element.
Old image of the button in front of the pseudo-elements of the picture.
Semi-transparent pseudo-elements of the picture in front of the old image of the button.