用js+css实现圆环型的进度条——js+css基础积累

如果用js+css实现圆环型的进度条:

直接上代码:

js 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>环形进度条</title>
  </head>

  <body>
    <div class="percent">
      <svg style="transform: rotate(-90deg)">
        <circle cx="50" cy="50" r="50" />
        <circle id="circle" cx="50" cy="50" r="50" style="stroke: red" />
      </svg>
      <div class="num">
        <h2>
          <span id="per">0</span>
          <span>%</span>
        </h2>
      </div>
    </div>
    <div class="percent">
      <svg style="transform: rotate(-90deg)">
        <circle cx="50" cy="50" r="50" />
        <circle id="circle2" cx="50" cy="50" r="50" style="stroke: #f90" />
      </svg>
      <div class="num">
        <h2>
          <span id="per2">0</span>
          <span>%</span>
        </h2>
      </div>
    </div>
    <div class="percent">
      <svg style="transform: rotate(-90deg)">
        <circle cx="50" cy="50" r="50" />
        <circle id="circle3" cx="50" cy="50" r="50" style="stroke: #67c23a" />
      </svg>
      <div class="num">
        <h2>
          <span id="per3">0</span>
          <span>%</span>
        </h2>
      </div>
    </div>
  </body>
  <style>
    body {
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 0;
    }

    .percent {
      position: relative;
      width: 110px;
      height: 110px;
    }

    .percent svg {
      width: 110px;
      height: 110px;
    }

    .percent svg circle {
      fill: none;
      stroke-width: 10;
      stroke: #1258e3;
      transform: translate(5px, 5px);
      /* 设置虚线长度 = 圆的周长(2*50*3.1415926) */
      stroke-dasharray: 310;
      /* 设置圆的空白间隙 */
      stroke-dashoffset: 310;
    }

    .percent svg circle:nth-child(1) {
      stroke-dashoffset: 0;
      stroke: #f3f3f3;
    }

    .percent svg circle:nth-child(2) {
      stroke-dashoffset: calc(310 - 310 * (0 / 100));
      stroke-linecap: round;
    }

    .percent .num {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    #per {
      font-size: 22px;
    }
  </style>
  <script>
    circleRender(
      document.querySelector('#circle'),
      document.querySelector('#per'),
      50
    );
    circleRender(
      document.querySelector('#circle2'),
      document.querySelector('#per2'),
      10
    );
    circleRender(
      document.querySelector('#circle3'),
      document.querySelector('#per3'),
      95
    );
    function circleRender(circle, per, percent) {
      let num = 0;
      const timer = setInterval(() => {
        num += 1;
        if (num > percent) {
          num = percent;
          clearInterval(timer);
          blo = false;
        }
        // 设置进度
        circle.style.strokeDashoffset = `calc(310 - 310*(${num}/100))`;
        per.innerHTML = num;
      }, 100);
    }
  </script>
</html>
相关推荐
liu****2 小时前
1.模拟算法
开发语言·c++·算法·1024程序员节
数据村的古老师2 小时前
Python数据分析实战:基于25年黄金价格数据的特征提取与算法应用【数据集可下载】
开发语言·python·数据分析
孔明兴汉3 小时前
第一章-第三节-Java开发环境配置
java·开发语言
小王不爱笑1323 小时前
Java 核心知识点查漏补缺(一)
java·开发语言·python
空空kkk4 小时前
Java——类和对象
java·开发语言
沐知全栈开发5 小时前
Python3 集合
开发语言
Jonathan Star5 小时前
用Python轻松提取视频音频并去除静音片段
开发语言·python·音视频
程序猿阿伟6 小时前
《首屏加载优化手册:Vue3+Element Plus项目提速的技术细节》
前端·javascript·vue.js
Evand J6 小时前
【自适应粒子滤波MATLAB例程】Sage Husa自适应粒子滤波,用于克服初始Q和R不准确的问题,一维非线性滤波。附下载链接
开发语言·matlab·卡尔曼滤波·自适应滤波·非线性
尘觉6 小时前
面试-浅复制和深复制?怎样实现深复制详细解答
javascript·面试·职场和发展