vue2路由页面切换动画

<template>
  <div id="app">
        <!-- 页面切换动画transitionName -->
        <transition :name="transitionName">
            <!-- 缓存数据 <router-view> -->
            <keep-alive>
                <router-view v-if="$route.meta.keepAlive"></router-view>
            </keep-alive>
        </transition>
        <transition :name="transitionName">
            <router-view v-if="!$route.meta.keepAlive"></router-view>
        </transition>
    
  </div>
</template>

<script>

export default {
  name: 'App',
    data() {
        return {
            transitionName: ""
        }
    },
    watch: {
        $route(to, from) {
            if (to.meta.index > 0 && from.meta.index) {
                if (to.meta.index < from.meta.index) {
                    this.transitionName = "slide-right";
                } else {
                    this.transitionName = "slide-left";
                }
            } else if (to.meta.index == 0 && from.meta.index > 0) {
                this.transitionName = "";
            }
        }
    }
}
</script>
/* 页面切换动画 */
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
    /* will-change属性可以提前通知浏览器我们要对元素做什么动画,这样浏览器可以提前准备合适的优化设置 */
    will-change: transform;
    transition: all ease 0.4s;
    -webkit-transition: all ease 0.4s;
    position: absolute;
    width: 100%;
    left: 0;
}

.slide-right-enter {
    transform: translateX(-100%);
    -webkit-transform: translateX(-100%);
}

.slide-right-leave-active {
    transform: translateX(100%);
    -webkit-transform: translateX(100%);
}

.slide-left-enter {
    transform: translateX(100%);
    -webkit-transform: translateX(100%);
}

.slide-left-leave-active {
    transform: translateX(-100%);
    -webkit-transform: translateX(-100%);
}

 

posted @ 2022-06-29 11:17  秃头的铲屎官  Views(1390)  Comments(0Edit  收藏  举报