动画
transition 过渡
语法:transition: <过渡属性> <过渡时间> <缓动函数> <延迟时间>
transition-property
:指定要过渡的 CSS 属性,可以是单个或多个属性,多个属性之间用逗号分隔transition-duration
:指定过渡的持续时间,以秒或毫秒为单位transition-timing-function
:控制过渡过程中变化的速度和方式linear
匀速ease
:默认值,以低速开始,然后加快,在结束前变慢ease-in
:以低速开始ease-out
:以低速结束ease-in-out
:缓慢开始,缓慢结束cubic-bezier()
贝塞尔曲线
transition-delay
:指定过渡效果的延迟时间,以秒或毫秒为单位
.box {
width: 100px;
height: 100px;
background-color: #ff0;
opacity: 0;
transition: all 1s ease;
}
.box:hover {
opacity: 1;
}
.box1 {
width: 100px;
height: 100px;
background-color: #f00;
transition: width 0.5s ease-in-out, height 0.5s ease-in-out;
}
.box1:hover {
width: 200px;
height: 200px;
}
其他的属性:
transition: all
指定所有 CSS 属性都要过渡transition: none
禁用所有过渡效果transition: initial
将所有过渡效果恢复到默认值transition: inherit
从父元素继承过渡效果
注意不是所有的 CSS 属性都支持过渡效果
animation
@keyframes
定义动画规则- 0% 是动画的开始,100% 是动画的完成
- 关键词
from
和to
,等同于 0% 和 100% - 要注意兼容性,一般要加浏览器前缀
示例:animation: box 5s;
表示动画名称是box
,动画时间是 5s
.container {
width: 300px;
height: 300px;
background: #f00;
animation: box 5s;
-o-animation: box 5s; /* Opera */
-moz-animation: box 5s; /* Firefox */
-webkit-animation: box 5s; /* Safari and Chrome */
}
@keyframes box {
0% {
background: #f00;
}
100% {
background: #00f;
}
}
@-o-keyframes box {
/* 同上 */
}
@-moz-keyframes box {
/* 同上 */
}
@-webkit-keyframes box {
/* 同上 */
}
动画属性
animation
简写:animation: name duration timing-function delay iteration-count direction fill-model play-pause:
animation-name
规定@keyframes
动画的名称animation-duration
规定动画完成一个周期所花费的秒或毫秒。默认是 0animation-timing-function
缓动函数,定义在每一个动画周期中执行的节奏linear
:匀速ease
:默认值,动画以低速开始,然后加快,在结束前变慢ease-in
:以低速开始ease-out
:以低速结束ease-in-out
:开始和结束均低速cubic-bezier(n, n, n, n)
:三次贝塞尔曲线,取值范围 0 到 1steps(number, position)
可以让动画不连续- 示例:steps(4, end)
- number:表示把动画分成了多少段
- position:表示动画是从时间段的开头连续还是末尾连续
animation-delay
延迟时间- 立即开始,默认值 0 秒,单位是 s 或 ms
- 正数,表示几秒后开始
- 负数,表示立即开始,但是位置从某个时间点开始
animation-iteration-count
播放次数- 默认值 1,可以是小数
infinite
表示无限次
animation-direction
播放方向normal
:正向播放,默认值reverse
:反向播放alternate
:正反交替播放,第一次迭代是正向播放alternate-reverse
:正反交替播放,第一次迭代是反向播放
animation-fill-mode
动画在执行之前和之后,如何将样式应用于目标animation-play-state
规定动画是否正在运行或暂停running
:运行中,默认值paused
:暂停- 恢复暂停的动画将从暂停时停止的位置开始播放
.container {
width: 300px;
height: 300px;
background: #f00;
animation-name: box;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state: running;
}
以上代码等效于以下所示:
.container {
animation: box 5s linear 2s infinite alternate;
}
animation-timing-function
此属性使用三次贝塞尔曲线来生成曲线
linear
匀速。等同于cubic-bezier(0,0,0.25,1)
ease
默认值。动画以低速开始,然后加快,在结束前变慢。等同于cubic-bezier(0.25,0.1,0.25,1)
ease-in
动画以低速开始。等同于cubic-bezier(0.42,0,1,1)
ease-out
动画以低速结束。等同于cubic-bezier(0,0,0.58,1)
ease-in-out
动画以低速开始和结束。等同于cubic-bezier(0.42,0,0.58,1)
cubic-bezier(n,n,n,n)
三次贝塞尔曲线,取值范围 0 到 1
animation-fill-mode
规定动画在执行之前和之后,如何将样式应用于其目标
none
当动画未执行时,动画不会将任何样式应用于目标forwards
当动画完成后,保持最后一个关键帧的值(由animation-duration
和animation-iteration-count
决定)backwards
立即应用第一个关键帧中定义的值,并在animation-delay
期间保留此值both
两种模式都被应用
假如一个盒子要在动画开始时立即改变颜色,并从左到右运动
/* 延迟 1 秒后执行动画 */
animation-delay: 1s;
/* 1秒后改变颜色,并从左到右运动,运动结束后回到原位置 */
animation-fill-mode: none;
/* 1秒后改变颜色,并从左到右运动,运动结束后保持在最后的位置 */
animation-fill-mode: forwards;
/* 立即改变 颜色,1秒后从左到右运动,运动结束后回到原位置 */
animation-fill-mode: backwards;
/* 立即改变颜色,1秒后从左到右运动,运动结束后保持在最后的位置 */
animation-fill-mode: both;