animation-iteration-count: infinite 用于让动画无限循环。需配合 @keyframes 定义动画帧,并通过 animation 属性应用到元素,如 .element { animation: example 2s infinite; }。示例中盒子每1.5秒左右移动并持续重复,常用于加载旋转、呼吸效果等UI动效。实际使用时应注意性能,避免大量复杂动画影响流畅度,可通过 Intersection Observer 在不可见时暂停以优化体验。
CSS 中的 animation-iteration-count: infinite 用于让动画无限次循环播放。它常配合 @keyframes 和其他动画属性一起使用,使元素的动画持续不断地运行。
常用写法如下:
.element {
animation-name: example;
animation-duration: 2s;
animation-iteration-count: infinite;
}
也可以简写在 animation 复合属性中:
.element {
animation: example 2s infinite;
}
示例:让一个 div 左右移动
@keyframes moveSide {
0% { transform: translateX(0); }
50% { transform: translateX(100px); }
100% { transform: translateX(0); }
}
.box {
width: 50px;
height: 50px;
background: blue
;
animation: moveSide 1.5s infinite;
}
这个盒子会每 1.5 秒完成一次左右移动,并且不断重复。
例如做一个旋转的加载图标:
.loader {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}