CSS3 animation(动画)属性详解

CSS3 animation(动画)属性详解

1.语法

简写为:animation: name duration timing-function delay iteration-count direction fill-mode;

@keyframes 规定 @keyframes 动画的名称。
animation 所有动画属性的简写属性,除了 animation-play-state 属性。
animation-name 规定 @keyframes 动画的名称。
animation-duration 规定动画完成一个周期所花费的秒或毫秒。默认是 0。
animation-timing-function 规定动画的速度曲线。默认是 “ease”。
animation-delay 规定动画何时开始。默认是 0。
animation-iteration-count 规定动画被播放的次数。默认是 1。
animation-direction 规定动画是否在下一周期逆向地播放。默认是 “normal”。
animation-play-state 规定动画是否正在运行或暂停。默认是 “running”。
animation-fill-mode 规定对象动画时间之外的状态。

2.animation-iteration-count

语法animation-iteration-count: n|infinite;
n 定义动画播放次数的数值。
infinite 规定动画应该无限次播放。

3.animation-play-state

语法animation-play-state: paused|running;
paused 规定动画已暂停。
running 规定动画正在播放。

4.animation-fill-mode

语法animation-fill-mode : none | forwards | backwards | both;
none 不改变默认行为。
forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)。
backwards 在 animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)。
both 向前和向后填充模式都被应用。
这个属性的意思就是元素都有自己本身的样式,如果你设置了forwards,就代表动画结束之后,元素会直接保留最后一帧的样式,而不会返回到元素本来的样式。如果设置了backwards,就代表点开页面就直接是动画里面第一帧的样式,然后等待动画延迟时间过了就开始执行动画,both就是上面两者都运用,好处就是你只用看关键帧里面定义的样式,而不用管元素本来的样式。

5.示例

<!DOCTYPE html>
<html>
<head>
<style> 
div
{
width:100px;
height:100px;
background:rebeccapurple;
position:relative;
/* 规定 @keyframes 动画的名称。 */
animation-name:myfirst;
/* 规定动画完成一个周期所花费的秒或毫秒。默认是 0。 */
animation-duration: 4s;
/* 规定动画的速度曲线。默认是 "ease"。 */
animation-timing-function: linear;
/* 规定动画的延迟时间。默认是 0。 */
animation-delay: 3s;
/* 规定动画被播放的次数。默认是 1。 */
animation-iteration-count:infinite;
/* 规定动画是否在下一周期逆向地播放。默认是 "normal"。 */
animation-direction:alternate;
/* 规定动画是否正在运行或暂停。默认是 "running"。属性值有两个,为paused和running */
animation-play-state:running;
 /* 当动画完成后,保持最后一个属性值 */
 animation-fill-mode:both;
/* animation: name duration timing-function delay iteration-count direction fill-mode; */
/* 以上可以简写为一个animation */
/* animation:myfirst 4s linear 3s 3 alternate; */
}

@keyframes myfirst
{
0%   {background:red; left:0px; top:0px;}
25%  {background:yellow; left:200px; top:0px;}
50%  {background:blue; left:200px; top:200px;}
75%  {background:green; left:0px; top:200px;}
100% {background:red; left:0px; top:0px;}
}
@-webkit-keyframes myfirst 
{
0%   {background:red; left:0px; top:0px;}
25%  {background:yellow; left:200px; top:0px;}
50%  {background:blue; left:200px; top:200px;}
75%  {background:green; left:0px; top:200px;}
100% {background:red; left:0px; top:0px;}
}
</style>
</head>
<body>
<p><b>注释:</b>本例在 Internet Explorer 中无效。</p>
<div></div>

</body>
</html>

6.补充

不同浏览器需要添加不同的前缀

 	animation:myfirst 5s;
   -moz-animation:myfirst 5s; /* Firefox */
   -webkit-animation:myfirst 5s; /* Safari and Chrome */
   -o-animation:myfirst 5s; /* Opera */
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊啊啊~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值