--- title: 使用 CSS 动画 slug: Web/CSS/CSS_Animations/Using_CSS_animations tags: - Advanced - CSS - CSS Animations - Example - Experimental - Guide - Using CSS animations translation_of: Web/CSS/CSS_Animations/Using_CSS_animations ---
{{SeeCompatTable}}{{CSSRef}}
CSS animations 使得可以将从一个CSS样式配置转换到另一个CSS样式配置。动画包括两个部分:描述动画的样式规则和用于指定动画开始、结束以及中间点样式的关键帧。
相较于传统的脚本实现动画技术,使用CSS动画有三个主要优点:
创建动画序列,需要使用{{ cssxref("animation") }}属性或其子属性,该属性允许配置动画时间、时长以及其他动画细节,但该属性不能配置动画的实际表现,动画的实际表现是由 {{ cssxref("@keyframes") }}规则实现,具体情况参见使用keyframes定义动画序列小节部分。
{{ cssxref("animation") }}的子属性有:
一旦完成动画的时间设置, 接下来就需要定义动画的表现。通过使用{{ cssxref("@keyframes") }}建立两个或两个以上关键帧来实现。每一个关键帧都描述了动画元素在给定的时间点上应该如何渲染。
因为动画的时间设置是通过CSS样式定义的,关键帧使用{{ cssxref("percentage") }}来指定动画发生的时间点。0%表示动画的第一时刻,100%表示动画的最终时刻。因为这两个时间点十分重要,所以还有特殊的别名:from和to。这两个都是可选的,若from/0%或to/100%未指定,则浏览器使用计算值开始或结束动画。
也可包含额外可选的关键帧,描述动画开始和结束之间的状态。
-webkit-前缀。该例中{{ HTMLElement("p") }} 元素由浏览器窗口右边滑至左边
p {
animation-duration: 3s;
animation-name: slidein;
}
@keyframes slidein {
from {
margin-left: 100%;
width: 300%;
}
to {
margin-left: 0%;
width: 100%;
}
}
{{ cssxref("animation-duration") }}属性指定 {{ HTMLElement("p") }} 上的动画从开始到结束耗费3秒,{{ cssxref("@keyframes") }} 指定使用名字为"slidein"的关键帧。
如果希望在不支持CSS动画的浏览器中使用自定义样式,应该将其写在这里;然而,在该例中,我们不需要除动画效果以外的任何自定义样式。
关键帧是用{{ cssxref("@keyframes") }}定义的。该例中,我们只使用了两个关键帧。第一个出现在0%(此例中使用了别名from)处,此处元素的左边距为100%(即位于容器的右边界),宽为300%(即容器宽度的3倍),使得在动画的第一帧中标题位于浏览器窗口右边界之外。
第二帧出现在100%(此例中使用了别名to)。元素的左边距设为0%,宽设为100%,使得动画结束时元素与窗口左边界对齐。
<p>The Caterpillar and Alice looked at each other for some time in silence: at last the Caterpillar took the hookah out of its mouth, and addressed her in a languid, sleepy voice.</p>
{{EmbedLiveSample("文本滑过浏览器窗口","100%","250")}}
让我们给上面的示例中添加一个关键帧,比如标题的字号先变大然后恢复正常,添加这个关键帧十分简单:
75% {
font-size: 300%;
margin-left: 25%;
width: 150%;
}
在动画序列执行到75%的时候,标题元素的左边距为25%,宽度为150%。
{{EmbedLiveSample("增加关键帧","100%","250")}}
{{ cssxref("animation-iteration-count") }}用以指定动画重复的次数,仅仅使用该属性就能使动画重复播放。在该例中,设该属性为infinite以使动画无限重复
p {
animation-duration: 3s;
animation-name: slidein;
animation-iteration-count: infinite;
}
{{EmbedLiveSample("重复动画","100%","250")}}
上面实现了动画的重复播放,但是每次动画开始时总跳回开始位置显得很怪异。我们真正想要的是标题来回滑动,这时只需要设置{{ cssxref("animation-direction") }}属性为alternate。
p {
animation-duration: 3s;
animation-name: slidein;
animation-iteration-count: infinite;
animation-direction: alternate;
}
{{EmbedLiveSample("来回运动","100%","250")}}
.slidein {
-moz-animation-duration: 3s;
-webkit-animation-duration: 3s;
animation-duration: 3s;
-moz-animation-name: slidein;
-webkit-animation-name: slidein;
animation-name: slidein;
-moz-animation-iteration-count: 3;
-webkit-animation-iteration-count: 3;
animation-iteration-count: 3;
-moz-animation-direction: alternate;
-webkit-animation-direction: alternate;
animation-direction: alternate;
}
@-moz-keyframes slidein {
from {
margin-left:100%;
width:300%
}
to {
margin-left:0%;
width:100%;
}
}
@-webkit-keyframes slidein {
from {
margin-left:100%;
width:300%
}
to {
margin-left:0%;
width:100%;
}
}
@keyframes slidein {
from {
margin-left:100%;
width:300%
}
to {
margin-left:0%;
width:100%;
}
}
我们使用JavaScript代码监听所有三种可能的动画事件,setup()方法设置事件监听器,当文档第一次加载完成时执行该方法。
var e = document.getElementById("watchme");
e.addEventListener("animationstart", listener, false);
e.addEventListener("animationend", listener, false);
e.addEventListener("animationiteration", listener, false);
e.className = "slidein";
class为slidein,启动动画。animationstart事件在动画一开始时就被触发,在我们的示例中,该事件在我们的代码执行前就被触发,所以我们自己通过设置元素的的class来启动动画。事件传递给listener()函数,代码如下所示
function listener(e) {
var l = document.createElement("li");
switch(e.type) {
case "animationstart":
l.innerHTML = "Started: elapsed time is " + e.elapsedTime;
break;
case "animationend":
l.innerHTML = "Ended: elapsed time is " + e.elapsedTime;
break;
case "animationiteration":
l.innerHTML = "New loop started at time " + e.elapsedTime;
break;
}
document.getElementById("output").appendChild(l);
}
这段代码同样非常简单,简单地通过{{ domxref("event.type") }}来判断发生的是何种事件,然后添加对应的注解到{{ HTMLElement("ul") }}中。
输出结果如下所示:
animationiteration事件,而触发animationend事件。下面是示例中的用到的HTML代码:
<body> <h1 id="watchme">Watch me move</h1> <p>This example shows how to use CSS animations to make <code>h1</code> elements move across the page.</p> <p>In addition, we output some text each time an animation event fires, so you can see them in action.</p> <ul id="output"> </ul> </body>
{{EmbedLiveSample('使用动画事件', '600', '300')}}