2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d78a7367417b2b2512ae0
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 使用CSS动画更改按钮的悬停状态
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2020-02-11 07:46:34 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cg4vZAa'
|
|
|
|
|
forumTopicId: 301073
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: use-css-animation-to-change-the-hover-state-of-a-button
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
你可以在按钮悬停时使用 `@keyframes` 改变按钮的颜色。
|
|
|
|
|
|
2020-02-11 07:46:34 +00:00
|
|
|
|
下面是在图片悬停时改变图片宽度的例子:
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
img:hover {
|
|
|
|
|
animation-name: width;
|
|
|
|
|
animation-duration: 500ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@keyframes width {
|
|
|
|
|
100% {
|
|
|
|
|
width: 40px;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<img src="https://bit.ly/smallgooglelogo" alt="Google's Logo" />
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
注意 `ms` 代表毫秒,1000ms 等于 1s。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
使用 `@keyframes` 来改变 `button` 元素的 `background-color`,使其在悬停时变成 `#4791d0`。`@keyframes` 规则应该只有一个 `100%` 条目。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`@keyframes` 规则的 `animation-name` 应该是 background-color。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/@keyframes\s+?background-color\s*?{/g));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
在 `@keyframes` 为 `100%` 的位置,应将 `background-color` 改成 `#4791d0`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/100%\s*?{\s*?background-color:\s*?#4791d0;\s*?}/gi));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-02-11 07:46:34 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
button {
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
color: white;
|
|
|
|
|
background-color: #0F5897;
|
|
|
|
|
padding: 5px 10px 8px 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
button:hover {
|
|
|
|
|
animation-name: background-color;
|
|
|
|
|
animation-duration: 500ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<button>Register</button>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
button {
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
color: white;
|
|
|
|
|
background-color: #0F5897;
|
|
|
|
|
padding: 5px 10px 8px 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
button:hover {
|
|
|
|
|
animation-name: background-color;
|
|
|
|
|
animation-duration: 500ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@keyframes background-color {
|
|
|
|
|
100% {
|
|
|
|
|
background-color: #4791d0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<button>Register</button>
|
|
|
|
|
```
|