📖 Scenario: You are building a simple Vue app that shows and hides a message with a smooth fade animation. This is like turning a light on and off gently instead of suddenly.
🎯 Goal: Create a Vue component that uses the <transition> component to animate showing and hiding a message with a fade effect.
📋 What You'll Learn
Create a Vue component with a boolean state called showMessage initialized to false.
Add a button that toggles showMessage when clicked.
Use the <transition> component with the name fade to wrap the message.
Define CSS classes for the fade transition to smoothly fade the message in and out.
💡 Why This Matters
🌍 Real World
Transitions make user interfaces feel smooth and polished by animating changes like showing or hiding content.
💼 Career
Knowing how to use Vue's transition component is important for frontend developers to create engaging and accessible web apps.
Progress0 / 4 steps
1
Set up the Vue component with data
Create a Vue component using <script setup>. Inside it, define a boolean ref variable called showMessage and set it to false.
Vue
Hint
Use import { ref } from 'vue' and then const showMessage = ref(false).
2
Add a button to toggle the message
Add a <button> element that toggles the value of showMessage when clicked. Use the @click event with showMessage = !showMessage.
Vue
Hint
Use <button @click="showMessage = !showMessage">Toggle Message</button>.
3
Wrap the message with the transition component
Inside the <template>, add a <transition> component with the attribute name="fade". Inside it, conditionally render a <p> element with the text "Hello, Vue Transition!" only when showMessage is true using v-if.
Vue
Hint
Use <transition name="fade"><p v-if="showMessage">Hello, Vue Transition!</p></transition>.
4
Add CSS for the fade transition
Add CSS styles for the fade transition using the classes .fade-enter-active, .fade-leave-active, .fade-enter-from, and .fade-leave-to. Use transition: opacity 0.5s ease for the active classes and set opacity: 0 for the from and to classes to create a fade effect.
Vue
Hint
Define CSS classes for fade transitions with opacity changes and a 0.5 second ease transition.
Practice
(1/5)
1. What is the main purpose of the <Transition> component in Vue?
easy
A. To add animations when elements enter or leave the page
B. To fetch data from an API
C. To manage application state globally
D. To create reusable UI components
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 A
Quick 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
A.
Content
B.
Content
C.
Content
D.
Content
Solution
Step 1: Recall the correct attribute for naming transition classes
The name attribute sets the base CSS class for transition states.
Step 2: Check the options
Only
Content
uses name="fade", which is correct. Others use invalid attributes like class, animation, or effect.
Final Answer:
<Transition name="fade"><div v-if="show">Content</div></Transition> -> Option C
Quick 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?