vue3.x移动端页面基于vue-router的路由切换动画

移动端页面切换一般都具有动画,我们既然要做混合开发,做完之后还是不能看起来就像一个网页,所以我们基于vue-router扩展了一个页面切换push和pop的动画。这是一篇比较硬核的帖子,作者花了不少精力来写

先上效果图

路由切换动画.gif

再贴核心代码

router文件夹下,新建transition-extend.js文件,实现如下:

/**
 * router扩展,页面切换动画
 */
// 负责SessionStorage存储路由历史。
const SessionStorage_key_Router_Extend_History = 'SessionStorage_key_Router_Extend_History'

function transitionExtend(orgin) {
  // 通过原路由对象创建一个新的对象
  let router = Object.create(orgin)

  // 扩展对象,保存当前栈数组和过渡动画名称
  router.customRouterData = {
    transitionName: '',
    history: []
  }

  // 路由位置字符串在数组中的位置
  router.indexOf = function (path) {
    let arrLen = router.customRouterData.history.length
    for (let i = arrLen - 1; i >= 0; i--) {
      if (router.customRouterData.history[i] == path) {
        return i;
      }
    }
    return -1;
  }

  // 添加历史路由去路由数组
  router.addRouterPath = function(path) {
    router.customRouterData.history.push(path)

    sessionStorage.setItem(SessionStorage_key_Router_Extend_History, JSON.stringify(router.customRouterData.history));
  }

  // 历史路由数组移除某个路由,n为参数可以移除多个
  router.removeLastRouterPath = function (n = 1) {
    if (n > 0) {
      for (let i = 0; i < n; i++) {
        router.customRouterData.history.pop()
      }

      sessionStorage.setItem(SessionStorage_key_Router_Extend_History, JSON.stringify(router.customRouterData.history));
    }
  }

  // 初始化,为了页面刷新能恢复路由记录等
  router.initRouterPaths = function (toPath) {
    // 当存储了 router paths 时候,读取并赋值
    let arrStr
    arrStr = sessionStorage.getItem(SessionStorage_key_Router_Extend_History);

    if (arrStr && arrStr != undefined) {
      let arr = JSON.parse(arrStr)
      if (Array.isArray(arr) && arr.length > 0) {
        // 进入页面
        router.customRouterData.history = arr;
      } else {
        // 新进入页面
        router.customRouterData.history = []
        router.customRouterData.history.push(toPath)
      }
    } else {
      // 新进入页面
      router.customRouterData.history = []
      router.customRouterData.history.push(toPath)
    }

    // 存储为了恢复
    sessionStorage.setItem(SessionStorage_key_Router_Extend_History, JSON.stringify(router.customRouterData.history));
  }

  // push 修改路由历史,并设置动画
  router.push = function () {

    let location = arguments[0]
    if (typeof location == 'string') {
      router.addRouterPath(location)
    } else {
      router.addRouterPath(location.path)
    }

    router.customRouterData.transitionName = 'slide_left'

    router.__proto__.push.call(this, ...arguments)
  };

  // replace 修改路由历史,并设置动画
  router.replace = function () {

    router.removeLastRouterPath()
    let location = arguments[0]
    if (typeof location == 'string') {
      router.addRouterPath(location)
    } else {
      router.addRouterPath(location.path)
    }

    router.customRouterData.transitionName = 'slide_left'

    router.__proto__.replace.call(this, ...arguments)
  };

  // go 修改路由历史,并设置动画
  router.go = function (n) {
    if (n > 0) {
      // 禁止使用,这种情况比较复杂,使用较少,先忽略
      console.error('router.go 暂不支持 前进 !');
      return;
    }
    router.removeLastRouterPath(-n)

    router.customRouterData.transitionName = 'slide_right'

    router.__proto__.go.call(this, n)
  };

  // back 修改路由历史,并设置动画
  router.back = function () {

    router.removeLastRouterPath()

    router.customRouterData.transitionName = 'slide_right'

    router.__proto__.go.call(this, -1)
  };

  router.forward = function () {
    // 禁止使用,这种情况比较复杂,使用较少,先忽略
    console.error('router.forward 暂不支持 !');
    return ;
  };

  /**
   * 按钮前进后退处理处理
   * 返回:测滑返回,微信返回按钮,web返回按钮,以及android物理返回,android测滑返回
   * 前进:微信上的前进按钮,web前进
   * // 前进这里有个坑,待解决,先忽略
   **/
  router.otherEventTransitionName = function (toPath, fromPath) {
    if (router.customRouterData.transitionName != '') {
      // 没有数据意味着从,其他操作方式得到的路由变化
      return;
    }

    let toIndex = router.indexOf(toPath)
    if (toIndex == -1 || router.customRouterData.history.length - toIndex != 2) {
      // 不存在,并且历史
      router.addRouterPath(toPath)
      router.customRouterData.transitionName = 'slide_left'
    } else {
      router.removeLastRouterPath()
      router.customRouterData.transitionName = 'slide_right'
    }
  }

  // 是否已经初始化
  let isInit = false;

  // 跳转之前
  router.beforeEach((to, from, next) => {
    if (isInit) {
      router.otherEventTransitionName(to.path, from.path)
    } else {
      isInit = true;
      router.initRouterPaths(to.path)
    }
    next();
  })

  // 跳转之后
  router.afterEach((to, from) => {
    setTimeout(() => {
      // 使用动画之后立即移除
      router.customRouterData.transitionName = ''
    }, 300)
  })

  return router
}

export default transitionExtend

使用

1、对全局router对象进行扩展,一般在router/index.js里面

// 引入扩展函数
import transitionExtend from "./transition-extend";

// 对router对象扩展
router = transitionExtend(router)

// export 扩展后的路由对象
export default router

2、为router-view添加过渡动画,一般在app.vue里面

<template>
  <router-view v-slot="{ Component }">
    <transition :name="$router.customRouterData.transitionName">
      <component :is="Component" />
    </transition>
  </router-view>
</template>

<script>
  export default {
    name: 'app'
  }
</script>

<style lang="stylus" scoped type="text/stylus">
  #app {
    position relative;
    width 100%;
    height 100%;
  }

  .slide_left-enter-active, .slide_left-leave-active, .slide_right-enter-active, .slide_right-leave-active {
    transition: all 0.3s;
    position absolute !important;
    background-color white;
    left 0;
    right 0;
    top 0;
    bottom 0;
    z-index 1;
  }

  .slide_left-enter-from, .slide_right-leave-to {
    opacity 1;
    transform: translateX(100%);
  }

  .slide_right-enter-from, .slide_left-leave-to {
    opacity 1;
    transform: translateX(-100%);
  }

  .slide_left-leave-to, .slide_right-leave-to {
    opacity 0.3;
  }
</style>

3、现在我们正常使用路由切换就可以看到路由动画了,赶快试试吧。

this.$router.push({
  path: '/test'
})

原创不易,转载请注明出处,有什么疑问欢迎留言指导。谢谢

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个HTML动画放大菜单,可以按照以下步骤进行设计和实现: 1. 在HTML文件中创建一个菜单按钮,用来触发菜单的显示和隐藏。 2. 创建一个包含菜单项的无序列表,并使用CSS设置它们的样式。每个菜单项应包含一个图标和一个文本标签。 3. 使用CSS设置菜单的初始样式,包括隐藏和缩小的状态。可以使用transform属性来缩放菜单。 4. 使用JavaScript编写一个函数,用来在菜单按钮被点击时切换菜单的显示和隐藏状态。在这个函数中,可以使用CSS的transform属性来放大或缩小菜单。 5. 使用CSS3的过渡效果和动画效果,为菜单的放大和缩小添加动画效果。可以使用transition和animation属性来实现这些效果。 6. 最后,测试并调试菜单的效果,确保它能够正常工作并具有良好的用户体验。 下面是一个简单的HTML动画放大菜单的示例代码,供参考: ```html <!DOCTYPE html> <html> <head> <title>HTML动画放大菜单</title> <style> .menu-btn { display: block; width: 30px; height: 30px; background-color: #333; color: #fff; text-align: center; line-height: 30px; cursor: pointer; } .menu { display: none; position: absolute; top: 50px; left: 50px; transform: scale(0.1); transition: transform 0.3s ease-in-out; } .menu.show { display: block; transform: scale(1); } .menu-item { display: block; padding: 10px; background-color: #333; color: #fff; cursor: pointer; } .menu-item:hover { background-color: #444; } .menu-item i { display: inline-block; width: 20px; height: 20px; margin-right: 10px; background-color: #fff; color: #333; text-align: center; line-height: 20px; border-radius: 50%; } @keyframes menu-show { from { transform: scale(0.1); } to { transform: scale(1); } } </style> </head> <body> <div class="menu-btn" onclick="toggleMenu()">☰</div> <ul class="menu"> <li class="menu-item"><i class="fa fa-home"></i>首页</li> <li class="menu-item"><i class="fa fa-user"></i>个人资料</li> <li class="menu-item"><i class="fa fa-cog"></i>设置</li> <li class="menu-item"><i class="fa fa-sign-out"></i>退出</li> </ul> <script> function toggleMenu() { var menu = document.querySelector('.menu'); menu.classList.toggle('show'); } </script> </body> </html> ``` 在这个示例中,菜单按钮使用了一个Unicode编码的三条横线符号(☰),并设置了一些基本的样式。菜单使用一个无序列表来实现,并包含了四个菜单项,每个菜单项包含一个图标和一个文本标签。菜单使用了CSS3的transform属性来实现放大和缩小的效果,同时使用了transition属性来添加过渡效果,使用animation属性来添加动画效果。JavaScript函数toggleMenu()用来切换菜单的显示和隐藏状态,通过classList.toggle()方法来添加和移除show类名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值