CSS动画: CSS动画是一种改变网页中各种元素的外观和行为的技术。它通过改变元素的运动或显示来控制元素。它由两部分组成,一部分包含描述元素动画的CSS属性,另一部分包含某些关键帧,这些关键帧指示元素的动画属性以及这些属性必须出现的特定时间间隔。
这个 @关键帧规则 : 关键帧是CSS动画工作的基础。它们定义动画在其整个持续时间的各个阶段的显示。例如:在下面的代码中,段落的颜色会随着时间而改变。完成率为0%时为红色,完成率为50%时为橙色,完成率为100%时为棕色。
例子: 本例描述了使用@keyframe规则的CSS动画。
HTML
<!DOCTYPE html> < html > < head > < style > #gfg { animation-name: color; animation-duration: 25s; padding-top: 30px; padding-bottom: 30px; font-family: Times New Roman; } #geeks { font-size: 40px; text-align: center; font-weight: bold; color: #090; padding-bottom: 5px; } #geeks1 { font-size: 17px; font-weight: bold; text-align: center; } @keyframes color { 0% { background-color: red; } 50% { background-color: orange; } 100% { background-color: brown; } } </ style > </ head > < body > < div id = "gfg" > < div id = "geeks" >GeeksforGeeks</ div > < div id = "geeks1" >A computer science portal for geeks</ div > </ div > </ body > </ html > |
输出:
动画属性: 下面给出了某些动画特性:
动画名称 : 它用于指定描述动画的@keyframes的名称。
animation-name: animation_name;
动画持续时间 : 它用于指定动画完成一个周期所需的持续时间。
例子: 本例使用动画持续时间属性描述CSS动画属性。
HTML
< html > < head > < style > #gfg1 { animation-name: text; animation-duration: 5s; animation-iteration-count: infinite; } #geek1 { font-size: 40px; text-align: center; font-weight: bold; color: #090; padding-bottom: 5px; } #geek2 { font-size: 17px; font-weight: bold; text-align: center; } @keyframes text { from { margin-top: 400px; } to { margin-top: 0px; } } </ style > </ head > < body > < div id = "gfg1" > < div id = "geek1" >GeeksforGeeks</ div > < div id = "geek2" >A computer science portal for geeks</ div > </ div > </ body > </ html > |
输出:
动画将如下所示:
动画计时功能 : 指定动画如何通过关键帧进行变换。它可以具有以下值:
- 轻松: 动画开始缓慢,然后快速,最后缓慢结束(这是默认设置)
- 线性的: 动画从头到尾以相同的速度播放
- 放松: 动画以缓慢的开始播放
- 放松: 动画以缓慢的结尾播放
- 轻松进出: 动画开始和结束缓慢。
例子: 本例使用动画计时功能属性描述CSS动画属性。
HTML
<!DOCTYPE html> < html > < head > < style > .geeks { font-size: 40px; text-align: center; font-weight: bold; color: #090; padding-bottom: 5px; font-family: Times New Roman; } .geeks1 { font-size: 17px; font-weight: bold; text-align: center; font-family: Times New Roman; } h2 { width: 350px; animation-name: text; animation-duration: 4s; animation-iteration-count: infinite; background-color: rgb(255, 210, 85); } #one { animation-timing-function: ease; } #two { animation-timing-function: linear; } #three { animation-timing-function: ease-in; } #four { animation-timing-function: ease-out; } #five { animation-timing-function: ease-in-out; } @keyframes text { from { margin-left: 60%; } to { margin-left: 0%; } } </ style > </ head > < body > < div class = "geeks" >GeeksforGeeks</ div > < div class = "geeks1" >A computer science portal for geeks</ div > < h2 id = "one" >This text is ease.</ h2 > < h2 id = "two" >This text is linear.</ h2 > < h2 id = "three" >This text is ease-in.</ h2 > < h2 id = "four" >This text is ease-out.</ h2 > < h2 id = "five" >This text is ease-in-out.</ h2 > </ body > </ html > |
输出:
动画延迟 : 它用于指定动画开始时的延迟。
例子: 本例使用动画延迟属性描述CSS动画属性。
HTML
<!DOCTYPE html> < html > < head > < style > .geeks { font-size: 40px; text-align: center; font-weight: bold; color: #090; padding-bottom: 5px; font-family: Times New Roman; } .geeks1 { font-size: 17px; font-weight: bold; text-align: center; font-family: Times New Roman; } #one { animation-name: example; animation-duration: 10s; } #two { animation-name: example; animation-duration: 10s; animation-delay: 10s; } @keyframes example { from { background-color: orange; } to { background-color: white; } } </ style > </ head > < body > < div class = "geeks" >GeeksforGeeks</ div > < div class = "geeks1" >A computer science portal for geeks</ div > < h2 id = "one" >Text animation without delayed.</ h2 > < h2 id = "two" >Text animation with 10 second delay.</ h2 > </ body > </ html > |
输出:
动画迭代计数 : 它用于指定动画将重复的次数。它可以指定为无限,无限期地重复动画。
例子: 此示例使用动画迭代计数属性描述CSS动画属性。
HTML
<!DOCTYPE html> < html > < head > < style > .geeks { font-size: 40px; text-align: center; font-weight: bold; color: #090; padding-bottom: 5px; font-family: Times New Roman; } .geeks1 { font-size: 17px; font-weight: bold; text-align: center; font-family: Times New Roman; } #one { animation-name: example; animation-duration: 2s; animation-iteration-count: 2; } #two { animation-name: example; animation-duration: 2s; animation-iteration-count: infinite; } @keyframes example { from { background-color: orange; } to { background-color: white; } } </ style > </ head > < body > < div class = "geeks" >GeeksforGeeks</ div > < div class = "geeks1" >A computer science portal for geeks</ div > < h2 id = "one" >This text changes its color two times.</ h2 > < h2 id = "two" >This text changes its color infinite times.</ h2 > </ body > </ html > |
输出:
动画方向 : 指定动画的方向。它可以具有以下值:
- 正常: 动画向前播放。这是默认值。
- 相反: 动画以相反的方向播放,即向后播放。
- 备选方案: 动画先向前播放,然后向后播放。
- 交替反转: 动画先向后播放,然后向前播放。
例子: 此示例使用“动画方向”属性描述CSS动画属性。
HTML
<!DOCTYPE html> < html > < head > < style > .geeks { font-size: 40px; text-align: center; font-weight: bold; color: #090; padding-bottom: 5px; font-family: Times New Roman; } .geeks1 { font-size: 17px; font-weight: bold; text-align: center; font-family: Times New Roman; } h2 { width: 100%; animation-name: text; animation-duration: 2s; animation-iteration-count: infinite; } #one { animation-direction: normal; } #two { animation-direction: reverse; } #three { animation-direction: alternate; } #four { animation-direction: alternate-reverse; } @keyframes text { from { margin-left: 60%; } to { margin-left: 0%; } } </ style > </ head > < body > < div class = "geeks" >GeeksforGeeks</ div > < div class = "geeks1" >A computer science portal for geeks</ div > < h2 id = "one" >This text is normal.</ h2 > < h2 id = "two" >This text is reverse.</ h2 > < h2 id = "three" >This text is alternate.</ h2 > < h2 id = "four" >This text is alternate-reverse.</ h2 > </ body > </ html > |
输出:
动画填充模式 : 指定动画在执行前后应用的值。
- 无: 动画不会在执行元素之前或之后对其应用任何属性。这是默认值。
- 转发: 动画完成后,元素将保留与最后一个关键帧相同的动画属性。
- 向后的: 该元素将在动画开始之前获取第一个关键帧的属性。
- 两者: 动画将遵循向前和向后的规则,即在开始之前获得为初始关键帧定义的属性,并在动画完成后保留最后一个关键帧的值。
例子: 本示例使用动画填充模式属性描述CSS动画属性。
HTML
<!DOCTYPE html> < html > < head > < style > .geeks { font-size: 40px; text-align: center; font-weight: bold; color: #090; padding-bottom: 5px; font-family: Times New Roman; } .geeks1 { font-size: 17px; font-weight: bold; text-align: center; font-family: Times New Roman; } h2 { width: 400px; background-color: orange; animation-name: text; animation-duration: 3s; } #one { animation-fill-mode: none; } #two { animation-fill-mode: forwards; } #three { animation-fill-mode: backwards; animation-delay: 2s; } #four { animation-fill-mode: both; animation-delay: 2s; } @keyframes text { from { margin-left: 0%; background-color: #aaaaaa; } to { margin-left: 60%; background-color: #008000; } } </ style > </ head > < body > < div class = "geeks" >GeeksforGeeks</ div > < div class = "geeks1" >A computer science portal for geeks</ div > < h2 id = "one" >none</ h2 > < h2 id = "two" >forwards</ h2 > < h2 id = "three" >backwards</ h2 > < h2 id = "four" >both</ h2 > </ body > </ html > |
输出:
动画播放状态 : 这允许您播放/暂停动画。
动画速记属性: 这是一种暗示动画属性的简写方式,用于更快的代码。属性应按以下顺序排列:
animation: [animation-name] [animation-duration] [animation-timing-function] [animation-delay] [animation-iteration-count] [animation-direction] [animation-fill-mode] [animation-play-state];
例如,通常动画代码如下所示:
例子: 本例使用动画播放状态属性描述CSS动画属性,而不使用动画速记属性。
HTML
<!DOCTYPE html> < html > < head > < style > #g4g { width: 400px; height: 100px; position: relative; animation-name: GFG; animation-duration: 5s; animation-timing-function: linear; animation-delay: 1s; animation-iteration-count: infinite; animation-direction: alternate; } @keyframes GFG { 0% { left: 0px; top: 0px; } 25% { left: 200px; top: 200px; } 50% { left: 200px; top: 0px; } 75% { left: 0px; top: 200px; } 100% { left: 0px; top: 0px; } } </ style > </ head > < body > < img id = "g4g" src = </ body > </ html > |
输出:
简而言之,上述HTML代码可以写成:
例子: 此示例使用动画播放状态属性和动画速记属性描述CSS动画属性。
HTML
<!DOCTYPE html> < html > < head > < style > #geeks4g { width: 400px; height: 100px; position: relative; animation: GFG 5s linear 1s infinite alternate; } @keyframes GFG { 0% { left: 0px; top: 0px; } 25% { left: 200px; top: 200px; } 50% { left: 200px; top: 0px; } 75% { left: 0px; top: 200px; } 100% { left: 0px; top: 0px; } } </ style > </ head > < body > < img id = "geeks4g" src = </ body > </ html > |
输出:
支持的浏览器:
- 谷歌Chrome 43.0
- 微软Edge 12.0
- Firefox 16.0
- Safari 9.0
- 歌剧30.0