CSS3动画属性介绍以及动画的使用

目录

一、常见动画属性介绍

1.1、动画定义

1.2、动画属性

二、 动画属性详细介绍

2.1、@keyframes 规则

2.1.1、定义

2.1.2、代码

2.2、animation-duration 属性

2.1.1、定义

2.1.2、代码

2.3、animation-delay 属性

2.3.1、定义

2.3.2 、代码

2.4 animation-iteration-count 属性

2.4.1、定义

2.4.2、代码

2.5 animation-direction 属性

2.5.1、定义

2.5.2、代码

2.6 animation-timing-function 属性

2.6.1、定义

2.6.2、代码

2.7 animation-fill-mode 属性

2.7.1、定义

2.7.2、代码

一、常见动画属性介绍

1.1、动画定义

  1. 动画使元素逐渐从一种样式变为另一种样式。
  2. 您可以随意更改任意数量的 CSS 属性。
  3. 如需使用 CSS 动画,您必须首先为动画指定一些关键帧。
  4. 关键帧包含元素在特定时间所拥有的样式。

1.2、动画属性

属性描述CSS
@keyframes规定动画。3
animation所有动画属性的简写属性。3
animation-name规定 @keyframes 动画的名称。3
animation-duration规定动画完成一个周期所花费的秒或毫秒。默认是 0。3
animation-timing-function规定动画的速度曲线。默认是 "ease"。3
animation-fill-mode规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式。3
animation-delay规定动画何时开始。默认是 0。3
animation-iteration-count规定动画被播放的次数。默认是 1。3
animation-direction规定动画是否在下一周期逆向地播放。默认是 "normal"。3
animation-play-state规定动画是否正在运行或暂停。默认是 "running"。3

二、 动画属性详细介绍

2.1、@keyframes 规则

2.1.1、定义

如果您在 @keyframes 规则中指定了 CSS 样式,动画将在特定时间逐渐从当前样式更改为新样式。

要使动画生效,必须将动画绑定到某个元素。

2.1.2、代码

将 "example" 动画绑定到 <div> 元素。

动画将持续 4 秒钟

同时将 <div> 元素的背景颜色从 "red" 逐渐改为 "yellow":

/* 动画代码 */
@keyframes example {
  from {background-color: red;}
  to {background-color: yellow;}
}

/* 向此元素应用动画效果 */
div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

2.2、animation-duration 属性

2.1.1、定义

  • 注意:animation-duration 属性定义需要多长时间才能完成动画。如果未指定 animation-duration 属性,则动画不会发生,因为默认值是 0s(0秒)。
  • 通过使用关键字 "from" 和 "to"(代表 0%(开始)和 100%(完成)),我们设置了样式何时改变。
  •  您也可以使用百分比值。通过使用百分比,您可以根据需要添加任意多个样式更改。

2.1.2、代码

面的例子将在动画完成 25%,

完成 50% 以及动画完成 100% 时更改 <div> 元素的背景颜色:

/* 动画代码 */
@keyframes example {
  0%   {background-color: red;}
  25%  {background-color: yellow;}
  50%  {background-color: blue;}
  100% {background-color: green;}
}

/* 应用动画的元素 */
div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

2.3、animation-delay 属性

2.3.1、定义

规定动画开始的延迟时间。

2.3.2 、代码

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-delay: 2s;
}

负值也是允许的。如果使用负值,则动画将开始播放,如同已播放 N 秒。

下面的例子中,动画将开始播放,就好像它已经播放了 2 秒钟一样:

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-delay: -2s;
}

2.4 animation-iteration-count 属性

2.4.1、定义

animation-iteration-count 属性指定动画应运行的次数。

2.4.2、代码

下面的例子在停止前把动画运行 3 次:

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 3;
}

使用值 "infinite" 使动画永远持续下去:

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}

2.5 animation-direction 属性

2.5.1、定义

反向或交替运行动画

animation-direction 属性指定是向前播放、向后播放还是交替播放动画。

animation-direction 属性可接受以下值:

  • normal - 动画正常播放(向前)。默认值
  • reverse - 动画以反方向播放(向后)
  • alternate - 动画先向前播放,然后向后
  • alternate-reverse - 动画先向后播放,然后向前

2.5.2、代码

将以相反的方向(向后)运行动画:

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-direction: reverse;
}
下面的例子使用值 "alternate" 使动画先向前运行,然后向后运行:
div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: alternate;
}

使用值 "alternate-reverse" 使动画先向后运行,然后向前运行:

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: alternate-reverse;
}

2.6 animation-timing-function 属性

2.6.1、定义

指定动画的速度曲线

animation-timing-function 属性规定动画的速度曲线。

animation-timing-function 属性可接受以下值:

  • ease - 指定从慢速开始,然后加快,然后缓慢结束的动画(默认)
  • linear - 规定从开始到结束的速度相同的动画
  • ease-in - 规定慢速开始的动画
  • ease-out - 规定慢速结束的动画
  • ease-in-out - 指定开始和结束较慢的动画
  • cubic-bezier(n,n,n,n) - 运行您在三次贝塞尔函数中定义自己的值

2.6.2、代码

可以使用的一些不同速度曲线:

#div1 {animation-timing-function: linear;}
#div2 {animation-timing-function: ease;}
#div3 {animation-timing-function: ease-in;}
#div4 {animation-timing-function: ease-out;}
#div5 {animation-timing-function: ease-in-out;}

2.7 animation-fill-mode 属性

2.7.1、定义

指定动画的填充模式

CSS 动画不会在第一个关键帧播放之前或在最后一个关键帧播放之后影响元素。animation-fill-mode 属性能够覆盖这种行为。

在不播放动画时(在开始之前,结束之后,或两者都结束时),animation-fill-mode 属性规定目标元素的样式。

animation-fill-mode 属性可接受以下值:

  • none - 默认值。动画在执行之前或之后不会对元素应用任何样式。
  • forwards - 元素将保留由最后一个关键帧设置的样式值(依赖 animation-direction 和 animation-iteration-count)。
  • backwards - 元素将获取由第一个关键帧设置的样式值(取决于 animation-direction),并在动画延迟期间保留该值。
  • both - 动画会同时遵循向前和向后的规则,从而在两个方向上扩展动画属性。

2.7.2、代码

在动画开始之前使 <div> 元素获得第一个关键帧设置的样式值,以及在动画结束时保留最后一个关键帧的样式值:

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
  animation-delay: 2s;
  animation-fill-mode: both;
}

让 <div> 元素在动画结束时保留来自最后一个关键帧的样式值:

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
  animation-fill-mode: forwards;
}

在动画开始之前(在动画延迟期间)使 <div> 元素获得由第一个关键帧设置的样式值:

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
  animation-delay: 2s;
  animation-fill-mode: backwards;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值