基于Vue实现页面切换左右滑动效果
作者:嘉爷 发布时间:2023-07-02 16:55:10
标签:Vue,页面切换,滑动
基于Vue的页面切换左右滑动效果,具体内容如下
HTML文本页面:
<template>
<div id="app>
<transition :name="direction" mode="out-in"> <!--动态获得transition 的name值-->
<router-view class="app-view" wechat-title></router-view>
</transition>
</div>
</template>
JS定义代码:定义在全局js文件里面
router.beforeEach((to, from, next) => {
const list = ['home', 'group', 'user'] // 将需要切换效果的路由名称组成一个数组
const toName = to.name // 即将进入的路由名字
const fromName = from.name // 即将离开的路由名字
const toIndex = list.indexOf(toName) // 进入下标
const fromIndex = list.indexOf(fromName) // 离开下标
let direction = ''
if (toIndex > -1 && fromIndex > -1) { // 如果下标都存在
if (toIndex < fromIndex) { // 如果进入的下标小于离开的下标,那么是左滑
direction = 'left'
} else {
direction = 'right' // 如果进入的下标大于离开的下标,那么是右滑
}
}
store.state.viewDirection = direction //这里使用vuex进行赋值
return next()
})
在App.vue文件中,进行计算属性:
computed: {
direction () {
const viewDir = this.$store.state.viewDirection
let tranName = ''
if (viewDir === 'left') {
tranName = 'view-out'
} else if (viewDir === 'right') {
tranName = 'view-in'
} else {
tranName = 'fade'
}
return tranName
},
},
css3进行动画效果定义:使用sass
待定义引入样式文件:
// Variables
$AnimateHook: "animated";
$AnimateDuration: 0.8s;
// Mixins
// Base Style
.#{$AnimateHook} {
-webkit-animation-duration: $AnimateDuration;
animation-duration: $AnimateDuration;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
// Modifier for infinite repetition
&.infinite {
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
}
}
// Slide
@-webkit-keyframes slideInLeft {
from {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
visibility: visible;
}
to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
}
@keyframes slideInLeft {
from {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
visibility: visible;
}
to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
}
.slideInLeft {
-webkit-animation-name: slideInLeft;
animation-name: slideInLeft;
}
@-webkit-keyframes slideInRight {
from {
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
visibility: visible;
}
to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
}
@keyframes slideInRight {
from {
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
visibility: visible;
}
to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
}
.slideInRight {
-webkit-animation-name: slideInRight;
animation-name: slideInRight;
}
@-webkit-keyframes slideOutLeft {
from {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
to {
visibility: hidden;
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
}
}
@keyframes slideOutLeft {
from {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
to {
visibility: hidden;
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
}
}
.slideOutLeft {
-webkit-animation-name: slideOutLeft;
animation-name: slideOutLeft;
}
@-webkit-keyframes slideOutRight {
from {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
to {
visibility: hidden;
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
}
}
@keyframes slideOutRight {
from {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
to {
visibility: hidden;
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
}
}
.slideOutRight {
-webkit-animation-name: slideOutRight;
animation-name: slideOutRight;
}
@-webkit-keyframes inRight {
0% {
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0)
}
to {
-webkit-transform: translateZ(0);
transform: translateZ(0)
}
}
@keyframes inRight {
0% {
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0)
}
to {
-webkit-transform: translateZ(0);
transform: translateZ(0)
}
}
@-webkit-keyframes outLeft {
0% {
-webkit-transform: translateZ(0);
transform: translateZ(0)
}
to {
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0)
}
}
@keyframes outLeft {
0% {
-webkit-transform: translateZ(0);
transform: translateZ(0)
}
to {
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0)
}
}
定义进入与离开的动画过渡:
.fade-enter-active,
.fade-leave-active {
transition: all .2s ease;
}
.fade-enter,
.fade-leave-active {
opacity: 0;
}
.view-in-enter-active,
.view-out-leave-active {
position: absolute;
top: 0;
width: 100%;
padding-top: px2rem($titbarHeight);
-webkit-animation-duration: .3s;
animation-duration: .3s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.view-in-enter-active {
-webkit-animation-name: inRight;
animation-name: inRight;
}
.view-out-leave-active {
-webkit-animation-name: outLeft;
animation-name: outLeft;
}


猜你喜欢
- 一、安装 wordcloudpip install wordcloud二、加载包、设置路径import osfrom wordcloud i
- 生命游戏的算法就不多解释了,百度一下介绍随处可见。因为网上大多数版本都是基于pygame,matlab等外部库实现的,二维数组大多是用num
- python修改大数据文件时,如果全加载到内存中,可能会导致内存溢出。因此可借用如下方法,将分件分段读取修改。with open('
- 相信大家对于常见 CSS BUG 的处理已经相对比较熟悉,例如:IE6 Three Pixel Gap、IE5/6 Doubled Floa
- 初学框架vue搭配vux使用发现这个UI库使用有些力不从心。下面说说自己在表单验证过程遇到的两个需求问题及解决的方法。1.使用x-input
- 制作爬虫的步骤制作一个爬虫一般分以下几个步骤:分析需求分析网页源代码,配合开发者工具编写正则表达式或者XPath表达式正式编写 python
- 两列布局的定宽自适应已经详解了,三列浮动中有两列定宽一列自适应的也详解了,那么该说说三列浮动中两列自适应一列定宽的布局了。中间定宽,左右两侧
- Mac本地环境搭建在Mac系统,我们可以使用MAMP Pro (官方网站:https://www.mamp.info/en/)软件来搭建本地
- 1查看Linux发行版本[root@typecodes ~]# cat /etc/redhat-releaseCentOS Linux re
- 零、前言python代码中配置文件是必不可少的内容。常见的配置文件格式有很多中:ini、yaml、xml、properties、txt、py
- pycharm自带对两个文件比对更新模块,方便查找不同,进行修改替换。方法如下:1.选择目标文件,右键选择compare with2.选择对
- CNN中最重要的就是参数了,包括W,b。 我们训练CNN的最终目的就是得到最好的参数,使得目标函数取得最小值。参数的初始化也同样重要,因此微
- 在数据库中执行建表语句CREATE TABLE `sys_acl` ( `id` int(11) NOT NULL AUTO_INCREME
- if xxx 和if xxx is None的区别一、 if xxxNone,’’,0,[],{},
- 一、平稳序列建模步骤假如某个观察值序列通过序列预处理可以判定为平稳非白噪声序列,就可以利用ARMA模型对该序列进行建模。建模的基本步骤如下:
- 数据库开发数据库应用,选择一个好的数据库是非常重要的。下面从一些方面比较了SQL Server与Oracle、DB2三种数据库,为你选择数据
- Mysql的utf8编码最多3个字节,而Emoji表情或者某些特殊字符是4个字节。因此会导致带有表情的昵称插入数据库时出错。只要修改MySQ
- 简介zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。安装pip instal
- Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '+'.
- 众所周知道,IE向来是我们在制作网页时最难搞定的对手。但又迫于其用户群数量之多,我们不得不想法设法搞定它。下面,将介绍的将是利用其特点而被发