使用CSS+SVG实现加载动画

使用CSS+SVG实现加载动画

效果展示

CSS知识点

  • SVG元素使用
  • SVG相关CSS属性运用

整体页面布局

html 复制代码
<section>
 <div class="box">
    <div class="loader">
      <svg>
        <circle cx="40" cy="40" r="40"></circle>
      </svg>
    </div>
  </div>
  <div class="box">
    <div class="loader">
      <svg>
        <circle cx="40" cy="40" r="40"></circle>
      </svg>
    </div>
  </div>
  <div class="box">
    <div class="loader">
      <svg>
        <circle cx="40" cy="40" r="40"></circle>
      </svg>
    </div>
  </div>
</section>

说明:这里定义了三个 box 元素,其中两个 box 主要是实现阴影效果。

定义场景样式和相关动画

css 复制代码
section {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #072631;
  animation: animateBg 10s linear infinite;
}
/* 背景颜色改变动画 */
@keyframes animateBg {
  0% {
    filter: hue-rotate(0deg);
  }
  100% {
    filter: hue-rotate(360deg);
  }
}

定义 svgcircle 元素样式

css 复制代码
svg {
  position: relative;
  width: 90px;
  height: 90px;
  z-index: 1000;
}
svg circle {
  width: 100%;
  height: 100%;
  fill: none;
  stroke-width: 10;
  stroke: #25e6ff;
  stroke-linecap: round;
  transform: translate(5px, 5px);
  stroke-dasharray: 250;
  stroke-dashoffset: 249;
}

完成上述代码后效果如下:

svgcircle 元素添加动画

css 复制代码
svg {
  animation: fixAnimation 2s ease-in-out infinite;
}
/* SVG元素主要是负责 */
@keyframes fixAnimation {
  0% {
    transform: rotateY(0deg);
  }
  50% {
    transform: rotateY(0deg);
  }
  50.00000001%,
  100% {
    transform: rotateY(181deg);
  }
}
svg circle {
  animation: animate 2s ease-in-out infinite;
}
/* 实现加载动画,动画实现的时候开始和结束进度设置为对称,这样动画执行的时候会比较平滑 */
@keyframes animate {
  0%,
  2% {
    stroke-dashoffset: 249;
  }
  50% {
    stroke-dashoffset: 125;
  }
  99%,
  100% {
    stroke-dashoffset: 249;
  }
}

实现上述效果后如下:

对三个 box 元素进行样式处理

css 复制代码
.box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 150px;
  display: flex;
  justify-content: center;
  /* 倒影效果 */
  -webkit-box-reflect: below -80px linear-gradient(transparent, #0004);
}
.box:nth-child(2) {
  filter: blur(10px);
}

.box:nth-child(3) {
  filter: blur(20px);
}

实现上述效果后,基本可以考到最终效果动画,但是后续还有一些动画需要处理。

加载动画进行旋转

css 复制代码
.loader {
  animation: positionX 2s linear infinite;
}

@keyframes positionX {
  0% {
    transform: rotate(180deg) translateX(-40px);
  }
  100% {
    transform: rotate(180deg) translateX(40px);
  }
}

完整代码下载

完整代码下载

相关推荐
rising start17 分钟前
前端基础一、HTML5
前端·html·html5
鬼谷中妖26 分钟前
JavaScript 循环与对象:深入理解 for、for...in、for...of、不可枚举属性与可迭代对象
前端
大厂码农老A31 分钟前
你打的日志,正在拖垮你的系统:从P4小白到P7专家都是怎么打日志的?
java·前端·后端
im_AMBER32 分钟前
CSS 01【基础语法学习】
前端·css·笔记·学习
DokiDoki之父36 分钟前
前端速通—CSS篇
前端·css
pixle039 分钟前
Web大屏适配终极方案:vw/vh + flex + clamp() 完美组合
前端·大屏适配·vw/vh·clamp·终极方案·web大屏
ssf19871 小时前
前后端分离项目前端页面开发远程调试代理解决跨域问题方法
前端
@PHARAOH1 小时前
WHAT - 前端性能指标(加载性能指标)
前端
尘世中一位迷途小书童1 小时前
🎨 SCSS 高级用法完全指南:从入门到精通
前端·css·开源
非凡ghost1 小时前
火狐浏览器(Firefox)tete009 Firefox 多语便携版
前端·firefox