Vue页面跳转动画效果实现

页面的前进后退,想要有前推和后推的运动效果,需要在每次路由触发的时候,来判断到底该使用前推还是后推的动画,所以就需要在每一个页面的路由上手动设置一个值,即用这个值来判断页面跳转的方向。

roter.js

meta 中的 index 就是上面提到的那个值,按照页面的先后顺序分别提前写好

export default new Router({
  mode: 'history',
  base: 'view',
  routes: [
    {
      path: '/login',
      name: 'login',
      meta:{
        index:1,
        title: '登陆/注册',
        auth:true,
      },
      component: () => import('./views/Login.vue')
    },
    {
      path: '/', //个人中心
      name: '',
      meta:{
        index:2,
        title:'个人中心',
        auth:true,
      },
      component: () => import('./views/PersonalCenter.vue'),
    },
    {
      path: '/personalInformation',//个人信息
      name: 'personalInformation',
      meta:{
        index:3,
        title:'个人信息',
        auth:true,
      },
      component: () => import('./views/PersonalInformation.vue')
    },
    {
      path: '/changeUserPhoneNum',//修改个人信息
      name: 'changeUserPhoneNum',
      meta:{
        index:4,
        title:'修改个人信息',
        auth:true,
      },
      component: () => import('./views/ChangeUserPhoneNum.vue')
    },
  ]
})

app.vue

在使用的时候,需要用watch 监听$router的变化,如果to索引大于from索引,判断为前推状态,反之则为后推状态,剩下的就是css了

<template>
  <div id="app">
    <transition :name="transitionName">
      <keep-alive include="placeOrder">
        <router-view/>
      </keep-alive>
    </transition>
  </div>
</template>

script>
export default {
  data () {
    return {
      transitionName: 'fold-left'
    }
  },
  watch: {//使用watch 监听$router的变化
    $route(to, from) {
      //如果to索引大于from索引,判断为前进状态,反之则为后退状态
      if(to.meta.index > from.meta.index){
        this.transitionName = 'fold-left';
        }else{
        this.transitionName = 'fold-right';
      }
    },
  }
}
</script>


<style>
@import url(./assets/css/base.css);
html,body,#app {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
  margin: 0;
  padding: 0;
  overflow: auto;
}

.fold-left-enter-active {
    animation-name: fold-left-in;
    animation-duration: .3s;
  }
  .fold-left-leave-active {
    animation-name: fold-left-out;
    animation-duration: .3s;
  }
  @keyframes fold-left-in {
    0% {
      -webkit-transform: translate3d(100%, 0, 0);
      transform: translate3d(100%, 0, 0);
      /* visibility: visible; */
    }
    /*50% {
      transform: translate3d(50%, 0, 0);
    }*/
    100% {
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
  }
  @keyframes fold-left-out {
    0% {
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
    /*50% {
      transform: translate3d(-50%, 0 , 0);
    }*/
    100% {
      -webkit-transform: translate3d(-100%, 0, 0);
      transform: translate3d(-100%, 0, 0);
      /* visibility: hidden; */
    }
  }
  .fold-right-enter-active {
    animation-name: fold-right-in;
    animation-duration: .3s;
  }
  .fold-right-leave-active {
    animation-name: fold-right-out;
    animation-duration: .3s;
  }
  @keyframes fold-right-in{
    0% {
      width: 100%;
      -webkit-transform: translate3d(-100%, 0, 0);
      transform: translate3d(-100%, 0, 0);
      /* visibility: visible; */
    }
    /*50% {
      transform: translate3d(50%, 0, 0);
    }*/
    100% {
      width: 100%;
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
  }
  @keyframes fold-right-out  {
    0% {
      width: 100%;
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
    /*50% {
      transform: translate3d(-50%, 0 , 0);
    }*/
    100% {
      width: 100%;
      -webkit-transform: translate3d(100%, 0, 0);
      transform: translate3d(100%, 0, 0);
      /* visibility: hidden; */
    }
  }
</style>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • vue-cli搭建项目 确保安装了node与npm 再目标文件夹下打开终端 执行cnpm i vue-cli -g...
    Akiko_秋子阅读 8,509评论 1 22
  • Laravel 学习交流 QQ 群:375462817 本文档前言Laravel 文档写的很好,只是新手看起来会有...
    Leonzai阅读 12,704评论 2 12
  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 13,301评论 1 52
  • VUE Vue :数据驱动的M V Vm框架 m :model(后台提供数据),v :view(页面),vM(模板...
    wudongyu阅读 10,766评论 0 11
  • 路由是实现模块间解耦的一个有效工具。如果要进行组件化开发,路由是必不可少的一部分。目前iOS上绝大部分的路由工具都...
    黑超熊猫zuik阅读 9,397评论 8 52

友情链接更多精彩内容