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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990---layout: "@/layouts/ExplainerLayout.astro"demoTitle: "View Transition Example: Custom Animation (Same-Document)"demoText: "This example defines a custom animation to transition between tabs."title: "How does it work?"---
### Goal
We want to animate the content area. When the view transition starts, the current content should shrink to a thin line and then the new content should come in expanding from zero to its full height.
### ChangesThe HTML code of this example is identical with the [example using default animations](/demo/BasicS/). The extensions are pure CSS.
#### View Transition Names
Because we do not want to animate the whole viewport in a single animation but rather the single content areas, we start with assigning view transition names to the two content areas.
```css ins={1-6}#tab1 { view-transition-name: tab1;}#tab2 { view-transition-name: tab2;}```
However, the default name "root" has not been removed: the headers will continue to change with the standard transition effect.
#### Custom Animations
Next thing is to define the animations: We shrink the old images until they are gone and after that we let the new images grow in. Note that shrinking and growing have a duration of 0.2s and 0.8s respectively, but growing only starts after a 0.2s delay. This way growing happens after shrinking.
```css ins={1-10}::view-transition-old(tab1),::view-transition-old(tab2) { animation-name: shrink; animation-duration: 0.2s; animation-fill-mode: forwards; transform-origin: top;}::view-transition-new(tab1),::view-transition-new(tab2) { animation-name: grow; animation-duration: 0.8s; animation-delay: 0.2s; animation-fill-mode: backwards; transform-origin: top;}```
The fill mode settings are also important in this example. To be honest, I usually set them to “both” if it is relevant. If all your animations start at the same time and have the same duration, you don't need to worry about it at all. However, in this example, the total duration of the animation is 1 second, and the two animations only take up a fraction of it each. Therefore, we instruct the shrink animation to leave the old tab invisible after the shrinking (forward), and we instruct the grow animation to be invisible from the beginning (backwards), although there is a delay of 0.2s before the animation starts.
#### Keyframes
Finally, the keyframes for shrinking and growing must be defined:
```css ins={1-10}@keyframes shrink { to { transform: scaleY(0); }}@keyframes grow { from { transform: scaleY(0); }}```
Where are the values for the start of shrinkage and the end of growth? These are implicit. The browser substitutes the current values, i.e. normal size or `scaleY(1)`.
## Inspection Chamber Insights
When inspecting a switch from `tab1` to `tab2` in The Chamber, looking at a time frame before 200 ms reveals that* `root` has an old an a new image and both are visible.* `tab1` only has an old image, which is visible right now. The only animation defined on this pseudo-element is `shrink`* `tab2` only has a new image, which is not yet visible.
<span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span>
When moving to a time frame beyond 200 ms, things change:* Now the old image of `tab1` is invisible and* the new image of `tab2` is visible and transformed by the `grow` animation
<span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span>