The Transition component helps you add smooth animations when elements appear, disappear, or change on the page. It makes your app feel lively and polished.
Transition component for animations in Vue
Introduction
Syntax
Vue
<Transition name="fade"> <element-or-component v-if="condition" /> </Transition>
The component wraps the element or component you want to animate.
The 'name' attribute sets the base class for CSS animations (e.g., 'fade' means CSS classes like 'fade-enter-from').
Examples
Vue
<Transition name="fade"> <p v-if="show">Hello!</p> </Transition>
Vue
<Transition name="slide"> <div v-if="isOpen">Menu content</div> </Transition>
Vue
<Transition>
<button v-if="clicked">Clicked!</button>
</Transition>Sample Program
This component shows a button that toggles a message. The message fades in and out smoothly using the Transition component and CSS classes.
Vue
<template> <button @click="toggle">Toggle Message</button> <Transition name="fade"> <p v-if="visible" class="message">Hello, Vue Transition!</p> </Transition> </template> <script setup> import { ref } from 'vue' const visible = ref(false) function toggle() { visible.value = !visible.value } </script> <style scoped> .fade-enter-from, .fade-leave-to { opacity: 0; } .fade-enter-active, .fade-leave-active { transition: opacity 0.5s ease; } .message { margin-top: 1rem; font-weight: bold; color: #2c3e50; } </style>
Important Notes
Always pair the Transition component with CSS classes for smooth animations.
Use v-if inside Transition to control when the element appears or disappears.
Test animations in different browsers to ensure consistent behavior.
Summary
The Transition component adds animations when elements enter or leave the page.
Wrap the element with <Transition> and use CSS classes named after the 'name' attribute.
It improves user experience by making changes feel smooth and natural.
Practice
1. What is the main purpose of the
<Transition> component in Vue?easy
Solution
Step 1: Understand the role of <Transition>
The <Transition> component is designed to wrap elements and add animations when they appear or disappear.Step 2: Compare with other options
Fetching data, managing state, and creating components are unrelated to animation transitions.Final Answer:
To add animations when elements enter or leave the page -> Option AQuick Check:
Transition component = animations on enter/leave [OK]
Hint: Think: Transition means smooth entering or leaving [OK]
Common Mistakes:
- Confusing Transition with data fetching
- Thinking it manages state
- Assuming it creates components
2. Which of the following is the correct way to use the
<Transition> component with a CSS class name 'fade'?easy
Solution
Step 1: Recall the correct attribute for naming transition classes
Thenameattribute sets the base CSS class for transition states.Step 2: Check the options
OnlyContentusesname="fade", which is correct. Others use invalid attributes likeclass,animation, oreffect.Final Answer:
<Transition name="fade"><div v-if="show">Content</div></Transition> -> Option CQuick Check:
Use name="fade" for CSS classes [OK]
Hint: Use 'name' attribute to link CSS classes [OK]
Common Mistakes:
- Using 'class' instead of 'name'
- Using non-existent attributes like 'animation'
- Forgetting to wrap the element inside <Transition>
3. Given the following Vue template code, what will happen when
show changes from false to true?
<Transition name="slide"> <div v-if="show">Hello</div> </Transition>
medium
Solution
Step 1: Understand v-if with Transition
Whenshowbecomes true, the div is inserted and Transition applies CSS classes based on the 'slide' name.Step 2: Identify the animation classes
The classes likeslide-enter-fromandslide-enter-activeare added to animate the div's entrance.Final Answer:
The div will appear with slide-enter-from and slide-enter-active CSS classes applied for animation -> Option BQuick Check:
v-if true + Transition name = enter animation [OK]
Hint: v-if true triggers enter animation classes [OK]
Common Mistakes:
- Thinking Transition blocks rendering
- Assuming no animation on enter
- Confusing slide with fade animation
4. Identify the error in this Vue template using
<Transition>:
<Transition name="fade"> <div v-show="visible">Content</div> </Transition>
medium
Solution
Step 1: Understand difference between v-if and v-show with Transition
Transition works best withv-ifbecause it controls element insertion/removal, triggering enter/leave animations.Step 2: Analyze the code
Usingv-showonly toggles visibility via CSS, so Transition cannot detect element entering or leaving, preventing animations.Final Answer:
Using v-show instead of v-if prevents proper enter/leave animations -> Option AQuick Check:
Use v-if for Transition animations [OK]
Hint: Use v-if, not v-show, for enter/leave animations [OK]
Common Mistakes:
- Thinking v-show triggers enter/leave animations
- Believing name attribute must be longer
- Assuming Transition can't wrap div
5. You want to create a fade animation using
<Transition> with these CSS classes:
.fade-enter-active { transition: opacity 0.5s; }
.fade-enter-from { opacity: 0; }
.fade-enter-to { opacity: 1; }
.fade-leave-active { transition: opacity 0.5s; }
.fade-leave-from { opacity: 1; }
.fade-leave-to { opacity: 0; }
Which Vue template correctly applies this animation to a paragraph that toggles with showText?hard
Solution
Step 1: Match CSS classes with Transition usage
The CSS classes use the base name 'fade', so the Transition component must havename="fade".Step 2: Use v-if to toggle element presence
v-if toggles the element in the DOM, triggering enter and leave animations. v-show only toggles visibility and won't trigger these animations.Step 3: Check options
Hello World
usesname="fade"andv-if, which is correct.Hello World
uses v-show, which won't animate properly.Hello World
uses an invalid attribute 'animation'.Hello World
shows the paragraph whenshowTextis false, which is opposite of expected.Final Answer:
<Transition name="fade"> <p v-if="showText">Hello World</p> </Transition> -> Option DQuick Check:
Use name="fade" + v-if for fade animation [OK]
Hint: Use v-if with name="fade" for fade animations [OK]
Common Mistakes:
- Using v-show instead of v-if
- Using wrong attribute like animation
- Showing element when condition is false
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
