Vue移动端页面切换动画效果

//App中

<template>
  <div id="app" class="Router">

 <transition :name="transitionName">
			<router-view></router-view>
		</transition>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      transitionName:''
    }
  },
  watch: {//使用watch 监听$router的变化
    $route(to, from) {
      //如果to索引大于from索引,判断为前进状态,反之则为后退状态
      if(to.meta.index > from.meta.index) {
        //设置动画名称
        this.transitionName = 'slide-left';
      }else{
        this.transitionName = 'slide-right';
      }
    }
  }
}
</script>
<style lang="scss">
#app{
  width: 100%;
  height: 100%;
}
.Router{
position: relative;
}
.Router>*{
position: absolute;
width: 100%;
}
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
 will-change: transform;
 transition: all 1500ms;
 position: absolute;
}
.slide-right-enter {
 opacity: 0;
 transform: translate3d(-100%, 0, 0);
}
.slide-right-leave-active {
 opacity: 0;
 transform: translate3d(100%, 0, 0);
}
.slide-left-enter {
 opacity: 0;
 transform: translate3d(100%, 0, 0);
}
.slide-left-leave-active {
 opacity: 0;
 transform: translate3d(-100%, 0, 0);
}
</style>

//router中定义index

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


Vue.use(VueRouter)
import route from './route'

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/login'),
    meta:{
      index:1
    }
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('@/views/login/detail'),
    meta:{
      index:2
    }
  },
  {
    path: '/city',
    name: 'city',
    component: () => import('@/views/login/city'),
    meta:{
      index:1
    }
  },
  ...route
]

const router = new VueRouter({
  routes
})

export default router
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值