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>```

总结

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

相关文章

  • 通过vue提供的keep-alive减少对服务器的请求次数

    通过vue提供的keep-alive减少对服务器的请求次数

    这篇文章主要介绍了通过vue提供的keep-alive减少对服务器的请求次数,文中给大家补充介绍了vue路由开启keep-alive时的注意点,需要的朋友可以参考下
    2018-04-04
  • vue-cli3 配置开发与测试环境详解

    vue-cli3 配置开发与测试环境详解

    这篇文章主要介绍了vue-cli3 配置开发与测试环境详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • vue开发公共组件之返回顶部

    vue开发公共组件之返回顶部

    这篇文章主要为大家详细介绍了vue开发公共组件之返回顶部,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 解决ElementUI中tooltip出现无法显示的问题

    解决ElementUI中tooltip出现无法显示的问题

    这篇文章主要介绍了解决ElementUI中tooltip出现无法显示的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • 解决vue.js 数据渲染成功仍报错的问题

    解决vue.js 数据渲染成功仍报错的问题

    今天小编就为大家分享一篇解决vue.js 数据渲染成功仍报错的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Vue vm.$attrs使用场景详解

    Vue vm.$attrs使用场景详解

    这篇文章主要介绍了vm.$attrs使用场景详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • 前端vue a链接下载文件失败的问题(未发现文件)

    前端vue a链接下载文件失败的问题(未发现文件)

    这篇文章主要介绍了前端vue a链接下载文件失败的问题(未发现文件),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Vue中引入第三方JS库的四种方式

    Vue中引入第三方JS库的四种方式

    在开发Vue项目的时候,有时需要使用一些非ES6格式的没有export的js库,下面这篇文章主要给大家介绍了关于Vue中引入第三方JS库的详细步骤,需要的朋友可以参考下
    2022-04-04
  • vue实现在线翻译功能

    vue实现在线翻译功能

    这篇文章主要为大家详细介绍了vue实现在线翻译功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • vuejs中监听窗口关闭和窗口刷新事件的方法

    vuejs中监听窗口关闭和窗口刷新事件的方法

    今天小编就为大家分享一篇vuejs中监听窗口关闭和窗口刷新事件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09

最新评论