loading css 制作
CSS 加载动画制作方法
CSS 加载动画可以通过关键帧动画(@keyframes)和 CSS 属性实现。以下是几种常见的加载动画实现方式。
旋转加载动画
使用 @keyframes 定义一个旋转动画,并将其应用到元素上:

.loader {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
点状加载动画
通过多个点的缩放动画实现加载效果:
.dot-loader {
display: flex;
justify-content: center;
gap: 8px;
}
.dot {
width: 10px;
height: 10px;
background-color: #3498db;
border-radius: 50%;
animation: bounce 1.4s infinite ease-in-out;
}
.dot:nth-child(2) {
animation-delay: 0.2s;
}
.dot:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes bounce {
0%, 100% { transform: scale(0); }
50% { transform: scale(1); }
}
进度条加载动画
使用 @keyframes 实现宽度变化的进度条:

.progress-loader {
width: 100%;
height: 4px;
background-color: #f3f3f3;
position: relative;
}
.progress-loader::before {
content: '';
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 0%;
background-color: #3498db;
animation: progress 2s linear infinite;
}
@keyframes progress {
0% { width: 0%; }
100% { width: 100%; }
}
波浪加载动画
通过伪元素和 @keyframes 实现波浪效果:
.wave-loader {
width: 50px;
height: 50px;
position: relative;
}
.wave-loader::before,
.wave-loader::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
border: 2px solid #3498db;
border-radius: 50%;
opacity: 0;
animation: wave 1.5s linear infinite;
}
.wave-loader::after {
animation-delay: 0.75s;
}
@keyframes wave {
0% {
transform: scale(0.5);
opacity: 0;
}
50% {
opacity: 0.5;
}
100% {
transform: scale(1.2);
opacity: 0;
}
}
优化加载动画性能
- 使用
transform和opacity代替width或height动画,以提高性能。 - 限制动画的复杂度和数量,避免过多消耗资源。
- 使用
will-change属性提前告知浏览器元素将发生动画变化。
.loader {
will-change: transform;
}
响应式加载动画
通过媒体查询调整加载动画的大小或速度:
@media (max-width: 768px) {
.loader {
width: 30px;
height: 30px;
border-width: 3px;
}
}
以上方法提供了多种 CSS 加载动画的实现方式,可根据需求选择或组合使用。






