当前位置:网站首页>Animation and transition effects
Animation and transition effects
2022-07-04 13:49:00 【18-year-old hates programming】
List of articles
Animation effect
Vue Provides transition
Package components of , In the following cases , You can add... To any element or component Get into / Leave the transition :
- Conditions apply colours to a drawing ( Use v-if)
- Condition display ( Use v-show)
- Dynamic components
- Component root
That is to say, use this transition label , We don't need to use data binding , Change the class to trigger the animation when entering and leaving .
This transition Label final Vue When parsing templates , Take them away
For example, now let's make a case :
Click to display , Click Hide again . There is animation moving in and out in the middle .
First of all, let's take transition Tags wrap the elements to be animated :
<template>
<div>
<button @click="isShow = !isShow"> Show / hide </button>
<transition>
<h1 v-show="isShow"> How do you do !</h1>
</transition>
</div>
</template>
<script> export default {
name:'AnimationTest', data() {
return {
isShow:true } }, } </script>
So let's use v-enter-active
and v-leave-active
To define the style to be activated when entering and the style to be activated when leaving .
<style scoped> h1{
background-color: orange; } .v-enter-active{
animation: move 0.5s linear; } .v-leave-active{
animation: move 0.5s linear reverse; } @keyframes move {
from{
transform: translateX(-100%); } to{
transform: translateX(0px); } } </style>
Let's focus on v-enter-active
and v-leave-active
Time to act :
v-enter-active
: Define the state when the transition takes effect . Apply... Throughout the transition , Takes effect before the element is inserted , In the transition / Remove... After animation . This class can be used to define the process time to enter the transition , Delay and curve functions .
When we click on the display , Play the animation before displaying
isShow Change immediately
v-leave-active
: Define the state when the exit transition takes effect . Apply... Throughout the transition , Takes effect immediately when the exit transition is triggered , In the transition / Remove... After animation . This class can be used to define the process time of leaving the transition , Delay and curve functions .
When we click Hide , The animation is immediately triggered
isShow Change immediately
There is another point of attention :
For these class names that switch in transition , If you use an unnamed <transition>, be v-
Is the default prefix for these class names . If you use a name , Then use the corresponding name to replace v-
For example, the case above , I give <transition> Naming – hello
So here's the code :
<template>
<div>
<button @click="isShow = !isShow"> Show / hide </button>
<transition name="hello">
<h1 v-show="isShow"> How do you do !</h1>
</transition>
</div>
</template>
<script> export default {
name:'AnimationTest', data() {
return {
isShow:true } }, } </script>
<style scoped> h1{
background-color: orange; } .hello-enter-active{
animation: move 0.5s linear; } .hello-leave-active{
animation: move 0.5s linear reverse; } @keyframes move {
from{
transform: translateX(-100%); } to{
transform: translateX(0px); } } </style>
If we want to open a web page, there will be animation when displaying elements , What to do ?
We can use transition Labeled appear attribute , This is a Boolean property
According to the above case We can write in the following ways :
<transition name="hello" :appear="true">
<h1 v-show="isShow"> How do you do !</h1>
</transition>
<transition name="hello" appear>
<h1 v-show="isShow"> How do you do !</h1>
</transition>
In fact, it can be popularized , You want to give the tag an attribute , Just write the attribute name directly , Don't bother to set its value to true.
If you don't want it to have this property , You need to explicitly declare false
Transition effects
We completed the above case with animation , So how do we complete the transition ?
Here we will introduce two new opportunities :v-enter
、v-enter-to
v-enter
: Define the start state of transition . Takes effect before the element is inserted , Remove the next frame after the element is inserted .
v-enter-to
:2.1.8 Version and above Define the end state of transition . The next frame takes effect after the element is inserted ( meanwhile v-enter Removed ), In the transition / Remove... After animation .
We can simply understand it as :
v-enter
Is the starting point of entryv-enter-to
Is the end of entry
Combined with our above case :
The orange part is the starting point , The green part is the end of entry !
The same goes for leaving , Finally, our code is as follows :
<template>
<div>
<button @click="isShow = !isShow"> Show / hide </button>
<transition name="hello" appear>
<h1 v-show="isShow"> How do you do !</h1>
</transition>
</div>
</template>
<script> export default {
name:'AnimationTest', data() {
return {
isShow:true } }, } </script>
<style scoped> h1{
background-color: orange; transition: 2s linear; } .hello-enter {
transform: translateX(-100%); } .hello-enter-to {
transform: translateX(0); } .hello-leave {
transform: translateX(0); } .hello-leave-to {
transform: translateX(-100%); } </style>
Because the starting point of entering is the same as the ending point of leaving , The end of entry is the same as the starting point of departure , So we can merge . At the same time, in order not to destroy the original style , We can put the transition time and curve on v-enter-active
and v-leave-active
in
So we can optimize the code :
<style scoped> h1{
background-color: orange; } /* The starting point of entry 、 The end of leaving */ .hello-enter,.hello-leave-to{
transform: translateX(-100%); } .hello-enter-active,.hello-leave-active{
transition: 0.5s linear; } /* Enter the end of 、 The starting point of departure */ .hello-enter-to,.hello-leave{
transform: translateX(0); } </style>
Multiple element transitions
If you want to add the same style to multiple elements , You can't use it transiton The label , It needs to be used transition-group label .
When elements with the same tag name switch , Need to pass through key attribute Set a unique value to mark so that Vue Distinguish them , otherwise Vue For efficiency, only the contents inside the same label will be replaced .
for example :
<template>
<div>
<button @click="isShow = !isShow"> Show / hide </button>
<transition-group name="hello" appear>
<h1 v-show="isShow" key="1"> How do you do !</h1>
<h1 v-show="isShow" key="2">CSDN</h1>
</transition-group>
</div>
</template>
Integrate third-party animation
stay Vue It can easily integrate third-party style libraries or animation libraries , To help us quickly achieve some particularly cool effects .
We are here to npm Upper animate.css For example
https://www.npmjs.com/package/animate.css
This library provides a lot of entry and exit effects .
Use steps :
First install this library in the terminal :
npm install animate.css --save
Introduce this library :
import 'animate.css'
Because we introduced css The library of , No js modular , So let's just follow the path
then animate__animated
,animate__bounce
Put it into what you want to use transition Labeled name Attribute to .
stay transition Add two new configuration items to the tag enter-active-class
,leave-active-class
. Put the selected name into the animation , And leave the name of the animation inside .
This name can be copied directly by clicking the icon behind
<template>
<div>
<button @click="isShow = !isShow"> Show / hide </button>
<transition-group name="animate__animated animate__bounce" appear enter-active-class="animate__swing" leave-active-class="animate__backOutUp" >
<h1 v-show="isShow" key="1"> How do you do !</h1>
<h1 v-show="isShow" key="2">CSDN</h1>
</transition-group>
</div>
</template>
And then ok 了 :
summary
Vue Encapsulated transition and animation
effect : In the insert 、 Update or remove DOM Element time , Add style class names to elements when appropriate .
Icon :
How to write it :
Prepare the style :
- Element entry style :
- v-enter: The starting point of entry
- v-enter-active: In the process
- v-enter-to: Enter the end of
- The style that the element leaves :
- v-leave: The starting point of departure
- v-leave-active: In the process of leaving
- v-leave-to: The end of leaving
- Element entry style :
Use
<transition>
Wrap over elements , And configuration name attribute :<transition name="hello"> <h1 v-show="isShow"> How do you do !</h1> </transition>
remarks : If there are multiple elements that need to be overused , You need to use :
<transition-group>
, And each element must specifykey
value .If it's animation , You only need to use
v-enter-active
、v-leave-active
. In case of transition , Can be used to .
边栏推荐
- Three schemes to improve the efficiency of MySQL deep paging query
- AI painting minimalist tutorial
- C语言程序设计
- Apache服务器访问日志access.log设置
- SQL language
- 求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
- Is the outdoor LED screen waterproof?
- 爬虫练习题(一)
- 使用宝塔部署halo博客
- Oracle was named the champion of Digital Innovation Award by Ventana research
猜你喜欢
Dgraph: large scale dynamic graph dataset
实战:fabric 用户证书吊销操作流程
2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
ASP.NET Core入门一
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
光环效应——谁说头上有光的就算英雄
Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
.Net之延迟队列
数据库公共字段自动填充
随机推荐
2022G3锅炉水处理考试题模拟考试题库及模拟考试
In 2022, it will be es2022 soon. Do you only know the new features of ES6?
Oracle 被 Ventana Research 评为数字创新奖总冠军
Commvault 和 Oracle 合作,在 Oracle 云上提供 Metallic数据管理即服务
C语言课程设计题
C#基础补充
C语言程序设计
Cors: standard scheme of cross domain resource request
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
实时云交互如何助力教育行业发展
WPF double slider control and forced capture of mouse event focus
Node の MongoDB 安装
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
C语言小型商品管理系统
Using nsproxy to forward messages
Fs4056 800mA charging IC domestic fast charging power IC
N++ is not reliable
C语言集合运算
CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021
CANN算子:利用迭代器高效实现Tensor数据切割分块处理