vue实现不同页面跳转的左滑右滑效果切换效果
1、在app.vue增加代码如下
<template> <div id="app"> <transition :name="transitionName"> <router-view class="views" /> </transition> </div> </template> <script> export default { name: 'App', data() { return {} }, computed: { transitionName () { return this.$store.state.transitionName } } } </script> <style lang="scss"> /*切换页面时的滑动效果*/ .views { position: absolute; width: 100%; transition: all .8s ease; top: 0; } .slide-left-enter-active { transition: all .3s ease; } .slide-left-leave-active { transition: all .4s ease; } .slide-left-enter { transform: translateX(100%); opacity: 0; } .slide-left-leave-to { transform: translateX(-100%); opacity: 0; } .slide-right-enter-active { transition: all .3s ease; } .slide-right-leave-active { transition: all .4s ease; } .slide-right-enter { transform: translateX(-100%); opacity: 0; } .slide-right-leave-to { transform: translateX(100%); opacity: 0; } </style>
main.js文件
import store from './store' // 路由跳转前进后退动画,Vue原型上定义transition为动画效果 router.beforeEach((to, from, next) => { store.commit('changeTransition', 'slide-none') if (from.meta.tree > to.meta.tree) { // 后退,想右滑动 store.commit('changeTransition', 'slide-right') } else if (from.meta.tree < to.meta.tree) { // 前进,想左滑动 store.commit('changeTransition', 'slide-left') } else { // 同一层级,无动画 store.commit('changeTransition', 'slide-none') } next() })
store文件
index.js
import Vue from 'vue' import Vuex from 'vuex' import state from './state' import mutations from './mutations' Vue.use(Vuex) export default new Vuex.Store({ state, mutations })
mutations.js
export default { changeTransition (state, transitionName) { state.transitionName = transitionName } }
state.js
export default { changeTransition (state, transitionName) { state.transitionName = transitionName } }
组件里面是content布局
<div :class="style"> <v-header title="每日成绩"> // 固定头部 </v-header> <div class="content"> // 滚动容器 </div> </div>