VUE路由轉場特效,WebAPP的前進與後退

1、效果圖css

 

 

2、思路vue

 1. 定義兩個 CSS 過分動畫,前進與後退: slide-right-enter   和   slide-left-entervue-router

 2. 給路由配置meta信息,設置各個路由的級別: indexapp

 3. 監控路由跳轉,比對 meta 信息級別,若是從大跳轉到小說明是返回,從小跳轉到大則是前進ide

 

3、具體代碼動畫

APP.vuethis

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

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 }
    }
  ]
})
相關文章
相關標籤/搜索