VUE路由转场特效,WebAPP的前进与后退

更新日期: 2019-02-17阅读: 4.6k标签: webapp

一、效果图

 

 

二、思路

 1. 定义两个 css 过度动画,前进与后退: slide-right-enter   和   slide-left-enter

 2. 给路由配置meta信息,设置各个路由的级别: index

 3. 监控路由跳转,比对 meta 信息级别,如果从大跳转到小说明是返回,从小跳转到大则是前进

 

三、具体代码

APP.vue

<template>
  <div id="app">
    <transition :name="transitionName">
      <router-view/>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      transitionName: ''
    }
  },
  watch: {
    $route (to, from) {
      if (to.meta.index > from.meta.index) {
        this.transitionName = 'slide-left'
      } else {
        this.transitionName = 'slide-right'
      }
    }
  }
}
</script>

<style lang="scss">
#app {
  color: #2c3e50;
}

//转场动画
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  // 启用硬件加速
  will-change: transform;
  transition: all 300ms;
  position: fixed;
}
.slide-right-enter {
  transform: translate(-100%, 0);
  transition-timing-function: ease-in;
}
.slide-left-enter {
  transform: translate(100%, 0);
  transition-timing-function: ease-in;
}
</style>

router.js 

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'

Vue.use(Router)

export default new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
      meta: { index: 1 }
    },
    {
      path: '/publish',
      name: 'publish',
      component: () => import('./views/Publish.vue'),
      meta: { index: 2 }
    },
    {
      path: '/personal',
      name: 'personal',
      component: () => import('./views/Personal.vue'),
      meta: { index: 2 }
    }
  ]
})


 来自:https://www.cnblogs.com/similar/archive/2019/02/17/10329667.html


链接: https://fly63.com/article/detial/2043

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!