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

再贴核心代码
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'
})
原创不易,转载请注明出处,有什么问题欢迎留言指导。谢谢
vue3.x移动端页面基于vue-router的路由切换动画的更多相关文章
- vue router 配合transition 切换动画
把<router-view>嵌套在<transition>里,路由变化的时候,vue会为包裹页面的div增加动画样式,我们要做的就是监听路由变化.定义这些动画样式,以规定页面到 ...
- vue 2.0 路由切换以及组件缓存源代码重点难点分析
摘要 关于vue 2.0源代码分析,已经有不少文档分析功能代码段比如watcher,history,vnode等,但没有一个是分析重点难点的,没有一个是分析大命题的,比如执行router.push之后 ...
- Vue 中的Vue Router一级路由,二级路由,三级路由以及跳转
今天编写了一下Vue中的路由 先用命令行新建一个空的项目,并且我知道要用路由,就下载了路由的相关依赖 vue init webpack demo5 完毕之后进入所在的项目 cd demo5 之后用vs ...
- Vue router 一个路由对应多个视图
使用命名路由 https://jsfiddle.net/posva/6du90epg/ <script src="https://unpkg.com/vue/dist/vue.js&q ...
- 基于Vue开发的tab切换组件
github地址:https://github.com/MengFangui/VueTabSwitch 1.index.html <!DOCTYPE html> <html lang ...
- Vue router 全局路由守卫
记录一下全局路由守卫的使用: 方法一:定义一个数组用于检测与管理需要登录的页面,全局路由守卫配合本地存储判断是否跳转 import Vue from 'vue' import Router from ...
- Vue中解决路由切换,页面不更新的实用方法
前言:vue-router的切换不同于传统的页面的切换.路由之间的切换,其实就是组件之间的切换,不是真正的页面切换.这也会导致一个问题,就是引用相同组件的时候,会导致该组件无法更新,也就是我们口中的页 ...
- vue router动态路由
<div id="#app"> <router-link to="/user/header">路由1</router-link&g ...
- vue router引入路由与路由配置容易犯错的地方与常见的报错与处理报错
首先npm安装vue-router插件,就不说了其次: 先看下我本地的目录结构吧 第一步:在src目录下新建一个专门存放router的index.js文件里面的内容为: import Vue from ...
随机推荐
- MySQL版本浅介
一.关于MySQL发行版介绍: 1. MySQL官方发行版 MySQL是最流行的数据库,主要特点: 简单:MySQL使用很简单,可以无师自通地参照文档安装运行和使用MySQL,几乎没有什么门槛. 开源 ...
- Intel® QAT 加速卡之IPSec示例
Intel QAT 加速卡之IPSec示例 文章目录 Intel QAT 加速卡之IPSec示例 1. QAT处理IPSec入站报文 2. QAT处理IPSec出站报文 3. 组织架构 4. 示例源码 ...
- github上使用C语言实现的线程池
网上介绍线程池的知识很多,但是在代码实现上介绍的又不是那么多.而且给人的一种感觉就是:你的这种实现是正规的方式还是你自己的实现? 如果有这么个疑问,且想找一个靠谱的代码拿来使用,那么这个项目是个不错的 ...
- JS011. 身份证号码校验(仅34行)
身份证格式 六位数字地址码 + 八位数字出生日期码 + 三位数字顺序码 + 一位数字校验码 checkIdCard.js checkIdCard: function (idCard){ //15位和1 ...
- javascript对象——基本对象
1.array对象 2.Boolean包装类对象和Date对象 3.Math对象 1到100随机数 4.RegExp对象 4.1 正则规则 如: 4.2 两种创建正则对象方式 /正则表达式/ ...
- gohbase使用文档
目录 1. 建立连接 2. 创建表 3. 插入记录 4. 删除记录 5. 查询记录 5.1 根据RowKey查询 5.2 scan范围查询 5.3 复杂查询(过滤器的使用) 5.3.1 比较过滤器 5 ...
- PTA——c++类与对象
对于给定的一个字符串,统计其中数字字符出现的次数. 类和函数接口定义: 设计一个类Solution,其中包含一个成员函数count_digits,其功能是统计传入的string类型参数中数字字符的个数 ...
- 织梦arclist文章标题字数太短
解决dedecms UTF-8首页文章标题显示字数太短的办法原因分析:因为UTF-8编码1个中文汉字占用的是3个字节,GBK占用的是2个字节,所以,原先$titlelen = AttDef($titl ...
- Loj#6247-九个太阳【单位根反演】
正题 题目链接:https://loj.ac/p/6247 题目大意 给出\(n,k\)求 \[\sum_{0\leq i\leq n,i|k}\binom{n}{i} \] 对\(998244353 ...
- P4338-[ZJOI2018]历史【LCT】
正题 题目链接:https://www.luogu.com.cn/problem/P4338 题目大意 给出\(n\)个点的一棵树,和每个点进行\(access\)的次数\(a_i\),要求安排一个顺 ...