当前位置: 首页 > news >正文

使用Vue实现CSS过渡和动画

01-初识动画和过渡

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>使用vue实现css过渡和动画</title></head><body><script src="https://unpkg.com/vue"></script><div id="root"></div><script>const app = Vue.createApp({template: `<div>hello world</div>`,});const vm = app.mount('#root')</script></body>
</html>

过渡和动画的区别:
在这里插入图片描述
在这里插入图片描述

动画:

<style>@keyframes move {0% {transform: translateX(100px);}50% {transform: translateX(50px);}100% {transform: translateX(0);}}.animation {animation: move 2s;}
</style>const app = Vue.createApp({template: `<div class="animation">hello world</div>`,
});
const vm = app.mount('#root')

效果:在这里插入图片描述
通过点击触发:

<script>const app = Vue.createApp({data() {return {animate: {animation: false}}},methods: {handleClick() {this.animate.animation = !this.animate.animation}},template: `<div :class="animate">hello world</div><button @click="handleClick">按钮</button>`,});const vm = app.mount('#root')
</script>

在这里插入图片描述

过渡

<style>
.pink {background-color: pink;
}
.green {background-color: green;
}
.transition {transition: 2s ease;
}
</style>
const app = Vue.createApp({data() {return {animate: {transition: true,green: true,pink: false}}},methods: {handleClick() {this.animate.green = !this.animate.greenthis.animate.pink = !this.animate.pink}},template: `<div :class="animate">hello world</div><button @click="handleClick">按钮</button>`,
});
const vm = app.mount('#root')

效果:
在这里插入图片描述

02-单元素/组件的入场和出场动画

过渡

基本使用:
入场过渡:

<style>/* 进入,开始 */.v-enter-from {opacity: 0;}/* 开始,结束规定动画的效果,这个必须要有,否则不生效 */.v-enter-active {transition: opacity 2s ease;}/* 出去,结束 */.v-enter-to {opacity: 1;}
</style>
const app = Vue.createApp({data() {return {show: false}},methods: {handleClick() {this.show = !this.show}},template: `<transition><div v-if="show">hello world</div></transition><button @click="handleClick">按钮</button>`,
});
const vm = app.mount('#root')

入场过渡效果:
在这里插入图片描述
出场过渡:

<style>
/* 出场的开始 */
.v-leave-from {opacity: 1;
}
.v-leave-active {transition: opacity 2s ease;
}
/* 出场的结束 */
.v-leave-to {opacity: 0;
}
</style>

出场过渡效果:
在这里插入图片描述

动画

<style>
@keyframes move {0% {transform: translateX(-100px);}50% {transform: translateX(-50px);}75% {transform: translateX(50px);}100% {transform: translateX(0);}
}
// 只需要 v-enter-active v-leave-active 即可
.v-enter-active {animation: move 2s ease-in;
}
.v-leave-active {animation: move 2s ease-in;
}
</style>

在这里插入图片描述
可以使用别名:

<style>
.yunmu-enter-active,
.yunmu-leave-active {animation: move 2s ease-in;
}
</style>
template: `
<transition name="yunmu"><div v-if="show">hello world</div>
</transition>
<button @click="handleClick">按钮</button>
`

也可以使用enter-active-class leave-active-class起别名

<style>
.hello,
.bye {animation: move 2s ease-in;
}
</style>
template: `
<transition name="yunmu"enter-active-class="hello"leave-active-class="bye"
><div v-if="show">hello world</div>
</transition>
<button @click="handleClick">按钮</button>

起别名的好处,引入第三方组件库,直接添加name属性就可以完成对应的动画,比如第三方的animate.css
引入第三方动画库:

<linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>

js中直接使用animate__animated animate__bounce

template: `
<transition name="yunmu"enter-active-class="animate__animated animate__bounce"leave-active-class="animate__animated animate__bounce"
><div v-if="show">hello world</div>
</transition>
<button @click="handleClick">按钮</button>
`

在这里插入图片描述
下面再来看这种场景:

<style>
@keyframes move {0% {transform: translateX(100px);}50% {transform: translateX(50px);}100% {transform: translateX(0);}
}
.v-enter-from {color: red;
}.v-enter-active,
.v-leave-active {animation: move 10s ease-in;transition: all 3s ease;
}
.v-leave-active {color: red;
}
</style>
template: `
<transition><div v-if="show">hello world</div>
</transition>
<button @click="handleClick">按钮</button>
`

动画10s,过渡3s;
在这里插入图片描述
可以看到,由于动画的时间比较长,所以过渡完了之后,动画依旧还在进行,如果我们想要达到两者相同的时间的效果的话,就需要制定一个标准:比如以时间短的为结束。可以使用下面的方法:

template: `
<transition type="transition"><div v-if="show">hello world</div>
</transition>
<button @click="handleClick">按钮</button>
`

transition增加一个type,表示时间以哪个为准。
在这里插入图片描述
也可以使用duration属性,表示过渡和动画的时间

也可以使用js进行控制
// 动画进入之前
handleBeforeEnter(el) {el.style.color = 'red'
},
// 执行过程中
handleEnterActive(el, done) {const timer = setInterval(() => {const color = el.style.color;if(color === 'red') {el.style.color = 'green'} else {el.style.color = 'red'}}, 1000);setTimeout(() => {clearInterval(timer)done()}, 4000);
},
// 动画结束之后的钩子 只有 handleEnterActive 中的done执行完之后,该钩子才会执行
handleAfterEnter() {alert(22222)
}template: `
<transition :css="false"@before-enter="handleBeforeEnter"@enter="handleEnterActive"@after-enter="handleAfterEnter"
><div v-if="show">hello world</div>
</transition>
<button @click="handleClick">按钮</button>
`,

在这里插入图片描述
另外离开的时候,也有对应的钩子before-leave leave after-leave

03-组件和元素切换动画的实现

<style>
.v-enter-from {opacity: 0;
}
.v-enter-active,
.v-leave-active {transition: opacity 1s;
}
.v-enter-to,
.v-leave-from{opacity: 1;
}
.v-leave-to {opacity: 0;
}
</style>
template: `
<transition 
><div v-if="show">hello world</div><div v-else>Bye world</div>
</transition>
<button @click="handleClick">按钮</button>
`,

在这里插入图片描述
有个问题:两个标签都是慢慢消失,慢慢出来,不是我们想要的效果
transition标签上面增加mode="out-in"属性,表示先出场,再进场。
在这里插入图片描述
对比一下in-out
在这里插入图片描述
这样动画就不是一个同步的了。上面的if else标签也可以改成组件。
另外列表组件可以使用transition-group标签进行动画渲染,和transition标签使用差不多。
vue.js的过渡和动画 (更新完成)

http://www.lryc.cn/news/330764.html

相关文章:

  • 一家购物商场的数据运营挑战
  • React Native框架开发APP,安装免费的图标库(react-native-vector-icons)并使用详解
  • idea端口占用
  • MQ消息队列详解以及MQ重复消费问题
  • 系统IO函数接口
  • 06 监听器
  • C语言第三十九弹---预处理(上)
  • 计算机视觉无人驾驶技术:入门指南
  • Golang和Java对比
  • 2024.2.29力扣每日一题——统计可能的树根数目
  • 同一个主机配置多个SSH key
  • SAP系统财务模块简介:实现财务管理的卓越之道
  • 【pytest】功能特性及常用插件
  • 基于SpringBoot和Vue的房产销售系统的设计与实现
  • ROS2从入门到精通1-2:详解ROS2服务通信机制与自定义服务
  • vue两个特性和什么是MVVM
  • CAD Plant3D 2023 下载地址及安装教程
  • 集成电路企业tapeout,如何保证机台数据准确、完整、高效地采集?
  • Nginx三大常用功能“反向代理,负载均衡,动静分离”
  • 类方法介绍、使用细节
  • Java SpringBoot中优雅地判断一个对象是否为空
  • 算法——矩阵:对于边界元素的处理
  • Git分支提交时自动大写 fatal: the remote end hung up unexpectedly
  • 隐私计算实训营第七讲-隐语SCQL的架构详细拆解
  • Android JNI开发定义全局变量
  • docker容器部署gitlab的runner的shell模式注册下job中无法使用docker指令
  • 【SpringCloud】Zuul网关中心 代码详细介绍
  • Delphi D12中实现安卓中文语音合成(中文朗读)不用第三方控件
  • 设计模式 - Provider 模式
  • R语言颜色细分