码迷,mamicode.com
首页 > 其他好文 > 详细

vue route 设置界面之间的过渡动效

时间:2021-05-24 05:24:59      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:return   tran   array   路由   outer   判断   标记   之间   nsf   

解决问题:

1.切换路由时判断是前进还是后退

2.每次切换左右动画如何实现

方法:

在基于路由的动态过渡中,需要定义路由层级,在路由元信息中增加index标记层级

import Vue from ‘vue‘
import VueRouter, { RouteConfig } from ‘vue-router‘
import ProcessForm from ‘../pages/process-form.vue‘
import LeaderConfirm from ‘../pages/leader-confirm/leader-confirm.vue‘
import Detail from ‘../pages/leader-confirm/detail.vue‘
Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {
    path: ‘/process_form‘,
    name: ‘process_form‘,
    meta: {index: 0},
    component: ProcessForm
  },
  {
    path: ‘/leader_confirm‘,
    name: ‘leader_confirm‘,
    meta: {index: 0},
    component: LeaderConfirm
  },
  {
    path: ‘/detail‘,
    name: ‘detail‘,
    meta: {index: 1},
    component: Detail
  }
]

const router = new VueRouter({
  // mode: ‘history‘,
  // base: process.env.BASE_URL,
  routes
})

export default router

监控路由跳转,进行路由间层级判断出前进还是后退

<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>

增加过渡样式

.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);
}

 

vue route 设置界面之间的过渡动效

标签:return   tran   array   路由   outer   判断   标记   之间   nsf   

原文地址:https://www.cnblogs.com/xuesen1995/p/14754936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!