ποΈ Vue Animations with v-for β Animate Lists with transition-group (2025 Guide)
π§² Introduction β Why Animate v-for Lists in Vue?
Animating elements rendered with v-for makes your app visually dynamic and interactive. Vue provides a special component called <transition-group> for handling animations when list items are added, removed, or reordered.
π― In this guide, youβll learn:
- How to animate elements rendered by
v-for - How
<transition-group>works with keys and tags - CSS transition classes and examples
- Tips to avoid common animation issues
π What Is <transition-group>?
Vueβs <transition-group> is a wrapper component designed to apply animations when multiple elements are rendered with v-for.
- Uses
:keyto track items - Works with entering, leaving, and moving transitions
- Accepts a
tagto render (likeul,div, etc.)
π§± Example β Animating a List with Fade & Slide
<template>
<div>
<button @click="addItem">Add Item</button>
<transition-group name="list" tag="ul">
<li v-for="item in items" :key="item" class="item">
{{ item }}
</li>
</transition-group>
</div>
</template>
<script>
export default {
data() {
return {
count: 1,
items: ['Item 1']
};
},
methods: {
addItem() {
this.count++;
this.items.push(`Item ${this.count}`);
}
}
};
</script>
<style>
.list-enter-active, .list-leave-active {
transition: all 0.5s ease;
}
.list-enter-from, .list-leave-to {
opacity: 0;
transform: translateY(30px);
}
.item {
background: #eee;
margin: 8px 0;
padding: 10px;
list-style: none;
}
</style>
π§ When items are added, they fade in and slide up.
π Removing Items with Animation
Modify the example to include a remove button:
<li v-for="(item, index) in items" :key="item">
{{ item }}
<button @click="items.splice(index, 1)">β</button>
</li>
βοΈ Vue automatically applies leave transition when an item is removed.
π Animating Reordered Lists
If you sort the list, Vue also animates the position change of the items.
<button @click="items.reverse()">Reverse List</button>
π This will trigger a move transition using *-move class.
β Example:
.list-move {
transition: transform 0.3s;
}
𧬠Using Custom Tags with transition-group
You can wrap your v-for items in any valid tag:
<transition-group name="fade" tag="div">
<span v-for="tag in tags" :key="tag">{{ tag }}</span>
</transition-group>
β οΈ Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Items not animating | Missing or duplicate :key | Ensure each item has a unique key |
| Transitions not applied | Class mismatch or incorrect naming | Use correct *-enter-* syntax |
| Move transitions not firing | CSS transform or position missing | Apply transform or position: relative |
π Summary β Recap & Next Steps
Animating v-for lists in Vue creates visually engaging interfaces with minimal effort. Vueβs <transition-group> component handles most of the complexity, making list additions, deletions, and reordering smooth and stylish.
π Key Takeaways:
- Use
<transition-group>for animatingv-forrendered elements - Always use unique
:keyfor each item - Combine
enter,leave, andmoveclasses for full control - Transitions support both CSS and JS hooks
βοΈ Real-World Relevance:
Used in chat apps, notification lists, sortable tasks, shopping carts, and dashboards.
β FAQ Section
β What is the purpose of <transition-group> in Vue?
β
It allows animations when multiple items are rendered with v-for.
β Why do I need a :key for list animations?
β
Vue uses the key to track which elements changed, so transitions work correctly.
β Can I animate reordered list items?
β
Yes, use the .move class with a transform property.
β Can I use custom tags with transition-group?
β
Yes. Use the tag attribute to wrap your list items in div, ul, etc.
β Is <transition-group> required for list transitions?
β
Yes. Regular <transition> doesnβt support multiple elements.
Share Now :
