css如何制作加载
使用CSS制作加载动画
纯CSS旋转动画
通过@keyframes和transform属性创建一个简单的旋转加载动画:
.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: 12px;
height: 12px;
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: translateY(0); }
50% { transform: translateY(-15px); }
}
进度条加载动画
创建一个从左到右填充的进度条效果:
.progress-loader {
width: 100%;
height: 4px;
background-color: #f3f3f3;
position: relative;
overflow: hidden;
}
.progress-loader::before {
content: "";
position: absolute;
left: -50%;
height: 100%;
width: 50%;
background-color: #3498db;
animation: loading 2s linear infinite;
}
@keyframes loading {
from { left: -50%; }
to { left: 100%; }
}
骨架屏加载效果
为内容未加载时创建占位效果:
.skeleton {
background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
}
@keyframes shimmer {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
波浪形加载动画
创建类似波浪起伏的效果:
.wave-loader {
display: flex;
justify-content: center;
gap: 6px;
height: 40px;
}
.wave {
width: 6px;
height: 100%;
background: linear-gradient(to top, #3498db, #2980b9);
animation: wave 1s ease-in-out infinite;
}
.wave:nth-child(2) {
animation-delay: 0.1s;
}
.wave:nth-child(3) {
animation-delay: 0.2s;
}
.wave:nth-child(4) {
animation-delay: 0.3s;
}
@keyframes wave {
0%, 100% { height: 40%; }
50% { height: 100%; }
}
这些CSS加载动画可以根据实际需求调整颜色、尺寸和动画时长。通过组合不同的动画属性和关键帧,可以创建各种视觉效果来指示加载状态。







