vue路由切换动画(转场动画)

通过transition 更改效果

html 复制代码
<template>
    <el-container class="h-100">
        <el-main>
            <transition :name="transitionName">
                <!-- <keep-alive v-if="$route.meta.keepAlive"> -->
                <router-view :key="$route.fullPath" />
                <!-- </keep-alive> -->
            </transition>
        </el-main>
    </el-container>
</template>

<script>
 export default {

    data() {
        return {
            transitionName: 'transitionLeft',
        };
    },
      watch: {
        '$route'(to, from) {
            const arr = ['coverSheet', 'home', 'product', 'about',]
            let toPath = to.path.split('/')[to.path.split('/').length - 1]
            let fromPath = from.path.split('/')[from.path.split('/').length - 1]

            if (toPath === 'coverSheet') {
                this.transitionName = 'transitionScale'
                return
            }
            if (fromPath === 'coverSheet' && toPath === 'home') {
                this.transitionName = 'transitionScale'
                return
            }
            //内页就直接切换
            if (arr.indexOf(toPath) == -1) {
                this.transitionName = 'transitionFade'
                return
            }	
            //判断,应该是左划还是右划效果,arr定义从左到右的页面路径
            this.transitionName = arr.indexOf(toPath) > arr.indexOf(fromPath) ? 'transitionLeft' : 'transitionRight'
        }
    },

}
</script>

<style lang="scss" scoped>
.transitionLeft-enter,
.transitionRight-leave-active {
    -webkit-transform: translate(100%, 0);
    transform: translate(100%, 0);
    transition: all 0.4s ease-out;
}

.transitionLeft-leave-active,
.transitionRight-enter {
    -webkit-transform: translate(-100%, 0);
    transform: translate(-100%, 0);
    transition: all 0.4s ease-out;
}

.transitionLeft-enter-active,
.transitionRight-enter-active {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    transition: all 0.4s ease-out;
}

.transitionScale-leave-active {
    transform: scale(1);
    transition: all 0.4s ease-out;
}

.transitionScale-enter {
    -webkit-transform: translate(-100%, 0);
    transform: translate(-100%, 0);
    transform: scale(0.4);
    transition: all 0.4s ease-out;
}

.transitionScale-enter-active {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    transition: all 0.4s ease-out;
    bottom: 0;
}


.transitionFade-enter-from,
.transitionFade-leave-to {
    opacity: 0;
}

.transitionFade-enter-to,
.transitionFade-leave-from {
    opacity: 1;
}

.transitionFade-enter-active,
.transitionFade-leave-active {
    transition: opacity 0.01s linear 0s;
}
</style >
相关推荐
老码沉思录1 小时前
写给初学者的React Native 全栈开发实战班
javascript·react native·react.js
我不当帕鲁谁当帕鲁1 小时前
arcgis for js实现FeatureLayer图层弹窗展示所有field字段
前端·javascript·arcgis
那一抹阳光多灿烂1 小时前
工程化实战内功修炼测试题
前端·javascript
红中马喽4 小时前
JS学习日记(webAPI—DOM)
开发语言·前端·javascript·笔记·vscode·学习
Black蜡笔小新5 小时前
网页直播/点播播放器EasyPlayer.js播放器OffscreenCanvas这个特性是否需要特殊的环境和硬件支持
前端·javascript·html
Dread_lxy7 小时前
vue 依赖注入(Provide、Inject )和混入(mixins)
前端·javascript·vue.js
涔溪7 小时前
Ecmascript(ES)标准
前端·elasticsearch·ecmascript
奔跑草-8 小时前
【前端】深入浅出 - TypeScript 的详细讲解
前端·javascript·react.js·typescript
羡与8 小时前
echarts-gl 3D柱状图配置
前端·javascript·echarts
咔咔库奇8 小时前
ES6进阶知识一
前端·ecmascript·es6