--- layout: "@/layouts/ExplainerLayout.astro" demoTitle: "View Transition Example: Custom Animation, Page 1 of 2" demoText: "A custom cross-document view transition animation that shrinks the old image on exit and enlarges the new image on entry." title: "How does it work?" --- ### Goal We want to shrink the current image as the new one grows in. The smaller the images are, the lower their opacity should be. When both images reach the midpoint of their transition, the animation should pause briefly. ### Changes We built on the [former example showing default animations](/demo/BasicC1/). The extensions are CSS only. #### View Transition Names We do not want to shrink the whole viewport but only the image. Therefore the first thing is to assign a view transition name to the image. We could add the property using inline styling to the `` element, but also with a new rule like this ```html ins={1-3} h2 + img { view-transition-name: img } ``` This has to be done for the `` on the second page, too. #### Custom Animations For the shrinking and growing, we use the same `zoom-out` keyframes, but for the zoom-in effect we run them in reveres order. The `forwards` definition says that the old image should stay hidden at the end of the transition. Both animation run for a second in parallel without any delays. ```css ins={1-6} ::view-transition-old(img) { animation-name: zoom-out; animation-duration: 1s; animation-fill-mode: forwards; } ::view-transition-new(img) { animation-name: zoom-out; animation-duration: 1s; animation-direction: reverse; } ``` #### Keyframes The effect gradually decreases the opacity and scale of the old image from 1 to 0, while simultaneously increasing the opacity and scale of the new image from 0 to 1. There's a plateau between 0.4s and 0.6s where all values are halfway. ```css ins={1-14} @keyframes zoom-out { 40% { transform: scale(0.5); opacity: 0.5; } 60% { transform: scale(0.5); opacity: 0.5; } 100% { transform: scale(0); opacity: 0; } } ``` ## Inspection Chamber Insights When we select the Full Control Mode of the Chamber we can check the definitions of the animations and have a closer look on how the animations behave.
### Animations First we take a look at the animations of the `img` view transition. There is an auto-generated animation for the group and our two animations for the exit and enter animations. The auto-generated `-ua-view-transition-group-anim-img` is not interesting in our case. It move the images from the old position to the new one, which are both the same. We expand our zoom animations and see how the browser automatically filled in the opacity and transformation values for the 0% position. We also see some animation defaults, which we haven't set explicitly like the ease timing function or the 0s duration-delay.
![Animation details](_ACan-dark.png) ![Animation details](_ACan-light.png)
### Images Now we want to have a detailed look at the images as they transition in and out.
![img at 0](_AC0000-dark.png) ![img at 0](_AC0000-light.png) t = 0 ms
![img at 150](_AC0150-dark.png) ![img at 150](_AC0150-light.png) t = 150 ms
![img at 250](_AC0250-dark.png) ![img at 250](_AC0250-light.png) t = 250 ms
![img at 333](_AC0333-dark.png) ![img at 333](_AC0333-light.png) t = 333 ms
![img at 500](_AC0500-dark.png) ![img at 500](_AC0500-light.png) t = 400 ms — 600 ms
![img at 666](_AC0666-dark.png) ![img at 666](_AC0666-light.png) t = 666 ms
![img at 750](_AC0750-dark.png) ![img at 750](_AC0750-light.png) t = 750 ms
![img at 850](_AC0850-dark.png) ![img at 850](_AC0850-light.png) t = 850 ms
![img at 1000](_AC1000-dark.png) ![img at 1000](_AC1000-light.png) t = 1 s
The yellow frame shows the `::view-transition-group(img)` pseudo element. It does not change through the animation. The `::view-transition-old(img)` has a purple frame, the `::view-transition-new(img)`'s frame is green. From _t = 400 ms_ to _t = 600 ms_ both images occupy the same space and their frames coincide. Both are half size and half opaque.