在 CSS3 中制作加载动画(Loading Animation)通常使用 @keyframes + animation + transform,常见形式有旋转、脉冲、点阵、条形等。下面从最常用到进阶给你几个典型示例。
<div class="spinner"></div>
.spinner {
width: 40px;
height: 40px;
border: 4px solid #eee;
border-top: 4px solid #409eff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
✅ 原理
border-radius: 50% 变成圆border-top 上色rotate() 无限旋转<div class="pulse"></div>
.pulse {
width: 40px;
height: 40px;
background: #409eff;
border-radius: 50%;
animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
0% {
transform: scale(0.6);
opacity: 0.6;
}
50% {
transform: scale(1);
opacity: 1;
}
100% {
transform: scale(0.6);
opacity: 0.6;
}
}
<div class="dots">
<span></span>
<span></span>
<span></span>
</div>
.dots {
display: flex;
gap: 6px;
}
.dots span {
width: 10px;
height: 10px;
background: #409eff;
border-radius: 50%;
animation: bounce 1.2s infinite ease-in-out;
}
.dots span:nth-child(2) {
animation-delay: 0.2s;
}
.dots span:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes bounce {
0%, 80%, 100% {
transform: scale(0.6);
opacity: 0.4;
}
40% {
transform: scale(1);
opacity: 1;
}
}
<div class="bars">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
.bars {
display: flex;
align-items: flex-end;
height: 30px;
gap: 4px;
}
.bars span {
width: 6px;
background: #409eff;
animation: stretch 1s infinite ease-in-out;
}
.bars span:nth-child(2) { animation-delay: 0.1s; }
.bars span:nth-child(3) { animation-delay: 0.2s; }
.bars span:nth-child(4) { animation-delay: 0.3s; }
.bars span:nth-child(5) { animation-delay: 0.4s; }
@keyframes stretch {
0%, 100% {
height: 10px;
}
50% {
height: 30px;
}
}
@keyframes定义动画的关键帧
@keyframes name {
from { ... }
to { ... }
}
animationanimation: 名称 时长 速度曲线 次数;
animation: spin 1s linear infinite;
常见属性:
animation-nameanimation-durationanimation-timing-functionanimation-iteration-counttransform: rotate()
transform: scale()
transform: translateY()
opacity、transform(性能更好)width/height如果你愿意,我可以帮你:
✅ 写一个 纯 CSS 的 Loading 组件
✅ 仿 Ant Design / Element Plus 的加载动画
✅ 做一个 SVG + CSS3 高级加载动画
你想做哪种?
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。