vue?路由切換過渡動效滑入滑出效果的實例代碼
更新時間:2022年03月29日 10:06:56 作者:想上天與太陽并肩
在支付寶賬單頁面有這樣一個特效切換過渡動效滑入滑出效果,非常方便實用,那么這個功能如何實現的呢?下面小編通過vue實現路由切換過渡動效滑入滑出效果,感興趣的朋友一起看看吧
效果展示

css 滑入和滑出的動畫
.twofade-enter {transform: translateX(100%);}
.twofade-enter-active {transition: all 0.3s;position: absolute;height:100%;background:#ececec;}
.twofade-leave-active {transition: all 0;transition-delay: 0.3s;position: absolute;}
.twofade-leave-to {transform: translateX(-100%);}
.threefade-enter {transform: translateX(-100%);}
.threefade-leave-to {transform: translateX(100%);}
.threefade-enter-active {transition: all 0s;position: absolute;z-index: 2;}
.threefade-leave-active {transition: all .3s;position: absolute;z-index: 999;height: 100%;background:#ececec;}transition
使用 vue提供的 transition 標簽,data中定義 transitionName 變量
<template>
<div id="app">
<transition :name="transitionName">
<router-view></router-view>
</transition>
</div>
</template>
export default {
name:"App",
data(){
return{
transitionName:""
}
}
}watch 監(jiān)聽路由的變化
通過監(jiān)聽路由的變化 知道是返回還是打開新頁面 在通過在變量 transitionName 賦不同的值改變動畫
watch:{
$route(to, from) {
if(to.meta.index > from.meta.index){
this.transitionName = 'twofade';
}else if(to.meta.index < from.meta.index){
this.transitionName = 'threefade';
}
}
}可能遇到的問題
關于樣式 操作上在切換中可能會有遇到樣式的問題 需要調整樣式來達到自己需要的效果
我的解決方法是
#app{//width: 100%;height: 100%;overflow-x: hidden;position: absolute;
&>div{width: 100%;min-height: 100vh;}
}到此這篇關于vue 路由切換過渡動效 滑入 滑出效果的文章就介紹到這了,更多相關vue 路由過渡動效內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

