Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vtbag/website. The @vtbag website. ⭐️ Please star to support this work! ⭐️
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161---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 NamesWe 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 `<img>` 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 `<img>` 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;}```
#### KeyframesThe 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.
<div style="display:flex; flex-wrap:wrap; justify-content: space-;"><div class="item" style="min-width: 300px;">
### AnimationsFirst 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.
</div><div class="item">
<span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span>
</div></div>
<style>{`.item { flex: 1;}.item img { width: 300px;}`}</style>
### Images
Now we want to have a detailed look at the images as they transition in and out.
<div style="display: flex; flex-wrap: wrap; justify-content: space-around;">
<div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 0 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 150 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 250 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 333 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 400 ms — 600 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 666 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 750 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 850 ms</span></div><div class="imgItem"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span><span class="t">t = 1 s</span></div>
</div><style>{`.imgItem img { width: 150px; }.imgItem:nth-child(5) { flex: 1 1 100%}.t { text-align: center; display: block; color: #888; font-size: 0.9rem; transform: translateY(-20px)}`}</style>
The <span style="background-color: darkgoldenrod; color:#000">yellow</span> frame shows the `::view-transition-group(img)` pseudo element. It does not change through the animation. The `::view-transition-old(img)` has a <span style="background-color: darkslateblue; color:#fff">purple</span> frame, the `::view-transition-new(img)`'s frame is <span style="background-color: darkolivegreen; color:#fff">green</span>. 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.