Dynamic transitions let you smoothly change how elements appear or disappear on your page. They make your app feel alive and easy to follow.
Dynamic transitions in Vue
<transition :name="transitionName"> <element-to-animate v-if="showElement" /> </transition>
The :name attribute binds the transition class name dynamically.
The element inside <transition> must be conditionally rendered (like with v-if) to trigger the animation.
<template> <transition :name="currentTransition"> <div v-if="show">Hello!</div> </transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) const currentTransition = ref('fade') </script>
<template> <button @click="toggle">Toggle</button> <select v-model="currentTransition"> <option value="fade">Fade</option> <option value="slide">Slide</option> </select> <transition :name="currentTransition"> <p v-if="show">Dynamic transition example</p> </transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) const currentTransition = ref('fade') function toggle() { show.value = !show.value } </script>
This Vue component lets you pick a transition style from a dropdown. When you toggle the box, it appears or disappears using the chosen animation. The box is keyboard accessible and uses ARIA roles for screen readers.
<template>
<div>
<label for="transition-select">Choose transition:</label>
<select id="transition-select" v-model="transitionName" aria-label="Select transition">
<option value="fade">Fade</option>
<option value="slide">Slide</option>
<option value="bounce">Bounce</option>
</select>
<button @click="toggleShow" aria-pressed="show">Toggle Box</button>
<transition :name="transitionName">
<div v-if="show" class="box" tabindex="0" role="region" aria-live="polite">
Watch me {{ transitionName }}!
</div>
</transition>
</div>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
const transitionName = ref('fade')
function toggleShow() {
show.value = !show.value
}
</script>
<style scoped>
.box {
width: 10rem;
height: 10rem;
background-color: #4f46e5;
color: white;
display: flex;
align-items: center;
justify-content: center;
margin-top: 1rem;
border-radius: 0.5rem;
font-weight: bold;
user-select: none;
outline-offset: 0.25rem;
}
/* Fade transition */
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
/* Slide transition */
.slide-enter-active, .slide-leave-active {
transition: transform 0.5s ease, opacity 0.5s ease;
}
.slide-enter-from {
transform: translateX(-100%);
opacity: 0;
}
.slide-leave-to {
transform: translateX(100%);
opacity: 0;
}
/* Bounce transition */
@keyframes bounce-in {
0% { transform: scale(0.5); opacity: 0; }
50% { transform: scale(1.2); opacity: 1; }
100% { transform: scale(1); opacity: 1; }
}
@keyframes bounce-out {
0% { transform: scale(1); opacity: 1; }
100% { transform: scale(0.5); opacity: 0; }
}
.bounce-enter-active {
animation: bounce-in 0.5s forwards;
}
.bounce-leave-active {
animation: bounce-out 0.5s forwards;
}
</style>Always use v-if inside <transition> to trigger animations properly.
Dynamic transitions let you reuse the same transition wrapper for many animation styles.
Use ARIA attributes and keyboard focus to keep animations accessible.
Dynamic transitions let you change animation styles on the fly in Vue.
Use the :name binding on <transition> to switch animations.
Combine with v-if to animate elements entering or leaving the page.
Practice
:name attribute in Vue's <transition> component control?Solution
Step 1: Understand the role of
The:namein<transition>:nameattribute sets the base CSS class name for the transition states like enter and leave.Step 2: Connect
Vue uses this name to apply classes such as:nameto CSS animation classes{name}-enter-activeand{name}-leave-activeto trigger CSS animations.Final Answer:
The CSS class name used for the transition animation -> Option AQuick Check:
Transition:name= CSS class base name [OK]
- Confusing :name with HTML tag or JS function
- Thinking :name controls timing instead of class names
- Assuming :name is optional for dynamic transitions
Solution
Step 1: Identify dynamic binding syntax in Vue
Dynamic binding uses:orv-bind:with a JavaScript expression inside quotes.Step 2: Check which option uses a variable for dynamic name
<transition :name="transitionName">uses:name="transitionName", correctly binding a variable namedtransitionName.Final Answer:
<transition :name="transitionName"> -> Option DQuick Check:
Dynamic binding = :name="variable" [OK]
- Using static string without binding for dynamic names
- Missing quotes around variable name
- Using :name without a value
<template>
<transition :name="animation">
<div v-if="show">Hello</div>
</transition>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
const animation = ref('fade')
</script>What happens if you change
animation.value from 'fade' to 'slide' while show is true?Solution
Step 1: Understand when transitions trigger
Transitions animate when elements enter or leave the DOM, triggered byv-ifchanges.Step 2: Changing transition name while element is visible
Changinganimation.valuealone does not trigger enter/leave, so no animation runs immediately.Final Answer:
No animation occurs untilshowchanges -> Option CQuick Check:
Transition triggers on DOM changes, not name changes [OK]
- Thinking transition changes animate immediately
- Assuming Vue errors on dynamic name changes
- Confusing v-if toggling with reactive variable changes
<transition :name="currentTransition">
<div v-if="visible">Content</div>
</transition>
<script setup>
import { ref } from 'vue'
const visible = ref(true)
const currentTransition = ref('fade')
// Later in code
currentTransition = 'slide'
</script>What is the main problem in this code?
Solution
Step 1: Identify how to update Vue refs
Refs are reactive objects; to update their value, use.value, not direct assignment.Step 2: Spot incorrect assignment
The code doescurrentTransition = 'slide'which replaces the ref instead of updating its value.Final Answer:
Directly assigning a new string to a ref variable instead of using .value -> Option AQuick Check:
Update ref with.value, not direct assignment [OK]
- Assigning new value directly to ref variable
- Thinking v-if cannot be used inside transition
- Believing transition names are static only
Solution
Step 1: Understand how transitions trigger in Vue
Transitions animate when elements enter or leave the DOM, controlled byv-ifor similar directives.Step 2: Combine dynamic transition name with toggling visibility
Changing the transition name alone won't animate; togglingshowtriggers the animation with the current transition name.Step 3: Confirm correct approach
Using a ref for transition name and togglingshowensures the element animates with the chosen transition each toggle.Final Answer:
Use a ref for transition name and toggleshowto trigger enter/leave animations -> Option BQuick Check:
Toggle visibility + dynamic name = dynamic transitions [OK]
- Changing transition name without toggling visibility
- Trying to apply multiple transitions at once
- Keeping element always visible expecting animation
Ready to prove it?
Pass the test and this pattern moves unknown
