Vue.js Transition and Animation

Vue.js transition and animation is an important feature of Vue.js framework. In this section, we are going to discuss the Vue.js transition and animation.

Vue.js Transition

This transition method allows us to use the transition effects when the elements are added, updated, and removed from the DOM. Vuse.js will automatically add/remove the CSS classes at the right time to trigger the CSS transitions. During the transition, we can also provide the JavaScript hook functions to manipulate the custom DOM.

Vue.js has an integrated transition feature, which is required to be wrapped around the transition element. Vue.js offers the transition wrapper component that permits us to add or remove the transition for any element in the following contexts:

  • Conditional Rendering (Using v-if)
  • Conditional Display (Using v-show)
  • Dynamic components
  • Component root nodes

The following are the syntax used for Vue.js transition:

Here, the name of the vue.js transition is fade. Vue.js transition offers some of the common transition classes, and these classes are prefixed with the transition name.

Example: This example explains how we can use the Vue transition in the Vue.js application.

Index.html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Transition and Animation

In the above screenshot, we can see that there is a “Click Here” button and the transition content. When we click on the “Click Here” button, the transition content will be disabled in just 5 sec seconds, as shown below:

Vue.js Transition and Animation

Transition classes

The vue.js have six transition classes used to add/remove the transitions in the Vue.js application. These transition classes are given below:

  • v-enter
  • v-enter-active
  • v-enter-to
  • v-leave
  • v-leave-active
  • v-leave-to

Now, we are going to discuss these transition classes in detail.

v-enter: The v-enter class is the starting state to enter. It is added before the insertion of an element, and the class is removed after adding the element.

v-enter-active: The v-enter-active class is the active state to enter. This class is applied during the entire entering phase of the Vue transition. It is added before the insertion of an element, and the class is removed when the transition is complete. It is used to declare the duration, delay, and easing curve for the entering in the Vue transition phase.

v-enter-to: It is only available in version 2.1.8+ or later version of Vue.js. It is an ending state to enter. This class added a frame after the element is inserted, and when the transition is complete, the frame is removed.

v-leave: The v-leave class is the starting state to leave. It is added when the leaving transition is triggered, and it removed a class after the frame is completed in the transition phase.

v-leave-active: It is the active state to leave. This class can be applied during the entire leaving phase of transition. It should be added when the leave transition is triggered, and the class is removed when the transition complete. It is used to declare the duration, delay, and easing curve for the leaving transition phase of the Vue.js.

v-leave-to: It is only available in version 2.1.8+ or later version of Vue.js. It is an ending state to enter. This class added a frame after the leaving transition is triggered, and when the transition is complete, the frame is removed.

Example: This example explains how we can use the transition classes in the Vue.js application. Here, the transition name of the Vue.js is the shift. This transition is used to shift the component to left and right according to the user requirement.

Index.html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Transition and Animation

In the above screenshot, we can see that there is a “Click Here” button and the transition content. If we click on the button, the transition content shifted to the right side.

Vue.js Transition and Animation

If we again click on the button, the content shifted to its original position.

Vue.js Transition and Animation

Vue.js Animation

Vue Animation can be applied in the same manner as the transition is applied. It also includes classes that are required to define for effect to take place.

Example: This example explains how we can use the Vue animation in the Vue.js.

Index.html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Transition and Animation

In the above screenshot, we can see that there is a “Click Here” button and the content. If we click on the button, the content changes with a different degree, and reverse the content. After a few seconds, the content comes to its original form.

Vue.js Transition and Animation

Custom Transition Classes

The Vue.js framework offers a list of custom classes that can be added as attributes to the transition element. The custom classes of the Vue.js are as follow:

These classes will override the conventional class names. Custom transition classes are very useful when we want to add the Vue’s transition system to an existing CSS animation library like Animate.css.

Let us take an example to understand how we can use the custom classes in the Vue.js application.

Example:

Index.Html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Transition and Animation
Vue.js Transition and Animation
Vue.js Transition and Animation

In the above Vue instance, we have applied two animations. The first animation is enter-active-class = “animated swing” and second leave-active-class = “animated bounceOutRight”. Also, we use the custom transition classes for the animation from the external CSS library.

Explicit Transition Duration

Vue.js can also allow us to use the explicit transition and animation on the element. By default, the Vue waits for the transitionend and animatedend event to find out that the transition or animation is finished.

In some cases, the transition and animation can cause delay. In such cases, we can define an explicit transition duration (in milliseconds) by using the duration prop on the <transition> component, as shown below:

We can also define the duration separately for entering and leaving, as shown below:

JavaScript Hooks

We can also define the JavaScript hooks in transition attributes, as shown below:

These JavaScript hooks can be used in combination with CSS transition and animation or by herself.

While using the JavaScript transitions, the done callbacks are needed to enter and leave the hooks. Otherwise, the JavaScript hooks will call synchronously and the Vue transition finished immediately.

It is a good thing to explicitly add the v-bind: css= “false” attribute to JavaScript transitions so that the Vue.js can avoid CSS detection. It also stops the CSS rules from unintentionally impacting the transition.

Let us take an example to understand the JavaScript hooks using Velocity.js.

Example: This example explains how we can use the JavaScript hooks in the Vue.js transition.

Index.html

Main.js

 Output: After executing this code, we will get the output, as shown below:

Vue.js Transition and Animation

When we click on the “Animation” button, it shows the various types of animation with the content.

Vue.js Transition and Animation

Transition on Initial Render

If we want to use the transition on the initial render of a node, we need to include an appear property to the transition component. The following are the syntax used for applying transition on the initial render:

Syntax:

By default, it will use the transition defined for entering and leaving. We can also define it in the custom CSS classes.

Example: This example explains how we can use the transition on the initial render in the Vue.js Transition.

Index.html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Transition and Animation

We can use either “appear” property or “v-on: appear” hook in the transition.

Transitioning between components

Transitioning between the components is a very easy task in the Vue.js. Here, we don’t need any key attributes in the transition. Instead, we can wrap a dynamic component in the Vue transition.

Example: This example explains how we can use the transitions between the components in the Vue.js transition.

Index.html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Transition and Animation

Pin It on Pinterest

Share This