如何给页面加入场和退场的动画
思路:利用 vuex 将路由保存在队列中,前进的时候压入队列,后退的时候移出队列 用到的技术:Vue Vuex VueRouter Van
1. 监听路由的beforeEach 事件
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
mode: 'hash',
base: process.env.BASE_URL,
routes: [
]
})
router.beforeEach((to, from, next) => {
store.dispatch('setRouter', { to, from }).then(() => next())
})
export default router
2. 在setRouter 中处理入场和退场的操作,这个是vuex 中的action 方法
import Vue from Vue;
import Vuex from 'vuex'
Vue.use(vuex)
const appModule = new Vuex.Store({
state: {
name: 'van-slide-left',
history: []
},
mutations: {
historyPush (state, route) {
state.history.push(route)
state.name = 'van-slide-left'
},
historyPop (state, route) {
state.history = state.history.filter((_) => _.fullPath !== route.fullPath)
state.name = 'van-slide-right'
}
},
actions: {
setRouter ({ commit, state }, payload) {
const fullPaths = state.history.map((_) => _.fullPath)
if (!fullPaths.includes(payload.to.fullPath)) {
commit('historyPush', payload.from)
} else {
commit('historyPop', payload.to)
}
}
},
getters: {
getAnimationName (name) {
return state.name
}
}
})
export default appModule
3. 路由监听和路由的动画已经确认,使用动画
import Vue from 'vue'
import App from './App'
import router from './router'
import state from './state'
new Vue({
router,
state,
render: h => h(App)
}).$mount('#app')
4. App.vue 中使用transition 加上动画效果
<template>
<div id="app">
<transition :name="getAnimationName">
<keep-alive><router-view /></keep-alive>
</transition>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters(['getAnimationName'])
}
}
</script>
5. ??注意,以上动画可能导致页面抖动,可以在App.vue 中加上如下样式
.van-slide-left-leave-to,
.van-slide-right-leave-to {
display: none;
}
|