温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS3如何制作加载动画

发布时间:2026-08-30 19:21:05 来源:亿速云 阅读:95 作者:小樊 栏目:编程语言

在 CSS3 中制作加载动画(Loading Animation)通常使用 @keyframes + animation + transform,常见形式有旋转、脉冲、点阵、条形等。下面从最常用到进阶给你几个典型示例。


一、最常见:旋转圆环加载(Spinner)

HTML

<div class="spinner"></div>

CSS

.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() 无限旋转

二、脉冲圆点(常见 App 加载)

HTML

<div class="pulse"></div>

CSS

.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;
  }
}

三、三点跳跃加载(微信/微博常见)

HTML

<div class="dots">
  <span></span>
  <span></span>
  <span></span>
</div>

CSS

.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;
  }
}

四、条形加载动画(类似音频频谱)

HTML

<div class="bars">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>

CSS

.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;
  }
}

五、关键知识点总结

1️⃣ @keyframes

定义动画的关键帧

@keyframes name {
  from { ... }
  to { ... }
}

2️⃣ animation

animation: 名称 时长 速度曲线 次数;
animation: spin 1s linear infinite;

常见属性:

  • animation-name
  • animation-duration
  • animation-timing-function
  • animation-iteration-count

3️⃣ 常用变换

transform: rotate()
transform: scale()
transform: translateY()

六、React / Vue 中使用建议

  • 直接封装成组件
  • opacitytransform(性能更好)
  • 避免频繁操作 width/height

如果你愿意,我可以帮你: ✅ 写一个 纯 CSS 的 Loading 组件
✅ 仿 Ant Design / Element Plus 的加载动画
✅ 做一个 SVG + CSS3 高级加载动画

你想做哪种?

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI