vue中transition动画使用(移动端页面切换左右滑动效果)

 更新时间:2024年07月27日 10:07:25   作者:冷太阳a  
这篇文章主要介绍了vue中transition动画使用(移动端页面切换左右滑动效果),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

例子一:(简单进入和离开动画)

  • demo1
<template>
  <div>
    <button @click="isShow = !isShow">显示/隐藏</button>
    <transition-group name="hello" appear>
      <h1 v-show="!isShow" key="1">vue2!</h1>
      <h1 v-show="isShow" key="2">VUE3!</h1>
    </transition-group>
  </div>
</template>
<script>
export default {
  name: 'Test',
  data () {
    return {
      isShow: true
    }
  },
}
</script>
<style scoped>
h1 {
  color: #fff;
  background-color: rgb(27, 247, 255);
}
/* 进入的起点、离开的终点 */
.hello-enter,
.hello-leave-to {
  transform: translateX(-100%);
}
.hello-enter-active,
.hello-leave-active {
  transition: 0.5s linear;
}
/* 进入的终点、离开的起点 */
.hello-enter-to,
.hello-leave {
  transform: translateX(0);
}
</style>
  • demo1
<!--
 * @Description: 
 * @Author: Ran junlin
 * @Date: 2021-09-24 14:07:16
 * @LastEditTime: 2022-02-10 17:29:28
 * @LastEditors: Ran junlin
-->
<template>
  <div id="app">
    <h2>pubsub中间件消息订阅:</h2>
    <hr />
    <v-hello />
    <v-add />
    <hr />
    <h1>动画</h1>
    <button @click="isShow = !isShow">点击显示/隐藏</button>
    <transition name="myHello" appear mode="in-out">
      <div v-show="isShow" class="demio">hello vue !</div>
    </transition>
  </div>
</template>
<script>
import pubsub from 'pubsub-js'
import vAdd from '@/components/v-add'
import vHello from '@/components/v-hello'
export default {
  name: 'App',
  components: {
    vHello,
    vAdd
  },
  data () {
    return {
      pubId: '',
      isShow: true
    }
  },
  created () {
  },
  mounted () {
    // 消息订阅
    this.pubId = pubsub.subscribe('hello', (name, data) => {
      console.log('有人发布了消息,消息发布回调执行', name, data);
    })
  },
  beforeDestroy () {
    pubsub.unsubscribe(this.pubId)
  },
  methods: {
  },
}
</script>
<style>
.bm-view {
  width: 100%;
  height: 600px;
}
* {
  touch-action: pan-y;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
#container {
  margin: 0 auto;
  width: 1500px;
  height: 1000px;
}
.demio {
  margin: 20px auto;
  width: 80%;
  height: 300px;
  line-height: 300px;
  text-align: center;
  font-size: 50px;
  background-color: rgb(94, 245, 182);
}

.myHello-enter-active {
  animation: showHello 0.5s linear;
}
.myHello-leave-active {
  animation: showHello 0.5s linear reverse;
}
@keyframes showHello {
  from {
    transform: translateX(-100%);
    transform: scaleX(0.2);
  }
  to {
    transform: translateX(-100%);
    transform: scaleX(1.1);
  }
}
</style>

例子二:(移动端上页面进入和离去动画)

<template>
<div id="app">
  <transition :name="animation" mode="in-out" appear>
      <router-view class="global-router" />
  </transition>
</div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      animation: ''
    };
  },
  watch: {
    $route(to, from) {
      wexinShare()
      if (!from.meta.pageNum=== undefined) {
        this.animation = 'none';
      } else {
        this.animation = to.meta.pageNum> from.meta.pageNum? 'left' : 'right';
      }
    }
  },
};
</script>
<style lang="less">
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  font-size: 15px;
}
</style><style lang="less" scoped>
.global-router {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.left-enter {
  transform: translateX(100%);
}

.right-enter {
  transform: translateX(-100%);
}

.left-enter-active,
.right-enter-active {
  position: relative;
  z-index: 999;
  transition: all 0.4s;
}

.left-leave-active,
.right-leave-active {
  position: relative;
  z-index: -1;
}
</style>

例子三:(利用第三方css库 Animate

<template>
	<div>
		<button @click="isShow = !isShow">显示/隐藏</button>
		<transition-group 
			appear
			name="animate__animated animate__bounce" 
			enter-active-class="animate__swing"
			leave-active-class="animate__backOutUp"
		>
			<h1 v-show="!isShow" key="1">你好啊!</h1>
			<h1 v-show="isShow" key="2">超级管理员!</h1>
		</transition-group>
	</div>
</template>
<script>
	import 'animate.css'
	export default {
		name:'Test',
		data() {
			return {
				isShow:true
			}
		},
	}
</script>
<style scoped>
	h1{
		background-color: orange;
	}
</style>```

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 一篇文章让你看懂封装Axios

    一篇文章让你看懂封装Axios

    axios的封装和api接口的统一管理,其实主要目的就是在帮助我们简化代码和利于后期的更新维护,这篇文章主要给大家介绍了关于封装Axios的相关资料,需要的朋友可以参考下
    2022-01-01
  • 在vue项目中利用popstate处理页面返回的操作介绍

    在vue项目中利用popstate处理页面返回的操作介绍

    这篇文章主要介绍了在vue项目中利用popstate处理页面返回的操作介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • FastApi+Vue+LayUI实现前后端分离的示例代码

    FastApi+Vue+LayUI实现前后端分离的示例代码

    本文主要介绍了FastApi+Vue+LayUI实现前后端分离的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Vue事件处理原理及过程详解

    Vue事件处理原理及过程详解

    这篇文章主要介绍了vue事件处理原理及过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • vue使用@include或@mixin报错的问题及解决

    vue使用@include或@mixin报错的问题及解决

    这篇文章主要介绍了vue使用@include或@mixin报错的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Vue3中无法为el-tree-select设置反选问题解析

    Vue3中无法为el-tree-select设置反选问题解析

    这篇文章主要介绍了Vue3中无法为el-tree-select设置反选问题分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • vue运行报错cache-loader的解决步骤

    vue运行报错cache-loader的解决步骤

    最近运行vue项目的时候报错了,通过查找相关资料最终解决,下面这篇文章主要给大家介绍了关于vue运行报错cache-loader的解决步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • Vue如何调用接口请求头增加参数

    Vue如何调用接口请求头增加参数

    这篇文章主要介绍了Vue如何调用接口请求头增加参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • vuex项目中登录状态管理的实践过程

    vuex项目中登录状态管理的实践过程

    由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长,为了解决这个问题,Vue 提供 vuex,这篇文章主要给大家介绍了关于vuex项目中登录状态管理的相关资料,需要的朋友可以参考下
    2021-09-09
  • 详解Vue Elememt-UI构建管理后台

    详解Vue Elememt-UI构建管理后台

    本篇文章给大家详细分享了Vue Elememt-UI构建管理后台的过程以及相关代码实例,一起参考学习下。
    2018-02-02

最新评论