@[toc]
3.25Vue封装的过度与动画
3.25.1知识点总结
3.25.2案例
注意点1:最好有css动画基础,再练习这块,但我只是了解所以原封不动拷贝看效果就是,当了解即可。
【动画/过度】使用方式:
1)定义【动画/过度】样式名称
2)用标签包裹起来要实现动画的元素
> 使用动画方式:
java <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>
> 使用过度方式:
java <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要求你想让谁实现动画效果,你就用标签把它包裹起来
java <transition name="hello" appear> <h1 v-show="isShow">你好啊!</h1> </transition>
注意点4:这个和标签效果一样,最终页面都不会显示这个标签,它只作为包裹作用使用

注意点5:每个过度可以取名字,如果\定义了name属性值,那么class的样式名称前缀也得改名,不然无法自动识别,比如未定义name属性,那么类名叫.v-enter-active和.v-leave-active,如果定义了name属性name="hello",那么类名叫.hello-enter-active和.hello-leave-active,(即vue不跟动画进行对话,而是跟样式的名称进行对话。)
java <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>
答案:报错说明\标签只能用于一个元素,如果想实现多个元素相同效果,请使用\标签。
问题:如果改成使用\标签后,运行还是报错了,感觉更严重了,下面两个过度一个都没显示,且还报错了。
答案:正确写法就是必须指定key值,这块在讲解v-for的时候着重强调要定义key的属性。
注意点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中定义一堆动画或过度\