Vue Advanced (Yaosanlu): Transition tag realizes page jump animation

I. Introduction

In Vuethe project development process, the family bucket is used to vue-routerrealize the routing jump, and the page forward and backward jump processes correspond to different switching animations. vue-routerHow to set transition animation when switching pages?

First, the following questions need to be considered:

  1. How to judge whether it is forward or backward when switching routes?
  2. How to switch animation left to right every time you switch?

It can be realized through the following solutions:
define levels for each page, and determine which level of page the user enters when switching routes. Animate forward if the user enters a higher-level route, and animate back if the user exits a lower-level route.

2. Implementation of the plan

// router/index.js
import VueRouter from 'vue-router'
import Home from '../components/home/home'
import User from '../components/user/user'
var router = new VueRouter({
    
    
    routes:[{
    
    
        name:'test',
        path:'/',
        meta:{
    
    index:0},//meta对象的index用来定义当前路由的层级,由小到大,由低到高
        component:{
    
    
            template:'<div>test</div>'
        }
    },{
    
    
        name:'home',
        path:'/home',
        meta:{
    
    index:1},
        component:Home
    },{
    
    
        name:'user',
        path:'/user/:id',
        meta:{
    
    index:2},
        component:User
    }]
});

Monitor the routing jump through watchthe monitoring function, judge the hierarchical relationship between the switching pages, and use this to judge whether the routing is forward or backward.

<template>
  <div id="app">
    <transition :name="transitionName">   
      <router-view></router-view>
    </transition>
  </div>
</template>
<script>
export default {
    
    
  name: 'App',
  data(){
    
    
      return {
    
    
          transitionName:''
      }
  },
  watch: {
    
    //使用watch 监听$router的变化
    $route(to, from) {
    
    
      //如果to索引大于from索引,判断为前进状态,反之则为后退状态
      if(to.meta.index > from.meta.index){
    
    
	    //设置动画名称
        this.transitionName = 'slide-left';
      }else{
    
    
        this.transitionName = 'slide-right';
      }
    }
  }
}
</script>
<style>
    // 编写slide-left 和 slide-right 类的动画
  	.slide-right-enter-active,
    .slide-right-leave-active,
    .slide-left-enter-active,
    .slide-left-leave-active {
    
    
      will-change: transform;
      transition: all 500ms;
      position: absolute;
    }
    .slide-right-enter {
    
    
      opacity: 0;
      transform: translate3d(-100%, 0, 0);
    }
    .slide-right-leave-active {
    
    
      opacity: 0;
      transform: translate3d(100%, 0, 0);
    }
    .slide-left-enter {
    
    
      opacity: 0;
      transform: translate3d(100%, 0, 0);
    }
    .slide-left-leave-active {
    
    
      opacity: 0;
      transform: translate3d(-100%, 0, 0);
    }  
</style>

3. Extended reading transition label

transitionThe tag is a built-in animation tag that is responsible for adding styles to elements when Vueinserting, updating, and removing elementsDOM

Note: transitionThe label can only contain one element, and the included label needs to be set v-if/v-showto control the display of the element

Animation style class name
Vue2
enters: .v-enterstart state, .v-enter-activeenter animation, .v-enter-toend state

Leave: .v-leavestart state, .v-leave-activeleave animation, v-leave-toend state

Vue3
enters: .v-enter-fromstart state, .v-enter-activeenter animation, .v-enter-toend state

Leave: .v-leave-fromstart state, .v-leave-activeleave animation, .v-leave-toend state

insert image description here

4. Extended reading

Guess you like

Origin blog.csdn.net/sunhuaqiang1/article/details/132046910