vue2知识点:Vue封装的过度与动画_动画

文章目录

  • ​​3.25Vue封装的过度与动画​​
  • ​​3.25.1知识点总结​​
  • ​​3.25.2案例​​
  • ​​本人其他相关文章链接​​

3.25Vue封装的过度与动画

3.25.1知识点总结

vue2知识点:Vue封装的过度与动画_动画_02


vue2知识点:Vue封装的过度与动画_vue2_03

3.25.2案例

vue2知识点:Vue封装的过度与动画_Vue封装的过度与动画_04



注意点1:最好有css动画基础,再练习这块,但我只是了解所以原封不动拷贝看效果就是,当了解即可。


【动画/过度】使用方式:


1)定义【动画/过度】样式名称


2)用标签包裹起来要实现动画的元素

使用动画方式:

<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>

<style scoped>
h1{
background-color: orange;
}

.hello-enter-active{
animation: atguigu 0.5s linear;
}

.hello-leave-active{
animation: atguigu 0.5s linear reverse;
}

@keyframes atguigu {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>

使用过度方式:

<style scoped>
h1{
background-color: orange;
}
/* 进入的起点、离开的终点 */
.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>

注意点2:动画效果来和去只写一个就行,另一个效果直接反转动画就是

注意点3:vue要求你想让谁实现动画效果,你就用标签把它包裹起来

<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>

注意点4:这个和标签效果一样,最终页面都不会显示这个标签,它只作为包裹作用使用

vue2知识点:Vue封装的过度与动画_vue.js_05


注意点5:每个过度可以取名字,如果<transition>定义了name属性值,那么class的样式名称前缀也得改名,不然无法自动识别,比如未定义name属性,那么类名叫.v-enter-active和.v-leave-active,如果定义了name属性name=“hello”,那么类名叫.hello-enter-active和.hello-leave-active,(即vue不跟动画进行对话,而是跟样式的名称进行对话。)

<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>

<style scoped>
h1{
background-color: orange;
}

.hello-enter-active{
animation: atguigu 0.5s linear;
}

.hello-leave-active{
animation: atguigu 0.5s linear reverse;
}

@keyframes atguigu {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>

注意点6:

问题:想实现多个元素产生相同过度效果时,错误代码如下,运行发生了报错如图

<transition>
<h1 v-show="!isShow" key="1">你好啊!</h1>
<h1 v-show="isShow" key="2">尚硅谷!</h1>
</transition-group>

vue2知识点:Vue封装的过度与动画_vue.js_06


答案:报错说明<transition>标签只能用于一个元素,如果想实现多个元素相同效果,请使用<transition-group>标签。

问题:如果改成使用<transition-group>标签后,运行还是报错了,感觉更严重了,下面两个过度一个都没显示,且还报错了。

答案:正确写法就是必须指定key值,这块在讲解v-for的时候着重强调要定义key的属性。

vue2知识点:Vue封装的过度与动画_css3_07


注意点7:Test3.vue使用第三方库animate.css,所以需要额外安装animate.css

使用步骤:

1)安装 npm install --save animate.css

2)引入 import ‘animate.css’

3)使用3个标签即可实现【动画/过度】效果,发别是name、enter-active-class、leave-active-class,使用第三方库比较方便,就不用像Test1.vue和Test2.vue中定义一堆动画或过度<style>标签内容

项目结构

vue2知识点:Vue封装的过度与动画_css3_08

main.js

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue的生产提示
Vue.config.productionTip = false

//创建vm
new Vue({
el:'#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this
},
})

App.vue

<template>
<div>
<Test/>
<Test2/>
<Test3/>
</div>
</template>

<script>
import Test from './components/Test'
import Test2 from './components/Test2'
import Test3 from './components/Test3'

export default {
name:'App',
components:{Test,Test2,Test3},
}
</script>

Test.vue

<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>
</div>
</template>

<script>
export default {
name:'Test',
data() {
return {
isShow:true
}
},
}
</script>

<style scoped>
h1{
background-color: orange;
}

.hello-enter-active{
animation: atguigu 0.5s linear;
}

.hello-leave-active{
animation: atguigu 0.5s linear reverse;
}

@keyframes atguigu {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>

Test2.vue

<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition-group name="hello" appear>
<h1 v-show="!isShow" key="1">你好啊!</h1>
<h1 v-show="isShow" key="2">尚硅谷!</h1>
</transition-group>
</div>
</template>

<script>
export default {
name:'Test',
data() {
return {
isShow:true
}
},
}
</script>

<style scoped>
h1{
background-color: orange;
}
/* 进入的起点、离开的终点 */
.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>

Test3.vue

<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>
</div>
</template>

<script>
export default {
name:'Test',
data() {
return {
isShow:true
}
},
}
</script>

<style scoped>
h1{
background-color: orange;
}

.hello-enter-active{
animation: atguigu 0.5s linear;
}

.hello-leave-active{
animation: atguigu 0.5s linear reverse;
}

@keyframes atguigu {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>