Home > Web Front-end > Vue.js > body text

How to implement forward and backward routing switching animation effects in a Vue project?

PHPz
Release: 2023-07-21 15:34:54
Original
1928 people have browsed it

How to implement forward and backward routing switching animation effects in a Vue project?

In Vue projects, we often use Vue Router to manage routing. When we switch routes, the page switching is completed instantly without a transition effect. If we want to add some animation effects to page switching, we can use Vue's transition system.

Vue’s transition system provides a simple way to add transition effects when elements are inserted or removed. We can use this feature to animate forward and backward routing switching.

First of all, we need to introduce Vue’s transition component into the project. It can be introduced in the root component or in the component where animation needs to be added.

Next, we need to define the style of the transition animation in the tag. Vue's transition component has the following CSS class names that can be used:

  • enter: the starting state when inserting, which will be applied immediately before inserting the animation
  • enter-active: insert The termination state when deleting will be applied one frame after the insertion animation is triggered until the insertion animation is completed
  • leave: The starting state when deleting, will be applied immediately before deleting the animation
  • leave-active : The termination state when deleting will be applied one frame after the deletion animation is triggered until the deletion animation is completed

The following is a code example of an example animation effect:

<template>
  <transition name="fade" mode="out-in">
    <router-view></router-view>
  </transition>
</template>

<style scoped>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
Copy after login

above In the code, we use a transition effect called "fade" and use the "out-in" mode. This means that when switching routes, first leave the old page and then enter the new page.

In the tag, we use to display the components corresponding to the current route. Corresponding CSS class names are added on entry and exit.

Next, we define the style of the transition animation through the