Vue实现H5页面左右切换动画

整体思路:采用Vue transition实现动画,在路由配置加上meta属性,用来判断是前一页还是后一页,前进的时候页面向左滑动,后退的时候页面向右滑动,所以要用两套动画实现,meta属性就用来判断用的是前进动画还是后退动画,直接上代码。

Vue入口文件,index.vue

<template>
  <div id="app">
    <transition :name="isGoNext">
      <router-view></router-view>
    </transition>
  </div>
</template>
export default {
  data() {
    return {
      viewMeta : 0,
      isGoNext : 'loan-go',
    }
  },
  watch: {
    '$route.matched': function (newVal, oldVal) {
      let meta = this.$route.matched[0].meta;
      this.isGoNext = meta >= this.viewMeta ? 'loan-go' : 'loan-back'
      this.viewMeta = meta
    }
  },
  mounted(){
    this.viewMeta = 1
  }
};
</script>
<style lang="scss">
@import "@/style/public/publicStyle.scss";
</style>

transition动画 publicStyle.scss文件

html,body,#app {
  height: 100%;
  background: #fff;
  font-family: '微软雅黑','MicrosoftYaHei', Helvetica, Arial, sans-serif;
}
.loan-go-enter-active, 
.loan-go-leave-active, 
.loan-back-enter-active, 
.loan-back-leave-active {
  transition: all 0.3s;
}
.loan-go-enter , .loan-back-leave-to{
  position: fixed;
  transform: translateX(100%);
}
.loan-back-enter , .loan-go-leave-to{
  position: fixed;
  transform: translateX(-100%);
}

在路由里面配置meta属性,用来判断页面的先后顺序,routers/index.js

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        name: 'MyLoan',
        component: () => import( '@/views/my_loan/MyLoan.vue'),
        meta:1
    },
    {
        path: '/ApplyForLoan/:lenderPhone/:outNetToken/:misToken/:terminal',
        name: 'ApplyForLoan',
        props:true,
        component: () => import( '@/views/my_loan/ApplyForLoan.vue'),
        meta:2
    },
    {
        path: '/Success/:successType',
        name: 'Success',
        props:true,
        component: () => import( '@/views/my_loan/Success.vue'),
        meta:3
    },
    {
        path: '/LoanDetails/:dataId/:terminal/:misToken',
        name: 'LoanDetails',
        props:true,
        component: () => import( '@/views/my_loan/LoanDetails.vue'),
        meta:4
    },
];

const router = new VueRouter({
  routes
})

export default router;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值