Transition & Animation — Vue

<Transition name="fade"> wraps one element that appears and disappears through v-if, v-show or a changing :key. Vue adds the classes fade-enter-from…

Mrs. Sri has a small sign that says "Added to cart!". If the sign appears and disappears right away, customers never notice it. So Mrs. Sri lifts it slowly, holds it a moment, then lowers it slowly. She stores the sign only once it's all the way down.

In Vue, slow movement like the sign's is handled by <Transition> and <TransitionGroup>. <Transition> animates one element as it enters or leaves the page. <TransitionGroup> does the same for every item of a list. Vue doesn't draw the animation itself. It adds CSS classes at each stage, and your CSS moves the element. When an element leaves, Vue waits for the movement to finish before removing that element from the DOM, like Mrs. Sri, who stores the sign only once it's all the way down.

Transition: one element entering and leaving

An example is an "Added to cart" toast. A toast is a small message that appears for a moment, then disappears by itself. This toast shows each time Add is clicked, then disappears after 2 seconds:

<script setup>
import { ref } from "vue"

const toast = ref("")
let timer

function add(name) {
  toast.value = "Added to cart: " + name
  clearTimeout(timer)
  timer = setTimeout(() => (toast.value = ""), 2000)
}
</script>

<template>
  <button @click="add('Fried Rice')">Add Fried Rice</button>

  <Transition name="fade">
    <p v-if="toast" class="toast">{{ toast }}</p>
  </Transition>
</template>

<style>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

Classes at each stage

This is the order of classes on the <p>, recorded in Chrome with the CSS above. One frame is one time the browser draws the screen:

Time What happens Classes on <p>
0 ms toast is filled, <p> goes into the DOM fade-enter-from fade-enter-active
1 frame later fade-enter-active fade-enter-to
300 ms the transition ends transition classes removed
2 seconds toast is emptied fade-leave-from fade-leave-active
1 frame later fade-leave-active fade-leave-to
300 ms later <p> is removed from the DOM

mode="out-in": swapping one element for another

If the <p> is written <p :key="toast"> and the toast's text changes from Fried Rice to Iced Tea, Vue treats that <p> as a new element: the old one leaves, the new one enters. Without mode, both run at the same time. So for 300 ms there are two <p> in the DOM, and the layout jumps. <Transition name="fade" mode="out-in"> makes the old one finish leaving first, then the new one enters.

v-if and v-else inside one <Transition> are fine too, because only one of them renders.

TransitionGroup: the cart list

This is the cart list with <TransitionGroup>, then its CSS:

<TransitionGroup name="list" tag="ul">
  <li v-for="item in cart" :key="item.id">{{ item.name }}</li>
</TransitionGroup>
.list-enter-active,
.list-leave-active {
  transition: all 0.3s ease;
}
.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
.list-move {
  transition: transform 0.3s ease;
}
.list-leave-active {
  position: absolute;
}

For example, the cart holds Fried Rice, Chicken Noodles and Iced Tea, then Fried Rice is removed. This table shows what you see, with and without keys:

:key What you see during those 300 ms
item.id Fried Rice fades out to the right, Chicken Noodles and Iced Tea slide up with list-move
none the first row turns into Chicken Noodles right away, the second into Iced Tea, and the third row fades out

Without keys, Vue reuses elements by position, swaps their text, then removes the last element. The animation runs on the wrong row.

In the exercise editor

The first exercise is written as a whole .vue file, as in a Vite project: <script setup>, <template> and <style>. The CSS in <style> is applied to the preview too. The ---template--- format the Lab's Playground uses has no <style> block: a <style> in the template is silently dropped. That's why the Lab's Playground code adds its CSS with a script:

document.getElementById("transition-style")?.remove()
const style = document.createElement("style")
style.id = "transition-style"
style.textContent = ".fade-enter-active, .fade-leave-active { transition: opacity 0.3s } .fade-enter-from, .fade-leave-to { opacity: 0 }"
document.head.appendChild(style)

⚠️ Why does the element vanish right away, with no animation?

TL;DR: <Transition name="fade"> wraps one element that appears and disappears through v-if, v-show or a changing :key. Vue adds the classes fade-enter-from, fade-enter-active, fade-enter-to when the element enters, and fade-leave-from, fade-leave-active, fade-leave-to when it leaves. Your CSS moves the element, and a leaving element is removed from the DOM only after its transition ends. mode="out-in" makes the old element leave before the new element enters. <TransitionGroup tag="ul"> animates every item of a list, always with a unique :key. The -move class (such as list-move) goes on items that change place, so that the move can be animated. Put v-if on the element, not on the Transition.

⚡ Transition: one element entering and leaving

Write:

<Transition name="fade">
  <p v-if="toast" class="toast">{{ toast }}</p>
</Transition>

<style>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

Meaning: <Transition> is a built-in Vue component that animates one element as it enters or leaves the page. Here the element is the <p>, a toast (a small message) that shows while toast holds text. Vue doesn't draw the animation itself. It adds a class at each stage, and your CSS does the moving. name="fade" sets the class prefix: fade-enter-from, fade-leave-to, and so on.

⚡ A leaving element is waited for

Guess first: toast is emptied. Is the <p> removed from the DOM right away?

Not yet. For about 300 ms, the <p> is still in the DOM with the class fade-leave-active, fading out. Vue reads the transition duration from the CSS, then waits.

Meaning: enter-from is the starting state when entering, enter-to its end state, and enter-active stays for the whole process. leave-from, leave-active and leave-to are their partners for leaving. Without CSS, the element appears and disappears instantly, with no error.

⚡ TransitionGroup for a list

Write:

<TransitionGroup name="list" tag="ul">
  <li v-for="item in cart" :key="item.id">{{ item.name }}</li>
</TransitionGroup>

Meaning: <Transition> is for one element, and <TransitionGroup> for every item of a list, here the cart list. Every item needs a unique :key, and tag="ul" renders a real <ul> around the items.

What you will learn