| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 58a7a6ebf9a6318348e2d5aa | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | title: 修改动画的填充模式 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2020-02-11 15:46:34 +08:00
										 |  |  | videoUrl: 'https://scrimba.com/c/cVJDmcE' | 
					
						
							|  |  |  | forumTopicId: 301064 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: modify-fill-mode-of-an-animation | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 太棒了,但是现在还不完美。 注意动画在 `500ms` 之后重置了,所以按钮又变成了之前的颜色。 而我们想要的效果是按钮在悬停时始终高亮。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 为此,我们可以通过把 `animation-fill-mode` 设置成 `forwards` 来实现。 `animation-fill-mode` 指定了在动画结束时元素的样式: 你可以这样设置: | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 10:58:20 -06:00
										 |  |  | ```css | 
					
						
							|  |  |  | animation-fill-mode: forwards; | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 10:58:20 -06:00
										 |  |  | 设置 `button:hover` 的 `animation-fill-mode` 属性为 `forwards`,使用户把鼠标悬停在按钮上时,按钮保持高亮。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 10:58:20 -06:00
										 |  |  | `button:hover` 应有一个值为 `forwards` 的 `animation-fill-mode` 属性。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert( | 
					
						
							|  |  |  |   code.match( | 
					
						
							|  |  |  |     /button\s*?:\s*?hover\s*?{[\s\S]*animation-fill-mode\s*?:\s*?forwards\s*?;[\s\S]*}/gi | 
					
						
							|  |  |  |   ) && | 
					
						
							|  |  |  |     code.match( | 
					
						
							|  |  |  |       /button\s*?:\s*?hover\s*?{[\s\S]*animation-name\s*?:\s*?background-color\s*?;[\s\S]*}/gi | 
					
						
							|  |  |  |     ) && | 
					
						
							|  |  |  |     code.match( | 
					
						
							|  |  |  |       /button\s*?:\s*?hover\s*?{[\s\S]*animation-duration\s*?:\s*?500ms\s*?;[\s\S]*}/gi | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01: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; | 
					
						
							|  |  |  |     /* Only change code below this line */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Only change code above this line */ | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   @keyframes background-color { | 
					
						
							|  |  |  |     100% { | 
					
						
							|  |  |  |       background-color: #4791d0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | <button>Register</button> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							| 
									
										
										
										
											2020-02-11 15:46:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01: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; | 
					
						
							|  |  |  |     animation-fill-mode: forwards; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   @keyframes background-color { | 
					
						
							|  |  |  |     100% { | 
					
						
							|  |  |  |       background-color: #4791d0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | <button>Register</button> | 
					
						
							|  |  |  | ``` |