Vue.js / Vue Animations and Transitions
Using CSS for Vue Animations
In this tutorial, you'll learn how to use CSS for creating animations in Vue.js. You'll get hands-on experience creating CSS transitions and animations for Vue components.
Section overview
5 resourcesCovers creating animations and transitions using Vue.js.
1. Introduction
In this tutorial, we will be learning how to use CSS for creating animations in Vue.js. Vue.js is a progressive JavaScript framework used for building user interfaces. Its core library is focused on the view layer which makes it easy to integrate with other libraries or existing projects.
By the end of this tutorial, you will be able to:
- Understand how to use CSS transitions and animations in Vue.js
- Create smooth and engaging animations for your Vue components
Prerequisites:
To follow this tutorial, you should have a basic understanding of:
- HTML, CSS, and JavaScript
- Vue.js basics
2. Step-by-Step Guide
Vue provides several ways to apply transition effects when items are inserted, updated, or removed from the DOM. This includes tools to:
- automatically apply classes for CSS transitions and animations
- integrate third-party CSS animation libraries
- use JavaScript to directly manipulate the DOM during transition hooks
Let's get into it:
2.1. CSS Transitions
Vue provides a <transition> wrapper component, allowing you to add entering/leaving transitions for any element or component in the following contexts:
- Conditional rendering (using v-if)
- Conditional display (using v-show)
- Dynamic components
- Component root nodes
For example:
<transition name="fade">
<p v-if="show">Hello</p>
</transition>
In this fade transition, Vue will add/remove CSS classes at appropriate times to trigger CSS transitions or animations:
v-enter: Starting state for enter. Added before element is inserted, removed one frame after element is inserted.v-enter-active: Active state for enter. Applied during entire entering phase. Added before element is inserted, removed when transition/animation finishes.v-leave: Starting state for leave. Added immediately when a leaving transition is triggered, removed after one frame.v-leave-active: Active state for leave. Applied during the entire leaving phase. Added immediately when leave transition is triggered, removed when the transition/animation finishes.
2.2. CSS Animations
CSS animations are applied in the same way with Vue:
<transition name="bounce">
<p v-if="show">Hello</p>
</transition>
And the CSS:
.bounce-enter-active {
animation: bounce-in .5s;
}
.bounce-leave-active {
animation: bounce-out .5s;
}
@keyframes bounce-in {
0% {
transform: scale(0);
}
50% {
transform: scale(1.5);
}
100% {
transform: scale(1);
}
}
@keyframes bounce-out {
0% {
transform: scale(1);
}
50% {
transform: scale(1.5);
}
100% {
transform: scale(0);
}
}
3. Code Examples
Here is a practical example of using Vue.js with CSS animations:
3.1. Vue Component with CSS Animation
<template>
<div>
<button @click="show = !show">
Toggle
</button>
<transition name="slide-fade">
<p v-if="show">hello</p>
</transition>
</div>
</template>
<script>
export default {
data () {
return {
show: true
}
}
}
</script>
<style>
.slide-fade-enter-active {
transition: all .3s ease;
}
.slide-fade-leave-active {
transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active below version 2.1.8 */ {
transform: translateX(10px);
opacity: 0;
}
</style>
In the above example, when you click the 'Toggle' button, the text 'hello' will slide in and fade, and slide out and fade when clicked again.
4. Summary
In this tutorial, we've learned how to create CSS transitions and animations in Vue.js. We've covered how to use Vue's <transition> wrapper component and how to write the corresponding CSS.
Next Steps:
To further your learning, you could:
- Experiment with different CSS transition and animation properties
- Try creating more complex animations
- Look at integrating third-party animation libraries with Vue
5. Practice Exercises
- Create a Vue component that uses a CSS animation to rotate an element 360 degrees.
- Create a Vue component that uses a CSS transition to change the color of a button when it's clicked.
Solutions:
- Rotating an element:
<template>
<div>
<transition name="rotate">
<p v-if="show">I will rotate</p>
</transition>
<button @click="show = !show">Toggle</button>
</div>
</template>
<script>
export default {
data () {
return {
show: true
}
}
}
</script>
<style>
.rotate-enter-active {
animation: rotate 2s;
}
.rotate-leave-active {
animation: rotate 2s reverse;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>
- Changing the color of a button:
<template>
<div>
<transition name="color-change">
<button v-if="show" @click="show = !show">I will change color</button>
</transition>
</div>
</template>
<script>
export default {
data () {
return {
show: true
}
}
}
</script>
<style>
.color-change-enter-active {
transition: background-color 1s;
}
.color-change-leave-active {
transition: background-color 1s;
}
.color-change-enter, .color-change-leave-to {
background-color: red;
}
.color-change-leave, .color-change-enter-to {
background-color: blue;
}
</style>
Have fun practicing with Vue and CSS animations!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article