vue 路由之左右滑动效果

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_39197547/article/details/82699194

 keep-alive 是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM,我们要把它也包裹在transition标签内,否则页面将重新渲染,切换的动画也会卡顿

<transition :name="transitionName">
          <keep-alive>
              <router-view class="transitionBody"></router-view>
          </keep-alive>
        </transition>

通过watch来处理路由跳转是左是右

 data () {
    return {
      transitionName: 'transitionLeft'
    }
  },
  watch: {
    '$route' (to, from) {
      const arr = ['NewsPage', '/jokePage', '/jokePage', '/myPage']
      const compare = arr.indexOf(to.path) > arr.indexOf(from.path)
      this.transitionName = compare ? 'transitionLeft' : 'transitionRight'
    }
  }
}

css样式

.transitionBody{
 transition: all 0.4s ease-out; /*定义动画的时间和过渡效果*/
}
.transitionLeft-enter,
.transitionRight-leave-active {
  -webkit-transform: translate(100%, 0);
  transform: translate(100%, 0);
   /*当左滑进入右滑进入过渡动画*/
}
.transitionLeft-leave-active,
.transitionRight-enter {
  -webkit-transform: translate(-100%, 0);
  transform: translate(-100%, 0);
}

看效果点这里https://wangweiruning.github.io/vue_scorll

猜你喜欢

转载自blog.csdn.net/qq_39197547/article/details/82699194
今日推荐