---
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.