10-vue3动画
文章目录
-
- 1.vue的transition动画
-
- 1.1transition的基本使用
- 1.2transition组件的原理
- 1.3过渡动画的class
- 1.4class的命名规则和添加时机
- 1.5显示的指定过渡时间
- 1.6过渡的模式mode
- 1.7动态组件的切换
- 1.8.appear初次渲染
- 2、animation动画
-
- 2.1同时设置animation和transition
- 3.结合第三方库
-
- 3.1. 结合animate库
-
- 3.1.1. 认识animate.css
- 3.1.2. 自定义过渡类名
- 3.1.3. animate.css使用
- 3.2. 结合gsap库
-
- 3.2.1. 认识gsap库
- 3.2.2. JavaScript钩子
- 3.2.3. gsap库使用
- 3.2.4. gsap数字变化
- 4. 列表的过渡
-
- 4.1. 列表过渡的基本使用
- 4.2. 列表过渡的移动动画
- 4.3. 列表的交错过渡案例
1.vue的transition动画
1.1transition的基本使用
Vue 提供了 transition
的封装组件,在下列情形中,可以给任何元素和组件添加进入/离开过渡
- 条件渲染 (使用
v-if
) - 条件展示 (使用
v-show
) - 动态组件
- 组件根节点
<template><div><p><button @click="isshow=!isshow">点击显示和隐藏</button> </p><transition name="fade"><div class="box" v-if="isshow"><h3>hello world!!</h3></div></transition></div>
</template>
<script setup>import {ref} from "vue"const isshow=ref(false);
</script>
<style lang="scss" scoped>
.box{width: 200px;height: 200px;background: pink;
}
.fade-enter-from,.fade-leave-to{opacity: 0;
}
.fade-enter-active,.fade-leave-active{transition:opacity 3s ease;
}
.fade-enter-to,.fade-leave-from{opacity:1;
}</style>
1.2transition组件的原理
我们发现,Vue自动给div元素添加了动画,这是什么原因呢?
当插入或删除包含在 transition
组件中的元素时,Vue 将会做以下处理:
- 自动嗅探目标元素是否应用了 CSS 过渡或动画,如果是,在恰当的时机添加/删除 CSS 类名。
- 如果过渡组件提供了 JavaScript 钩子函数,这些钩子函数将在恰当的时机被调用。
- 如果没有找到 JavaScript 钩子并且也没有检测到 CSS 过渡/动画,DOM 操作 (插入/删除) 在下一帧中立即执行。
1.3过渡动画的class
我们发现上面提到了很多个class,事实上vue就是帮助我们在这些class之间来回切换完成的动画.
在进入/离开的过渡中,会有 6 个 class 切换。
v-enter-from
:定义进入过渡的开始状态。在元素被插入之前生效,在元素被插入之后的下一帧移除。v-enter-active
:定义进入过渡生效时的状态。在整个进入过渡的阶段中应用,在元素被插入之前生效,在过渡/动画完成之后移除。这个类可以被用来定义进入过渡的过程时间,延迟和曲线函数。v-enter-to
:定义进入过渡的结束状态。在元素被插入之后下一帧生效 (与此同时v-enter-from
被移除),在过渡/动画完成之后移除。v-leave-from
:定义离开过渡的开始状态。在离开过渡被触发时立刻生效,下一帧被移除。v-leave-active
:定义离开过渡生效时的状态。在整个离开过渡的阶段中应用,在离开过渡被触发时立刻生效,在过渡/动画完成之后移除。这个类可以被用来定义离开过渡的过程时间,延迟和曲线函数。v-leave-to
:离开过渡的结束状态。在离开过渡被触发之后下一帧生效 (与此同时v-leave-from
被移除),在过渡/动画完成之后移除。
1.4class的命名规则和添加时机
这里的每个 class 都将以过渡的名字添加前缀。如果你使用了一个没有名字的 <transition>
,则 v-
是这些 class 名的默认前缀。举例来说,如果你使用了 <transition name="my-transition">
,那么 v-enter-from
会替换为 my-transition-enter-from
1.5显示的指定过渡时间
我们也可以显示的来指定过渡的时间,通过 duration
属性。
duration可以设置两种类型的值:
- number类型:同时设置进入和离开的过渡时间;
- object类型:分别设置进入和离开的过渡时间;
number类型的值:
<transition name="fade" type="transition" :duration="1000"><div class="box" v-if="isshow"><h3>hello world!!</h3> </div>
</transition>
object类型的值:
<transition name="fade" type="transition" :duration="{enter: 800, leave: 1000}"><div class="box" v-if="isshow"><h3>hello world!!</h3> </div></transition>
1.6过渡的模式mode
<transition name="fade"><h3 v-if="isshow">hello world!!</h3> <h3 v-else>嗨,世界!!</h3></transition>
我们会发现 Hello World
和 嗨,世界!!
是同时存在的:
- 这是因为默认情况下进入和离开动画是同时发生的;
- 如果确实我们希望达到这个的效果,那么是没有问题;
但是如果我们不希望同时执行进入和离开动画,那么我们需要设置transition的过渡模式:
in-out
: 新元素先进行过渡,完成之后当前元素过渡离开;out-in
: 当前元素先进行过渡,完成之后新元素过渡进入;
<transition name="fade" mode="out-in"><h3 v-if="isshow">hello world!!</h3> <h3 v-else>嗨,世界!!</h3>
</transition>
1.7动态组件的切换
<!-- vue3在<script setup>中如何使用动态组件
针对这个问题,其实 vue官方已经给出了说明。
由于组件被引用为变量而不是作为字符串键来注册的 -->
<transition name="fade" mode="out-in"><component :is="isshow?userList:userDetail"></component>
</transition>
<script setup>import {ref} from "vue"import userDetail from "./userDetail.vue"import userList from "./userList.vue"const isshow=ref(false);
</script>
1.8.appear初次渲染
默认情况下,首次渲染的时候是没有动画的,如果我们希望给他添加上去动画,那么就可以增加另外一个属性 appear。默认情况下为 :appear=“false”, :appear="true"简写为appear
<transition name="fade" appear ><h3 v-if="isshow">hello world!!</h3>
</transition>
2、animation动画
<template><div><p><button @click="isshow=!isshow">点击显示和隐藏</button> </p><transition name="fade"><div class="box" v-if="isshow"><h3>hello world!!</h3></div></transition></div>
</template>
<script setup>import {ref} from "vue"const isshow=ref(false);
</script>
<style lang="scss" scoped>
.box{width: 200px;height: 200px;background: pink;
}
@keyframes bounceIn{0%{transform: scale(0);background: aqua; }50%{transform: scale(1.5); background: rgb(213, 226, 33); }100%{transform: scale(1);}
}@keyframes bounceOut{0%{transform: scale(1);background: aqua; }50%{transform: scale(1.5); background: rgb(213, 226, 33); }100%{transform: scale(0);}
}
.fade-enter-active{animation: bounceIn 3s; }
.fade-leave-active{animation: bounceOut 3s;}
</style>
2.1同时设置animation和transition
Vue 为了知道过渡何时完成,必须设置相应的事件监听器。它可以是 transitionend
或 animationend
,这取决于给元素应用的 CSS 规则。如果你只使用了其中一种,Vue 能自动识别其正确类型。
但是,在一些场景中,你需要给同一个元素同时设置两种过渡动效,比如有一个通过 Vue 触发的 CSS 动画,并且在悬停时结合一个 CSS 过渡。在这种情况中,你就需要使用 type
attribute 并设置 animation
或 transition
来显式声明你需要 Vue 监听的类型。
<transition name="fade" type="transition" ><div class="box" v-if="isshow"><h3>hello world!!</h3> </div></transition>
3.结合第三方库
3.1. 结合animate库
3.1.1. 认识animate.css
如果我们手动一个个来编写这些动画,那么效率是比较低的,所以在开发中我们可能会引用一些第三方库的动画库,比如animate.css。
什么是animate.css呢?
- Animate.css is a library of ready-to-use, cross-browser animations for use in your web projects. Great for emphasis, home pages, sliders, and attention-guiding hints.
- Anmate.css是一个已经准备好的、跨平台的动画库为我们的web项目,对于强调、主页、滑动、注意力引导非常有用;
如何使用Animate库呢?
- 第一步:需要安装animate.css库;
- 第二步:导入animate.css库的样式;
- 第三步:使用animation动画或者animate提供的类;
安装animate.css:
npm install animate.css
在main.js中导入animate.css:
import "animate.css";
3.1.2. 自定义过渡类名
我们可以通过以下 attribute 来自定义过渡类名:
enter-from-class
enter-active-class
enter-to-class
leave-from-class
leave-active-class
leave-to-class
他们的优先级高于普通的类名,这对于 Vue 的过渡系统和其他第三方 CSS 动画库,如 Animate.css. 结合使用十分有用。
3.1.3. animate.css使用
animate.css的原理非常简单,就是帮我们提前写好了很多的动画而已
方式一:直接使用动画名称
<transition name="fade" ><h3 v-if="isshow">hello world!!</h3>
</transition> //jsimport "animate.css"<style>
.fade-enter-active{animation: rotateIn 2s ;
}
.fade-leave-active{
animation: rotateOut 2s;
}</style>
方式二:使用动画的类名
<transition name="fade" enter-active-class="animate__animated animate__rotateInDownLeft" leave-active-class="animate__animated animate__rotateOutDownLeft" ><h3 v-if="isshow">hello world!!</h3>
</transition>
3.2. 结合gsap库
3.2.1. 认识gsap库
某些情况下我们希望通过JavaScript来实现一些动画的效果,这个时候我们可以选择使用gsap库来完成。
什么是gsap呢?
- GSAP是The GreenSock Animation Platform(GreenSock动画平台)的缩写;
- 它可以通过JavaScript为CSS属性、SVG、Canvas等设置动画,并且是浏览器兼容的;
这个库应该如何使用呢?
- 第一步:需要安装gsap库;
- 第二步:导入gsap库;
- 第三步:使用对应的api即可;
我们先对库来进行一个安装:
npm install gsap
3.2.2. JavaScript钩子
在使用动画之前,我们先来看一下transition组件给我们提供的JavaScript钩子,这些钩子可以帮助我们监听动画执行到什么阶段了。
<transition name="fade"@before-enter="beforeEnter"@enter="enter"@after-enter="afterEnter"@enter-cancelled="enterCancelled"@before-leave="beforeLeave"@leave="leave"@after-leave="afterLeave"@leave-cancelled="leaveCancelled" ><h3 v-if="isshow">hello world!!</h3>
</transition> <script setup>
const beforeEnter=()=>{console.log("beforeEnter");
}const enter=()=>{console.log("enter");
}const afterEnter=()=>{console.log("afterEnter");
}
const enterCancelled=()=>{console.log("enterCancelled");
}
const leave=()=>{console.log("beforeLeave");
}
const afterLeave=()=>{console.log("beforeLeave");
}
const leaveCancelled=()=>{console.log("beforeLeave");
}
const beforeLeave=()=>{console.log("beforeLeave");
}</script>
当我们使用JavaScript来执行过渡动画时,需要进行 done
回调,否则它们将会被同步调用,过渡会立即完成。
添加 :css="false"
,也会让 Vue 会跳过 CSS 的检测,除了性能略高之外,这可以避免过渡过程中 CSS 规则的影响。
3.2.3. gsap库使用
那么接下来我们就可以结合gsap库来完成动画效果:
<transitionname="fade"@enter="enter"@leave="leave"><h2 v-if="isshow">Hello World!!</h2></transition><script setup>import gsap from 'gsap';
const enter=(el, done)=> {gsap.from(el, {scale: 0,x: 200,onComplete: done})}const leave=(el, done)=>{gsap.to(el, {scale: 0,x: 200,onComplete: done})}
</script>
3.2.4. gsap数字变化
在一些项目中,我们会见到数字快速变化的动画效果,这个动画可以很容易通过gsap来实现:
<template>
<input type="number" v-model.number="counter" step="100"><h2>{{animatedNumber}}</h2><!-- 下面的写法也是可以的 --><h2>{{showNumber.toFixed(0)}}</h2>
</tempalte><script setup>
import {ref,computed,watch} from "vue"
import gsap from 'gsap';
const counter=ref(0);const showNumber=ref(0);const animatedNumber=computed(()=>{return showNumber.value.toFixed(0);}) watch(counter,(newValue,oldValue)=>{console.log("jiantingdaole")gsap.to(counter.value, {duration: 1, showNumber: newValue})gsap.registerPlugin()}) </script>
4. 列表的过渡
目前为止,过渡动画我们只要是针对单个元素或者组件的:
- 要么是单个节点;
- 要么是统一时间渲染多个节点中的一个;
那么如果希望渲染的是一个列表,并且该列表中添加删除数据也希望有动画执行呢?
- 这个时候我们要使用
<transition-group>
组件来完成;
使用<transition-group>
有如下的特点:
- 默认情况下,它不会渲染一个元素的包裹器,但是你可以指定一个元素并以
tag
attribute 进行渲染; 过渡模式
不可用,因为我们不再相互切换特有的元素;- 内部元素总是需要提供唯一的
key
attribute 值; - CSS 过渡的类将会应用在内部的元素中,而不是这个组/容器本身;
4.1. 列表过渡的基本使用
我们来做一个案例:
- 案例是一列数字,可以继续添加或者删除数字;
- 在添加和删除数字的过程中,对添加的或者移除的数字添加动画;
<template><div><button @click="addNum">添加数字</button><button @click="removeNum">删除数字</button><transition-group name="list" tag="p"><span v-for="item in numbers" :key="item" class="item">{{item}}</span></transition-group></div></template>
<script>export default {data() {return {numbers: [1, 2, 3, 4, 5, 6, 7, 8],nextNum: 10}},methods: {randomIndex() {return Math.floor(Math.random() * this.numbers.length)},addNum() {this.numbers.splice(this.randomIndex(), 0, this.nextNum++)},removeNum() {this.numbers.splice(this.randomIndex(), 1);}}}
</script>
<style scoped>.item {display: inline-block;margin-right: 10px;}.list-enter-active,.list-leave-active {transition: all 1s ease;}.list-enter-from,.list-leave-to {opacity: 0;transform: translateY(30px)}
</style>
4.2. 列表过渡的移动动画
在上面的案例中虽然新增的或者删除的节点是有动画的,但是对于哪些其他需要移动的节点是没有动画的:
- 我们可以通过使用一个新增的
v-move
的class来完成动画; - 它会在元素改变位置的过程中应用;
- 像之前的名字一样,我们可以通过name来自定义前缀;
<template><div><button @click="addNum">添加数字</button><button @click="removeNum">删除数字</button><button @click="shuffleNum">打乱数字</button><transition-group name="list" tag="p"><span v-for="item in numbers" :key="item" class="item">{{item}}</span></transition-group></div>
</template>
<script>import _ from 'lodash';export default {data() {return {numbers: [1, 2, 3, 4, 5, 6, 7, 8],nextNum: 10}},methods: {randomIndex() {return Math.floor(Math.random() * this.numbers.length)},addNum() {this.numbers.splice(this.randomIndex(), 0, this.nextNum++)},removeNum() {this.numbers.splice(this.randomIndex(), 1);},shuffleNum() {this.numbers = _.shuffle(this.numbers)}}}</script>
<style scoped>.item {display: inline-block;margin-right: 10px;}.list-enter-active,.list-leave-active {transition: all 1s ease;}.list-leave-active {position: absolute;}.list-enter-from,.list-leave-to {opacity: 0;transform: translateY(30px)}.list-move {transition: transform 1s ease;}</style>
4.3. 列表的交错过渡案例
我们来通过gsap的延迟delay属性,做一个交替消失的动画:
交替过渡案例
<template><div><input v-model="query"><transition-group name="list" tag="ul"@beforeEnter="beforeEnter"@enter="enter"@leave="leave"><li v-for="(item, index) in showNames" :key="item" :data-index="index">{{item}}</li></transition-group></div>
</template><script>import gsap from 'gsap';export default {data() {return {query: "",names: ["abc", "cba", "nba", "dna", "why", "kobe", "james", "curry"]}},computed: {showNames() {return this.names.filter(item => {return item.toLowerCase().indexOf(this.query.toLowerCase()) !== -1})}},methods: {beforeEnter(el) {el.style.opacity = 0;el.style.height = 0;},enter(el, done) {gsap.to(el, {opacity: 1,height: "1.6em",delay: el.dataset.index * 0.5,onComplete: done})},leave(el, done) {gsap.to(el, {opacity: 0,height: 0,delay: el.dataset.index * 0.5,onComplete: done})}}}</script>