css loading制作
CSS加载动画制作方法
使用纯CSS创建旋转加载动画
通过@keyframes定义旋转动画,结合border属性实现经典旋转效果:
.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: #333;
border-radius: 50%;
animation: bounce 1s infinite alternate;
}
.dot-loader div:nth-child(2) {
animation-delay: 0.2s;
}
.dot-loader div:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes bounce {
to { opacity: 0.3; transform: translateY(-10px); }
}
进度条式加载动画 通过改变伪元素的宽度模拟进度加载:
.progress-loader {
width: 100%;
height: 4px;
background: #eee;
position: relative;
overflow: hidden;
}
.progress-loader::after {
content: '';
position: absolute;
left: 0;
top: 0;
width: 30%;
height: 100%;
background: #4285f4;
animation: progress 2s ease-in-out infinite;
}
@keyframes progress {
50% { width: 70%; }
100% { width: 30%; left: 100%; }
}
高级技巧:SVG与CSS结合 使用SVG路径动画实现复杂加载效果:
<svg class="svg-loader" viewBox="0 0 50 50">
<path d="M25,5 A20,20 0 1,1 5,25" fill="none" stroke="#3498db" stroke-width="3"/>
</svg>
.svg-loader {
width: 50px;
height: 50px;
animation: rotate 1.5s linear infinite;
}
@keyframes rotate {
100% { transform: rotate(360deg); }
}
响应式加载动画设计 添加媒体查询确保不同设备上的显示效果:
@media (max-width: 600px) {
.loader {
width: 30px;
height: 30px;
border-width: 3px;
}
.dot-loader div {
width: 8px;
height: 8px;
}
}
这些方法可根据实际需求组合使用或修改参数,关键是通过@keyframes控制动画节奏,利用transform和opacity等属性创造平滑过渡效果。对于性能优化,建议优先使用CSS硬件加速属性如transform和opacity。






