css如何制作加载
使用纯CSS制作加载动画
通过CSS的@keyframes和animation属性可以创建平滑的加载动画效果。以下是几种常见的实现方式:
旋转加载动画

.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-loader div {
width: 12px;
height: 12px;
background-color: #3498db;
border-radius: 50%;
animation: bounce 1.4s infinite ease-in-out;
}
.dot-loader div:nth-child(2) {
animation-delay: 0.2s;
}
.dot-loader div:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes bounce {
0%, 80%, 100% { transform: scale(0); }
40% { transform: scale(1); }
}
使用CSS伪元素增强效果
可以通过:before和:after伪元素创建更复杂的加载动画:

.double-spinner {
position: relative;
width: 40px;
height: 40px;
}
.double-spinner:before,
.double-spinner:after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
opacity: 0.6;
}
.double-spinner:before {
background-color: #3498db;
animation: scaleout 2s infinite ease-in-out;
}
.double-spinner:after {
background-color: #e74c3c;
animation: scaleout 2s infinite ease-in-out 1s;
}
@keyframes scaleout {
0% { transform: scale(0); }
100% { transform: scale(1); opacity: 0; }
}
进度条式加载动画
线性进度条适合显示加载进度:
.progress-bar {
width: 100%;
height: 4px;
background-color: #f3f3f3;
position: relative;
overflow: hidden;
}
.progress-bar:after {
content: "";
position: absolute;
height: 100%;
width: 30%;
background-color: #2ecc71;
animation: progress 2s linear infinite;
}
@keyframes progress {
0% { left: -30%; }
100% { left: 100%; }
}
响应式加载动画设计
为确保加载动画在不同设备上表现良好,可以添加响应式设计:
@media (max-width: 768px) {
.loader {
width: 30px;
height: 30px;
border-width: 3px;
}
.dot-loader div {
width: 8px;
height: 8px;
}
}
性能优化建议
- 优先使用
transform和opacity属性制作动画,这些属性不会触发重排 - 避免过多使用
box-shadow等耗性能的属性 - 对动画元素使用
will-change: transform提示浏览器优化 - 限制同时运行的动画数量
这些CSS技术可以组合使用,创建各种自定义加载效果,无需JavaScript即可实现流畅的视觉反馈。






