Home > Article > Web Front-end > Introduction to the difference between transition and animation in css3
** I have been working on a project for a long time. The two css3 attributes of transition and animation are often used in actual projects. Think about sorting them out. As follows:
1: First introduce the transition:
a, we often encounter such situations when doing projects, such as a button , which changes when the mouse is moved into it. Button background color and font color, at this time we usually do this:
.btn{width: 80px;height: 25px;border: 1px solid #333;color:#333;text-align: center;line-height: 25px;} .btn:hover{background:green;color:white;}
b, we will find that the background and The font color changes instantly, does it look particularly stiff?
The transition appears at this time, see the code:
.btn{width: 80px;height: 25px;border: 1px solid #333;color:#333;text-align: center;line-height: 25px;} .btn:hover{background:green;color:white;transition:0.4s;}
c, after adding transition, we will find that the button background color and font color have a gradual process of time until the end.
How does this gradualness come about? Yes, it is 0.4s.
When talking about 0.4s, we have to talk about the various attributes of transition. I am not going to go into detail here. I wrote them all on the blog because you can completely check the information yourself.
(1: The above 0.4s is the abbreviation of one of the attributes of transition: transition-duration
transition-duration, as the name suggests, represents the duration of animation, which is the above 0.4s. The animation of background color and font color is completed in 0.4 seconds.
(2: When it comes to the continuous animation in 0.4 seconds, we have to talk about the speed of object movement. Know that a thing has the following types of motion:
(a linear: uniform speed
(b ease-in: acceleration
(c ease-out: deceleration
(d cubic-bezierFunction: Custom speed mode (almost not used)
In the above code, only transition is simply written: 0.4s; Why does it have a movement?
(3: It’s like this. Transition has an attribute called transition-timing-function. The default is ease, and its movement is gradually slowed down.
Not abbreviated is transition: 0.4s ease
d. We see that after the button hovers, all changes described by css in the hover style have the transition described Animation.
My explanation of this sentence is a bit convoluted. Let’s explain it directly with the code:
1) If we only want the background color to change over a period of time, we How to do it?
transition: 0.4s background ease-in
2) We saw the background in the above code, yes, that’s why It specifies that only the background color of the animation has this time period.
It is the abbreviation of one of the properties of transition, called: transition-property. As the name suggests, it specifies the property.
e, we will find in actual projects that sometimes we need an animation to have a delayed display, and do not want it to generate animation immediatelyAt this time, another attribute of transition comes out , transition-delaySee code:transition: 0.4s 1s;
We will find that the background font animation of this button at this time It starts after 1 second.
event
to be triggered, such as hover, so it cannot occur automatically when the web page is loaded2: Transition is one-time and cannot occur repeatedly , unless triggered repeatedly.
3: Transition can only define the startstate
and the end state, but cannot define the intermediate state, which means there are only two states. (To express clearly, the above 4 restrictions are based on the introduction I quoted from Ruan Yifeng’s blog) In order to break through these restrictions, animation Out. 2: animation: a), let’s not explain the various attributes of animation in detail first, let’s go directly to the showing code.c{width: 100px;height: 100px;margin: 200px 0 300px 300px;background: orange; } .c:hover{animation: 2s change infinite;} @keyframes change { 0% { background: orange; } 50% { background: pink;width: 200px; } 100% { background: red;height: 300px; } }
b),当鼠标移入p的时候,p会发生一系列的样式改变(截图无法表现过程),在2秒的时间完成背景颜色以及宽高的变化,并无限制重复这个两秒的动画,是因为什么呢?
c),我准备作这样的解释,此时,你需要做一个animation动画只需要3点
1. 需要一个承载动画的元素,如p
2. 当前的元素写一个animation的css,其中定义你所需要这个动画产生的效果。(你暂时不需要知道如何编写这个动画内部的css)
3. 编写一个动画进程,以@keyframes关键字来定义,而这个动画的进程有一个名字,如change,
----------》你可以把这个进程理解成一个函数,@keyframes对应的就是function,而change对应的就是函数名字-----------》最终等待被调用。
好了,明白了以上三点,我们就可以来剖析animation的庐山真面目了。
a)我们接着再来看这段代码:
animation: 2s change infinite; @keyframes change { 0% { background: orange; } 50% { background: pink;width: 200px; } 100% { background: red;height: 300px; } }
1. 我们先来看这个“所谓的函数change”
(1):这个change是animation其中的一个属性,叫做动画名字-----》 animation-name:change;
2. 我们再来看这个“百分比”
(1):这个百分比你只要理解它是这个这个动画在多少时间内完成的一系列样式改变的进度。这个进度你可以描绘这个元素你想改变的的样式属性(可以定义多种)
(2):当然也可以这样写:
@keyframes change { from { background: orange; } 50% { background: pink;width: 200px; } to { background: red;height: 300px; } }
3.看完了动画制作的过程,现在我们来看如何调用这个动画:见代码:
.c:hover{animation: 2s change infinite;}
(1),机智的你肯定看到了2s,是的没错,就是它让动画2秒完成。和transition一样,它是animation的一个属性,
叫做:animation-duration: 2s;
(2),机智的你肯定看到了change,是的没错,就是如此调用这个“动画函数”的.只需要在当前元素animation的css样式里写入就可以了。
(3),刚刚前面我们说了,这段代码会在鼠标移入p元素后2秒的时间完成背景颜色以及宽高的变化,并无限制重复这个两秒的动画
*:注意看到无限制三个字,
*:无限制怎么来的呢?此时这段代码只剩下infinite了。
*:不用想,他也是animation其中之一属性:叫做 animation-iteration-count: infinite;
*:这个属性是用来定义这个动画应该播放多少次,infinite代表的无限制(无数次),当然你也可以在让它播放一个定值的数次,比如3次
animation-iteration-count: 3;
在这里,你只需要在animation里的css里写入次数就可以了:
.c:hover{animation: 2s change 3;}
4:这个动画虽然已经介绍完了,但是我们会发现动画在两秒钟后又会恢复原样(初始状态):
此时我们想让动画两秒执行完毕之后保持在结束状态,这该怎么办了,此时animation的另一个属性就派上用场了
-------------》animation-fill-mode:forwards;
在这里,你只需要在animation里的css里写入forwards就可以了:
.c:hover{animation: 2s change 3 forwards;}
2秒动画结束后就会是这样:
5:同样的,animation也和transition一样具备延时动画的属性:
------------》animation-delay: 1s;
同样的animation简写写法如下,代表鼠标移入p内,1秒后动画开始
.c:hover{animation: 2s 1s change 3 forwards;}
6:同样的,animation也和transition一样具备动画以何种速度呈现的属性:默认是ease,它运动的形式是逐渐放慢的。
------------------》animation-timing-function: ease;
(a linear:匀速
(b ease-in:加速
(c ease-out:减速
(d cubic-bezier函数:自定义速度模式(几乎不用)
****要改变运动形势只要添加相应的速度代表参数了,见代码:
.c:hover{animation: 2s 1s change 3 forwards linear;}
7:animation还有一个属性我就不打算细写了,--------》animation-direction,默认情况下是normal,
它是用来改变动画播放不仅只可以从结束状态跳回到起始状态这种形式。
8:上面说过,animation浏览器一加载便可以自动触发:
.c{width: 100px;height: 100px;margin: 200px 0 300px 300px;background: orange; animation: 2s change forwards;}
** 此时你会发现,浏览器一运行这个p就开始动画了。至于什么时候触发,那就要看项目具体需求了。
结语:自此,css3中的两大动画transition和animation就介绍完了。如有错误,欢迎指正。
The above is the detailed content of Introduction to the difference between transition and animation in css3. For more information, please follow other related articles on the PHP Chinese website!