vue_cli动画

本文详细介绍了如何在 Vue CLI 项目中实现从右向左的路由切换动画,并将其封装在 `router.js` 文件中。通过在父组件和目标组件中设置 CSS 过渡效果,结合 Vue Router 的钩子函数,实现了平滑的页面过渡。同时,文中还展示了如何封装 `go` 和 `push` 方法,以控制不同方向的动画效果。此外,还提供了在组件中传递参数和接收参数的方法。
摘要由CSDN通过智能技术生成

常见动画–从右向左显示

在路由index.js中

{
  path: '/parent',
  name: 'Parent',
  component: Parent,
  children: [
    {
      path: '/parent/ChildrenA',
      name: 'ChildrenA',
      component: ChildrenA
    }
  ]
},

在父组件中

<template>
  <div class="parent">
    <div @click="clickA">点我跳转到ChildrenA界面并且有动画</div>
    <transition> <!-- 给路由出口设置动画 -->
      <router-view></router-view>
    </transition>
  </div>
</template>
<script>
export default {
  name: 'Parent',
  methods: {
    clickA () {
      this.$router.push({
        path: '/parent/ChildrenA'
      })
      console.log('clickA...')
    }
  }
}
</script>
<style lang="scss">
  .parent{
    /*给路由出口设置动画*/
    .v-enter{
      transform: translateX(100%);
    }
    .v-enter-to{
      transform: translateX(0%);
    }
    .v-enter-active{
      transition: transform 0.5s;
    }
    .v-leave{
      transform: translateX(0%);
    }
    .v-leave-to{
      transform: translateX(100%);
    }
    .v-leave-active{
      transition: transform 0.5s;
    }
  }
</style>

在要跳转的目标组件中

<template>
  <div class="childrenA">
    <div @click="$router.go(-1)">返回上一页</div>
    <div>我是ChildrenA</div>
  </div>
</template>
<script>
export default {
  name: 'ChildrenA'
}
</script>
<style lang="scss">
  .childrenA{
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: red;
  }
</style>

结果

点击跳转从右向左显示显示完
123456789

常见动画–封装在router.js

在路由router.js中

// 防止点击重复路由报错
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

// 需要左方向动画的路由用this.$router.togo('****')
VueRouter.prototype.togo = function (pathName, paramsObj) {
  this.isleft = true
  this.isright = false
  this.push({
    name: pathName,
    params: paramsObj
  })
}
// 需要右方向动画的路由用this.$router.goRight('****')
VueRouter.prototype.goRight = function (path) {
  this.isright = true
  this.isleft = false
  this.push(path)
}
// 需要返回按钮动画的路由用this.$router.goBack(),返回上一个路由
VueRouter.prototype.goBack = function () {
  this.isright = true
  this.isleft = false
  this.go(-1)
}
// 点击浏览器返回按钮执行,此时不需要路由回退
VueRouter.prototype.togoback = function () {
  this.isright = true
  this.isleft = false
}
// 点击浏览器前进按钮执行
VueRouter.prototype.togoin = function () {
  this.isright = false
  this.isleft = true
}
// 点击跳转页面并传参数
VueRouter.prototype.goQuery = function (pathName, paramsObj) {
  this.isleft = true;
  this.isright = false;
  this.push({
    path: pathName,
    query: paramsObj
  })
}

在App.vue中

<template>
    <div id="app">
        <transition :name="transitionName">
            <router-view class="Router"></router-view>
        </transition>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                transitionName: "",
            };
        },
        watch: {
            $route() {
                if (this.$router.isleft) {
                    this.transitionName = "slideleft";
                }
                if (this.$router.isright) {
                    this.transitionName = "slideright";
                }
            },
        },
    }
</script>

<style lang="scss">
    .Router {
        /*background: #f7f5f5;*/
        /*display: flex;*/
        /*flex-direction: column;*/
        // justify-content: space-between;
        /*align-items: stretch;*/
        width: 100%;
        min-height: 100vh;
        transition: all 0.5s ease;
        -webkit-transition: all 0.5s ease;
        -moz-transition: all 0.5s ease;
    }
    .slideleft-enter, .slideright-leave-active {
        opacity: 1;
        transform: translate3d(90%, 0, 0);
        -webkit-transform: translate3d(90%, 0, 0);
        -moz-transform: translate3d(90%, 0, 0);
    }
    .slideleft-leave-active, .slideright-enter {
        opacity: 1;
        transform: translate3d(-90%, 0, 0);
        -webkit-transform: translate3d(-90%, 0, 0);
        -moz-transform: translate3d(-90%, 0, 0);
    }
    /* 公共样式 */
    #app {
        font-family: Avenir, Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        color: #333;
    }
</style>

在要跳转的组件中

<template>
    <div class="test">
        <div v-for="(item, i) in myData" :key="i">
            <van-button @click="navTo('/parent', item)">跳转parent</van-button>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Test",
        data () {
            return {
                myData: [
                    {
                        name: "陈",
                        age: 18
                    },
                ]
            }
        },
        methods: {
            navTo (url, params) {
                console.log(params);
                this.$router.goQuery(url, { params: JSON.stringify(params) });
            },
        }
    }
</script>

<style lang="less">
.test{
    width: 100%;
    height: 100%;
    background-color: #999;
}
</style>

在要接收的组件中

<template>
    <div class="parent">
        <div>我是parent</div>
        <div>我接收到url传递的params是: {{paramsData}}</div>
    </div>
</template>

<script>
    export default {
        name: "Parent",
        data () {
            return {
                paramsData: {}
            }
        },
        mounted() {
            this.paramsData = JSON.parse(this.$route.query.params);
            console.log(this.paramsData);
        }
    }
</script>

<style lang="less">
.parent{
    width: 100%;
    height: 100%;
    background-color: #ccc;
}
</style>

结果

点击跳转从右向左显示显示完
在这里插入图片描述在这里插入图片描述在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上暴富

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值