--- id: 587d78a7367417b2b2512adf title: Learn How the CSS @keyframes and animation Properties Work challengeType: 0 videoUrl: '' localeTitle: 了解CSS @keyframes和动画属性的工作原理 --- ## Description
要为元素设置动画,您需要了解动画属性和@keyframes规则。动画属性控制动画的行为方式, @keyframes规则控制动画期间发生的事情。总共有八个动画属性。这个挑战将保持简单,并首先覆盖两个最重要的: animation-name设置animation-name ,后来@keyframes使用@keyframes来告诉CSS哪些规则与哪些动画一起使用。 animation-duration设置animation-duration长度。 @keyframes是如何准确指定动画在整个持续时间内发生的事情。这是通过在动画期间为特定“帧”提供CSS属性来完成的,百分比范围为0%到100%。如果将其与电影进行比较,则0%的CSS属性是元素在开场场景中的显示方式。 100%的CSS属性是元素在信用点滚动之前的结尾显示的方式。然后CSS应用魔法在给定的持续时间内转换元素以执行场景。这是一个例子来说明@keyframes和动画属性的用法:
#anim {
动画名称:多彩;
动画持续时间:3s;
}
@keyframes colorful {
0%{
背景颜色:蓝色;
}
100%{
背景颜色:黄色;
}
}
对于具有anim ID的元素,上面的代码片段将animation-namecolorful ,并将animation-duration为3秒。然后@keyframes规则链接到名称为colorful的动画属性。它在动画开始时将颜色设置为蓝色(0%),在动画结束时将其转换为黄色(100%)。您不仅限于开始 - 结束转换,您可以为0%和100%之间的任何百分比设置元素的属性。
## Instructions
通过将animation-name设置为rainbow并将animation-duration为4秒,为id为rect的元素创建动画。接下来,声明一个@keyframes规则,并将动画开头的background-color0% )设置为蓝色,将动画的中间( 50% )设置为绿色,将动画的结尾( 100% )设置为黄色。
## Tests
```yml tests: - text: id为rect的元素应该有一个animation-name属性,其值为rainbow。 testString: 'assert($("#rect").css("animation-name") == "rainbow", "The element with id of rect should have an animation-name property with a value of rainbow.");' - text: id为rect的元素应该具有值为4s的animation-duration属性。 testString: 'assert($("#rect").css("animation-duration") == "4s", "The element with id of rect should have an animation-duration property with a value of 4s.");' - text: @keyframes规则应该使用rainbow的animation-name 。 testString: 'assert(code.match(/@keyframes\s+?rainbow\s*?{/g), "The @keyframes rule should use the animation-name of rainbow.");' - text: 彩虹的@keyframes规则应使用0%的蓝色background-color 。 testString: 'assert(code.match(/0%\s*?{\s*?background-color:\s*?blue;\s*?}/gi), "The @keyframes rule for rainbow should use a background-color of blue at 0%.");' - text: 彩虹的@keyframes规则应使用50%的绿色background-color 。 testString: 'assert(code.match(/50%\s*?{\s*?background-color:\s*?green;\s*?}/gi), "The @keyframes rule for rainbow should use a background-color of green at 50%.");' - text: 彩虹的@keyframes规则应使用100%的黄色background-color 。 testString: 'assert(code.match(/100%\s*?{\s*?background-color:\s*?yellow;\s*?}/gi), "The @keyframes rule for rainbow should use a background-color of yellow at 100%.");' ```
## Challenge Seed
```html
```
## Solution
```js // solution required ```