ποΈ Vue Transitions & Animations β Enhance UX with Seamless Effects (2025 Guide)
π§² Introduction β Why Use Transitions in Vue?
Vue makes it incredibly easy to add transitions and animations to your components, especially during conditional rendering (v-if
, v-show
) or when elements enter/leave the DOM. Using the built-in <transition>
and <transition-group>
components, you can implement fade-ins, slide-outs, list animations, and more with zero third-party dependencies.
π― In this guide, youβll learn:
- The core concepts of Vue transitions and animations
- How to use
<transition>
and<transition-group>
- CSS-based and JavaScript hook transitions
- Real-world animation examples
π What Is a Transition in Vue?
A transition is a visual effect applied to an element as it enters or leaves the DOM. Vue wraps the target element with a <transition>
component to manage these effects.
π§± Basic Example β Fade Effect on Element
<template>
<button @click="toggle">Toggle Box</button>
<transition name="fade">
<div v-if="show" class="box">Hello!</div>
</transition>
</template>
<script>
export default {
data() {
return { show: true };
},
methods: {
toggle() {
this.show = !this.show;
}
}
};
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
.box {
background: lightblue;
padding: 20px;
}
</style>
π§ Vue automatically adds and removes classes during enter/leave phases.
π Vue Transition Class Names
Phase | Class Applied |
---|---|
Enter | *-enter-from |
Enter Active | *-enter-active |
Enter End | *-enter-to |
Leave | *-leave-from |
Leave Active | *-leave-active |
Leave End | *-leave-to |
Replace *
with the transition name like fade
.
𧬠JavaScript Hook Transitions
Use this for complex animation logic or integrating animation libraries (e.g., GSAP).
<transition
@before-enter="beforeEnter"
@enter="enter"
@leave="leave"
>
<div v-if="show">Animated Box</div>
</transition>
<script>
export default {
data() {
return { show: true };
},
methods: {
beforeEnter(el) {
el.style.opacity = 0;
},
enter(el, done) {
setTimeout(() => {
el.style.transition = 'opacity 1s';
el.style.opacity = 1;
done();
}, 0);
},
leave(el, done) {
el.style.transition = 'opacity 1s';
el.style.opacity = 0;
setTimeout(() => done(), 1000);
}
}
};
</script>
π§ͺ Transitioning Multiple Elements β transition-group
Use this to animate lists with v-for
.
<template>
<button @click="addItem">Add</button>
<transition-group name="list" tag="ul">
<li v-for="item in items" :key="item">{{ item }}</li>
</transition-group>
</template>
<script>
export default {
data() {
return { items: ['One', 'Two', 'Three'] };
},
methods: {
addItem() {
this.items.push(`Item ${this.items.length + 1}`);
}
}
};
</script>
<style>
.list-enter-active, .list-leave-active {
transition: all 0.5s ease;
}
.list-enter-from {
transform: translateY(-20px);
opacity: 0;
}
.list-leave-to {
transform: translateY(20px);
opacity: 0;
}
</style>
π§ transition-group
requires a unique :key
for each element.
π Animating v-show
v-show
doesnβt trigger actual DOM insertion/removal, so transitions work only on opacity, display, or visibility.
<transition name="fade">
<div v-show="visible">Visible with v-show</div>
</transition>
βοΈ Best Practices
Practice | Why It Matters |
---|---|
Use :key in transition-group | Ensures unique tracking of items |
Combine CSS & JS hooks carefully | Avoid conflicting effects |
Optimize with will-change or GPU | Improves animation smoothness |
Prefer CSS transitions for simple FX | Faster and declarative |
π Summary β Recap & Next Steps
Vue makes it easy to animate and transition elements using <transition>
and <transition-group>
. Whether you’re fading in modals or animating list items, Vue has you covered with built-in support.
π Key Takeaways:
- Use
<transition>
to wrap conditional elements - Define
.enter-*
and.leave-*
classes for smooth effects - Use
transition-group
for lists - Combine CSS and JS transitions for more control
βοΈ Real-World Relevance:
Used in toasts, modals, dropdowns, form toggles, interactive lists, and dashboards.
β FAQ Section
β What is the <transition>
tag in Vue?
β A wrapper component that enables CSS/JS-based transitions for elements or components.
β Can I animate list items in Vue?
β
Yes, use <transition-group>
with unique keys and CSS classes.
β How do transitions differ between v-if
and v-show
?
β
v-if
adds/removes DOM nodes (full transitions). v-show
only toggles visibility.
β Can I use animation libraries like GSAP with Vue?
β
Absolutely! Use @enter
and @leave
JS hooks for integration.
β What happens if I forget to add :key
in transition-group
?
β Vue can’t track items efficiently, which breaks the animation behavior.
Share Now :