您现在的位置是:网站首页 > 动画属性的完整配置文章详情
动画属性的完整配置
陈川
【
CSS
】
35370人已围观
3458字
动画属性的完整配置
CSS动画通过animation
属性实现,它是多个子属性的简写形式。完整的动画配置包含八个关键子属性,每个属性控制动画的不同方面。理解这些属性的具体用法和组合方式,能创建出更精细复杂的动效。
animation-name
定义关键帧规则的名称,需与@keyframes
配合使用。名称区分大小写,不能包含特殊字符(除连字符和下划线)。允许多个动画名称以逗号分隔,实现复合动画效果。
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(100px); }
}
@keyframes fade {
from { opacity: 1; }
to { opacity: 0.3; }
}
.box {
animation-name: slide, fade;
}
animation-duration
设置单次动画周期时长,单位为秒(s)或毫秒(ms)。默认值0表示无动画。多动画时用逗号分隔不同时长,数量需与animation-name
匹配。
.element {
animation-name: spin, color-change;
animation-duration: 2s, 500ms; /* 旋转动画2秒,颜色变化0.5秒 */
}
animation-timing-function
定义加速度曲线,内置值包括:
linear
匀速ease
缓入缓出(默认)ease-in
缓入ease-out
缓出ease-in-out
缓入缓出cubic-bezier(n,n,n,n)
自定义贝塞尔曲线steps(n, start|end)
步进动画
.moving {
animation-timing-function:
cubic-bezier(0.1, 0.7, 1.0, 0.1),
steps(5, end);
}
animation-delay
设置动画开始前的延迟时间,支持负值使动画从中途开始。多动画时分别设置延迟:
.notification {
animation-name: pop-up, vibrate;
animation-delay: 0.5s, 2s; /* 先延迟0.5秒弹出,再延迟2秒振动 */
}
animation-iteration-count
控制动画重复次数:
- 数字:具体次数(如2.5表示两次半)
infinite
:无限循环
.loader {
animation-iteration-count: infinite;
}
.progress {
animation-iteration-count: 1; /* 默认值 */
}
animation-direction
定义动画播放方向:
normal
正向播放(默认)reverse
反向播放alternate
正反交替alternate-reverse
反反正交替
.pendulum {
animation-direction: alternate;
}
animation-fill-mode
设置动画执行前后如何应用样式:
none
不应用任何样式(默认)forwards
保留最后一帧样式backwards
应用第一帧样式(含delay期间)both
同时应用前后样式
.dialog {
animation-fill-mode: both;
}
animation-play-state
控制动画运行状态:
running
运行中(默认)paused
暂停状态
可通过JavaScript动态修改实现交互控制:
document.querySelector('.player').style.animationPlayState = 'paused';
复合写法
完整属性的简写顺序为:
name duration timing-function delay iteration-count direction fill-mode play-state
.card {
animation:
slide-in 0.8s ease-out 0.2s 1 normal forwards,
glow 2s linear 1s infinite alternate;
}
关键帧规则
@keyframes
定义动画序列,百分比或from/to
指定阶段:
@keyframes complex-animation {
0% { transform: scale(1); }
20% { opacity: 0.8; }
50% { transform: scale(1.2) rotate(10deg); }
100% {
transform: scale(1) rotate(0);
background: #ff0000;
}
}
性能优化建议
- 优先使用
transform
和opacity
属性,它们能触发GPU加速 - 避免在滚动事件中触发动画
- 对复杂动画使用
will-change
属性预声明 - 减少同时运行的动画数量
.optimized {
will-change: transform, opacity;
animation: smooth-move 1s ease;
}
多动画协同
通过调整各子属性实现动画序列化:
.sequence {
animation:
fade-in 0.4s ease-in,
expand 0.6s 0.4s ease-out,
stabilize 0.2s 1s linear forwards;
}
浏览器前缀处理
考虑兼容性时应添加前缀:
@-webkit-keyframes example { /* Safari/Chrome */ }
@-moz-keyframes example { /* Firefox */ }
@-o-keyframes example { /* Opera */ }
调试技巧
使用开发者工具的动画面板:
- 暂停/重放特定动画
- 修改贝塞尔曲线实时预览
- 显示动画重绘区域
- 测量帧率变化
动态控制示例
通过JavaScript与CSS变量结合实现参数动态化:
<style>
.dynamic {
animation: var(--anim-name) var(--anim-dur) ease-in-out;
}
</style>
<script>
element.style.setProperty('--anim-dur', `${speed}ms`);
</script>
高级技巧:动画遮罩
结合mask-image
创建特殊效果:
.reveal {
animation: reveal 3s;
mask-image: linear-gradient(90deg, #000 0%, transparent 100%);
}
@keyframes reveal {
from { mask-position: 100%; }
to { mask-position: 0%; }
}