温馨提示×

温馨提示×

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

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

css3如何做加载动画

发布时间:2026-09-18 22:42:05 来源:亿速云 阅读:95 作者:小樊 栏目:编程语言

在 CSS3 中,加载动画通常通过 @keyframes + animation 来实现,也可以结合 transform、opacity 等属性。下面按常见类型给你几种实用做法。


一、基础:旋转加载圈(最常用)

HTML

<div class="loader"></div>

CSS

.loader {
  width: 40px;
  height: 40px;
  border: 4px solid #eee;
  border-top-color: #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

✅ 原理:

  • border-top-color 做高亮部分
  • rotate 持续旋转

二、圆点跳动加载(类似 iOS)

HTML

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

CSS

.dots {
  display: flex;
  gap: 6px;
}

.dots span {
  width: 10px;
  height: 10px;
  background: #333;
  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);
  }
  40% {
    transform: scale(1);
  }
}

三、进度条加载动画

HTML

<div class="bar"></div>

CSS

.bar {
  width: 100%;
  height: 4px;
  background: linear-gradient(90deg, #3498db, #2ecc71);
  background-size: 200% 100%;
  animation: loading 1.5s infinite;
}

@keyframes loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

四、脉冲加载(呼吸效果)

.pulse {
  width: 40px;
  height: 40px;
  background: #3498db;
  border-radius: 50%;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(0.8);
    opacity: 0.7;
  }
  100% {
    transform: scale(1.2);
    opacity: 0;
  }
}

五、常用动画属性说明

animation: name duration timing-function delay iteration-count direction;

示例:

animation: spin 1s linear infinite;
属性 说明
duration 动画时间
timing-function 缓动方式
iteration-count 次数(infinite 无限)
direction 方向(alternate 来回)

六、建议

  • 加载动画尽量用 transform / opacity(性能更好)
  • 可配合 will-change: transform
  • 加载完成后用 JS 移除或 display:none

如果你想要 特定风格(如:微信风格、Material、骨架屏、纯 CSS 无 JS),可以告诉我,我可以直接给你完整示例。

向AI问一下细节

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

AI
助
手