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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219---title: View Transition Examplesdescription: Basic examples on how to use the View Transition API for same-document and cross-document view transitions.head: - tag: meta attrs: property: "og:image" content: "/examples-og.png"---
import DemoLink from "/src/components/DemoLink.astro";
This page references a series of view transition examples, starting with basic configurations and progressively incorporating more features. The tables below provide a brief description of what distinguishes each example and includes a link to the live example which also includes its code and a detailed explanation.
You should be familiar with the [default animations](/basics/default-animations/) inserted by the View Transition API.
The [first set of examples](#same-document-view-transition-examples) focuses on **exit and entry** animations for **same-document** view-transitions, animating a component directly on its page.
The [second set of examples](#cross-document-view-transition-examples) highlights **exit and entry** animations of **cross-document** view transitions. Those are triggered by following a link to a new page.
While the first two sets demonstrate exit and entry animations, the [Image Morph Set](#image-morph-examples) introduces **group/morph animations**, starting with **image morphs**, which tend to be easier compared to morphs involving text.
For morphing text with view transitions, we investigate two additional sets: the [Strange Artifacts Set](#strange-text-morph-examples), which identifies several common pitfalls, and the [Improved Set](#improved-text-morph-examples), which attempts to fix and improve these issues.
## Same-Document View Transition Examples
These examples focus on a tab component where you can switch between different content sections. When you click on the headers, the content in the main area of the tab changes, but the component itself remains in the same position. As a result, these examples demonstrate **exit and entry** animations of the content only.
<div title="Select a link from the table below"><a href="/demo/BasicS/"> <Card title="Same-Document 'Tabs' Example"> <style>{` .tabs { pointer-events: none; } .tab { display: inline-block; padding: 10px 20px; cursor: pointer; background-color: #887; border: 1px solid #887; border-bottom: none; } .tab.active { background-color: #797; border-bottom: 1px solid #797; } .tab-content { transform: translateY(-1rem); display: none; padding: 20px; border: 1px solid #888; } .tab-content.active { display: block; }`}</style> <div class="tabs" id="sd-example"> <div class="tab active" data-tab="tab1"> Dad Jokes </div> <div class="tab" data-tab="tab2"> Punny Puns </div> </div> <div class="tab-content active" id="tab1">
Why don't skeletons fight each other?\ They don't have the guts.
I'm reading a book on anti-gravity.\ It's impossible to put down! </div> </Card> </a></div>
| Name | Description ||---|---|| <DemoLink id="basics" href="/demo/BasicS/">Basic</DemoLink> | Just the default cross-fade transition of the whole document || <DemoLink id="basicas" href="/demo/BasicAS/">Custom Animation</DemoLink> | With a custom animation that shrinks the old content to a line before revealing the new one || <DemoLink id="basicms" href="/demo/BasicMS/">Multiple Transitions</DemoLink> | This animates the content area and headers with two independent animations |
## Cross-Document View Transition Examples
These examples focus on a picture component where you navigate through individual pages, each displaying a different image. On every page, both the picture and the button that advances to the next page remain in the same position. While they move during the transition, these are purely **exit and entry** animations, with no morphing involved yet.
<div title="Select a link from the table below"><a href="/demo/BasicC1/"> <Card title="Cross-Document 'Picture' Example"> <style>{` #image { display: block; margin: 0 auto; mask-image: radial-gradient( ellipse at center, white 35%, transparent 71% ); } #images { pointer-events: none; } `}</style> <div id="images"> <img id="image" width="200" height="200" src="/bag.webp" alt="The Bag of Tricks" /> </div> </Card> </a></div>
| Name | Description ||---|---|| <DemoLink id="basicc" href="/demo/BasicC1/">Basic</DemoLink> | Just the default cross-fade transition of the whole page || <DemoLink id="basicac" href="/demo/BasicAC1/">Custom Animation</DemoLink> | With a custom animation that shrinks the current image to a point before revealing the next image || <DemoLink id="basicmc" href="/demo/BasicMC1/">Multiple Transitions</DemoLink> | Adds a second animation that slides the navigation button |
## Image Morph Examples
Morphing thumbnails into hero images is a classic use case for view transitions. Morphing animations share the characteristic that the position and/or size of elements with the same view transition name differ before and after the transition. The animation visually connects the two, showing that the small thumbnail serves as a placeholder for the larger image.
<div title="Select a link from the table below"><a href="/demo/BasicIM/"> <Card title="Thumbnail to Detail Morphs"> <style>{`#one img{ width: 30%; height: auto;}#two img { width: 100%; height: auto;}@media (orientation: landscape) { #one { width: 40%; height: auto; } #two { width: 40%; height: auto; }}`}</style> <div style="display: flex; flex-wrap: wrap; align-items: center; justify-content: space-around"> <div id="one"> <div class="portrait"></div> </div> <div id="two"> <div class="portrait"></div> </div> </div> </Card> </a></div>
| Name | Description ||---|---|| <DemoLink id="basicim" href="/demo/BasicIM/">Same aspect ratio</DemoLink> | The default morph transition with same aspect-ratio images || <DemoLink id="basicim2" href="/demo/BasicIM2/">Different image</DemoLink> | The default morph transition with different images and different aspect-ratio || <DemoLink id="basicim3" href="/demo/BasicIM3/">Clipped overflow</DemoLink> | An alternative morph transition where images do not grow beyond the group outline || <DemoLink id="basicim4" href="/demo/BasicIM4/">Covered object fit</DemoLink> | A non-overflowing alternative for images with different aspect-ratio || <DemoLink id="basicim5" href="/demo/BasicIM5/">Changing aspect ratio</DemoLink> | A neat trick to morph the same image between different aspect ratios || <DemoLink id="failim" href="/demo/FailIM/">Surprising fade</DemoLink>| An unfortunate attempt that fails due to a typo |
## Strange Text Morph Examples
When it comes to group animations, text can be much trickier to work with compared to image morph transitions. Morphing text often results in some unexpected glitches. Below are some of the most common issues you're likely to encounter in your own projects.
<div title="Select a link from the table below"><a href="/demo/Fail1/"> <Card title="Morphing text into text"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span> </Card> </a></div>
| Name | Description ||---|---|| <DemoLink id="fail1" href="/demo/Fail1/">Inline to block morph</DemoLink> | A typical pitfall when morphing links to headings || <DemoLink id="fail2" href="/demo/Fail2/">Unintended ratio differences</DemoLink> | When both elements are inline and still differ in aspect ratio |
## Improved Text Morph Examples
Now that you've seen the potential pitfalls of morphing text in animations, you're probably eager to find out how to avoid these issues. Good news: there are some effective solutions you can apply to smooth out these rough edges and achieve cleaner transitions.
<div title="Select a link from the table below"><a href="/demo/Improved1/"> <Card title="Morphing text into text"><span class="light:sl-hidden"></span><span class="dark:sl-hidden"></span> </Card> </a></div>
| Name | Description ||---|---|| <DemoLink id="improved1" href="/demo/Improved1/">Inline to block morph</DemoLink> | A typical pitfall when moving links to headings |
import { Card } from "@astrojs/starlight/components";
<Card title="Side note"> Creating these example pages was a blast! They bring together a mix of Astro components, MDX partials, and endpoints. Best of all, with each example, you can simply hit <kbd>Ctrl-U</kbd> (or "view page source") to explore readable source code, free from all the typical clutter! And yes, these are still static pages generated with SSG.</Card>