CSS3 animation动画与transition过渡比较

共同点 

animation动画与transition过渡都可以实现动画效果,拥有下面共同的属性

animation:mymove 2s linear infinite alternate;
transition: width 1s ease-out;

1、动画名称(name)@key-frame

2、过渡时间(duration)规定动画完成一个周期所花费的秒或毫秒。默认是 0。

3、延迟时间(delay)规定动画何时开始。

4、时间函数(timing-function)规定动画的速度曲线,默认是 "ease"。

描述
linear 动画从头到尾的速度是相同的。
ease 默认。动画以低速开始,然后加快,在结束前变慢。
ease-in 动画以低速开始。
ease-out 动画以低速结束。
ease-in-out 动画以低速开始和结束。
cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。

区别

animation动画比transition过渡多了下面的属性。

1、播放次数(iteration-count)

animation:mymove 2s linear infinite; 

参数:

n:表示播放n次

infinite:无限播放

2、播放方向(direction)

animation:mymove 2s linear 1 alternate;

参数:

normal:默认值。动画应该正常播放。

alternate:动画先正后反播放。

reverse:动画反向播放。

3、动画时间外的状态(fill-mode)

animation-fill-mode : none | forwards | backwards | both;
描述
none 不改变默认行为。
forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)。
backwards 在 animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)。
both 向前和向后填充模式都被应用

4、是否暂停(play-state)

animation-play-state: paused|running;

另外animation解决了transition与display不兼容的bug。

相关代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .demo1{
                width:100px;
                height: 100px;
                background: #608CF8;
                transition: transform 1s ease-out;
            }
            .demo1:hover{
                transform: rotate(90deg);
            }
            
            .demo2{
                width:200px;
                height:200px;
                background: #849320;
            }
            .demo2:hover{
                animation:mymove 2s linear infinite alternate;
            }
            @keyframes mymove{
                100%{
                    transform:translateX(200px)
                }
            }
        </style>
    </head>
    <body>
        <div class="demo1">transition</div>
        <div class="demo2">animation</div>
    </body>
</html>

猜你喜欢

转载自www.cnblogs.com/liangtao999/p/11802466.html