Using CSS for Vue Animations

Tutorial 2 of 5

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

  1. Create a Vue component that uses a CSS animation to rotate an element 360 degrees.
  2. Create a Vue component that uses a CSS transition to change the color of a button when it's clicked.

Solutions:

  1. 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>
  1. 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!