Skip to main content
Vue.js for Beginners to Advanced
CHAPTER 21 Beginner

Vue Transitions and Animations

Updated: May 18, 2026
5 min read

# CHAPTER 21

Vue Transitions and Animations

1. Chapter Introduction

Animations transform a functional app into a delightful experience. Vue's <Transition> and <TransitionGroup> components apply CSS classes at exactly the right animation moment, making motion declarative and predictable.

2. How Vue Transitions Work

text
123456789
ENTER lifecycle:
1. .v-enter-from   → Start state (opacity: 0)
2. .v-enter-active → Transition rule applied
3. .v-enter-to     → End state (opacity: 1)

LEAVE lifecycle:
1. .v-leave-from   → Start state (opacity: 1)
2. .v-leave-active → Transition rule applied
3. .v-leave-to     → End state (opacity: 0)

3. Basic Transitions

vue
123456789101112131415161718192021222324252627282930313233
<script setup>
import { ref } from &#039;vue'
const show = ref(true)
const tab = ref(&#039;a')
</script>

<template>
  <button @click="show = !show">Toggle</button>

  <Transition name="fade">
    <p v-if="show">I fade in/out</p>
  </Transition>

  <!-- mode="out-in": leave first, then enter -->
  <Transition name="slide" mode="out-in">
    <div v-if="tab === &#039;a'" key="a">Tab A</div>
    <div v-else key="b">Tab B</div>
  </Transition>

  <!-- appear: animate on initial render -->
  <Transition name="fade" appear>
    <p>Animates on page load!</p>
  </Transition>
</template>

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

.slide-enter-active, .slide-leave-active { transition: all 0.35s ease; }
.slide-enter-from { opacity: 0; transform: translateX(-20px); }
.slide-leave-to { opacity: 0; transform: translateX(20px); }
</style>

4. TransitionGroup — List Animations

vue
1234567891011121314151617181920212223242526272829303132
<script setup>
import { ref } from &#039;vue'
const items = ref([
  { id: 1, text: &#039;Learn Vue' },
  { id: 2, text: &#039;Build projects' },
])
let nextId = 3

function add() { items.value.push({ id: nextId++, text: `Item ${nextId - 1}` }) }
function remove(id) { items.value = items.value.filter(i => i.id !== id) }
function shuffle() { items.value = [...items.value].sort(() => Math.random() - 0.5) }
</script>

<template>
  <button @click="add">Add</button>
  <button @click="shuffle">Shuffle</button>

  <TransitionGroup name="list" tag="ul">
    <li v-for="item in items" :key="item.id">
      {{ item.text }}
      <button @click="remove(item.id)">✕</button>
    </li>
  </TransitionGroup>
</template>

<style>
.list-enter-active, .list-leave-active { transition: all 0.4s ease; }
.list-enter-from { opacity: 0; transform: translateX(-30px); }
.list-leave-to { opacity: 0; transform: translateX(30px); }
.list-move { transition: transform 0.4s ease; }
.list-leave-active { position: absolute; } /* prevents layout jump */
</style>

5. Common Mistakes

  • Missing :key on TransitionGroup children: Vue needs keys to identify entering/leaving elements.
  • Using v-show with <Transition>: <Transition> works with v-if primarily. v-show also works but cannot use mode.

6. MCQs

Question 1

Class applied at start of enter?

Question 2

mode="out-in" means?

Question 3

TransitionGroup requires unique?

Question 4

.list-move class enables?

Question 5

appear prop?

Question 6

:css="false" when using?

Question 7

v-enter-active defines?

Question 8

TransitionGroup default renders?

Question 9

Custom CSS class names?

Question 10

JS enter hook done param?

7. Interview Questions

  • Q: What is the difference between <Transition> and <TransitionGroup>?
  • Q: Explain mode="out-in" and when you would use it.

8. Summary

Vue transitions are class-injection systems — Vue adds/removes CSS classes at lifecycle moments. <Transition> for single elements, <TransitionGroup> for animated lists with FLIP moves. Custom enter-active-class enables Animate.css integration with zero JS.

9. Next Chapter Recommendation

In Chapter 22: Authentication and Authorization, we implement JWT login, protect routes, and manage user sessions in Vue.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·