Vue Transitions
When elements appear and disappear instantly, the interface can feel abrupt. Vue makes it easy to animate elements as they enter and leave the DOM with two built-in components:
<Transition>(<transition>in in-DOM templates) — animates a single element or component toggled withv-if,v-showor a changing:key.<TransitionGroup>(<transition-group>) — animates items in a list rendered withv-for, including moves.
Vue doesn't animate by itself: it adds and removes CSS classes at the right moments, and your CSS defines the animation.
The six transition classes
| Class | Applied |
|---|---|
v-enter-from |
Start state of entering (first frame only) |
v-enter-active |
During the whole enter phase — put transition here |
v-enter-to |
End state of entering |
v-leave-from |
Start state of leaving |
v-leave-active |
During the whole leave phase |
v-leave-to |
End state of leaving |
With <transition name="fade"> the prefix v- becomes fade-, e.g. fade-enter-from.
Fade in and out
<style>
.fade-enter-active, .fade-leave-active { transition: opacity 0.5s ease; }
.fade-enter-from, .fade-leave-to { opacity: 0; }
.note { background: #ecfdf5; padding: 10px; margin-top: 8px; border-left: 4px solid #42b883; }
</style>
<div id="app">
<button @click="show = !show">Toggle tip</button>
<transition name="fade">
<p v-if="show" class="note">💡 Tip: transitions make UI changes easier to follow.</p>
</transition>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({ data: () => ({ show: true }) }).mount('#app');
</script>Only the two "from/to" states need declaring; the -active class holds the transition property. The element's normal styles are the other end of the animation.
Slide and different enter/leave styles
Enter and leave can be different. Here the panel slides down when it appears and fades upward when it leaves:
<style>
.slide-enter-active { transition: all 0.4s ease-out; }
.slide-leave-active { transition: all 0.25s ease-in; }
.slide-enter-from { transform: translateY(-20px); opacity: 0; }
.slide-leave-to { transform: translateY(-10px); opacity: 0; }
.menu { background: #1f2937; color: white; padding: 10px; width: 180px; border-radius: 6px; margin-top: 6px; }
</style>
<div id="app">
<button @click="open = !open">☰ Menu</button>
<transition name="slide">
<div v-show="open" class="menu">Home<br>Shop<br>Contact</div>
</transition>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({ data: () => ({ open: false }) }).mount('#app');
</script><transition> works with v-show too — the element is hidden after the leave animation ends.
Switching between elements
Give each element a different :key (or use v-if/v-else), and mode="out-in" so the old element leaves before the new one enters:
<style>
.swap-enter-active, .swap-leave-active { transition: all 0.25s; }
.swap-enter-from { opacity: 0; transform: translateX(20px); }
.swap-leave-to { opacity: 0; transform: translateX(-20px); }
.big { font-size: 40px; display: inline-block; }
</style>
<div id="app">
<button @click="i = (i + 1) % weather.length">Next day</button>
<p>
<transition name="swap" mode="out-in">
<span class="big" :key="i">{{ weather[i] }}</span>
</transition>
</p>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({ data: () => ({ i: 0, weather: ['☀️', '⛅', '🌧️', '⛈️', '🌈'] }) }).mount('#app');
</script>Animating lists with TransitionGroup
<transition-group> renders a real element (set with tag), and every child must have a unique key. Besides enter/leave, the v-move class (list-move here) animates items sliding to their new positions.
<style>
ul { padding: 0; list-style: none; width: 220px; }
li { padding: 6px 10px; margin: 4px 0; background: #ecfdf5; border-radius: 4px; display: flex; justify-content: space-between; }
.list-move, .list-enter-active, .list-leave-active { transition: all 0.4s ease; }
.list-enter-from, .list-leave-to { opacity: 0; transform: translateX(30px); }
.list-leave-active { position: absolute; } /* lets the others move smoothly */
</style>
<div id="app">
<input v-model="text" @keyup.enter="add" placeholder="New item + Enter">
<button @click="shuffle">Reverse</button>
<button @click="sortAZ">Sort A–Z</button>
<transition-group name="list" tag="ul">
<li v-for="item in items" :key="item.id">
{{ item.text }} <button @click="remove(item.id)">✕</button>
</li>
</transition-group>
</div>
<script type="module">
import { createApp, ref } from 'vue';
createApp({
setup() {
let nextId = 4;
const text = ref('');
const items = ref([
{ id: 1, text: 'Banana' }, { id: 2, text: 'Apple' }, { id: 3, text: 'Cherry' }
]);
const add = () => {
if (!text.value) return;
items.value.unshift({ id: nextId++, text: text.value });
text.value = '';
};
const remove = (id) => { items.value = items.value.filter(i => i.id !== id); };
const shuffle = () => { items.value = [...items.value].reverse(); };
const sortAZ = () => { items.value = [...items.value].sort((a, b) => a.text.localeCompare(b.text)); };
return { text, items, add, remove, shuffle, sortAZ };
}
}).mount('#app');
</script>JavaScript hooks and other options
| Feature | Example |
|---|---|
| Animate on first render | <transition appear> |
Use CSS @keyframes |
.bounce-enter-active { animation: bounce-in .5s; } |
| Custom class names (e.g. Animate.css) | enter-active-class="animate__animated animate__tada" |
| Explicit duration | :duration="{ enter: 500, leave: 800 }" |
| JavaScript hooks | @before-enter, @enter(el, done), @after-leave… |
Tip: Animate
opacityandtransformwhenever possible. Browsers can animate them on the GPU, while properties likeheight,topormarginforce layout recalculations and can stutter.
Note: Respect users who prefer less motion: wrap animations in
@media (prefers-reduced-motion: no-preference) { … }or shorten them underprefers-reduced-motion: reduce.
Test yourself
With <Transition name="fade">, which class defines the starting state of an entering element?
fade-enter-activefade-enter-fromfade-inv-enter-to
What does mode="out-in" do?
- Plays both animations at the same time
- The old element leaves first, then the new one enters
- Only animates leaving
- Reverses the animation
What is required for every child of a <TransitionGroup>?
- A
ref - A unique
key - A
v-show - A
nameattribute