原生svg 绘制进度条

效果图

在这里插入图片描述

本次进度条运用到svg 和 circle标签 对此方面不太熟悉的小伙伴可以收藏记录 防止用的时候找不到 因多为标签运用及css 少量js代码 方便理解每个参数的作用 注释都在代码中

HTML

<div>
  <svg width='200' height='200' viewBox="0 0 60 60" style="width:200px;height:200px;">
      <circle cx="30" cy="30" r="27"    fill="transparent" stroke-width="4" stroke="#eee" />
      <circle class="progress"  r="27" cy="30" cx="30" stroke-width="4" stroke="blue" 
      stroke-linejoin="round" stroke-linecap="round" fill="none" stroke-dashoffset="0px"  
      stroke-dasharray="169px" />
      /*
      	第一个circle是一个底色圆环 为了进度条不到100%时显示底色
      	第二个circle是真正的进度条 r cy这些常见的参数就不说了  stroke-width 圆环宽度
      	stroke 圆环颜色  stroke-linejoin  stroke-linecap 起始点和结束点的过度 目前是圆角过渡
      	fill 填充色 
      	stroke-dashoffset 起始点 stroke-dasharray 起始点到结束点的圆环周长 目前169px 为整个园 
      	 周长=派(3.1415926)*r(半径)的平方
      */
  </svg>
</div>

<style>
   svg {
       transform: rotate(-90deg);
   }
   .progress {
       animation: rotate 1s linear both;
       /*infinite : 循环执行   both:执行一次动画*/
   }
   @keyframes rotate {
       from {
           stroke-dashoffset: 169px;
       }
       to {
           stroke-dashoffset: 0px;
       }
   }
</style>

数据可控圆环进度

在这里插入图片描述

js控制显示进度

<div>
    <svg width='200' height='200' viewBox="0 0 60 60" style="width:200px;height:200px;">
        <circle cx="30" cy="30" r="27"    fill="transparent" stroke-width="4" stroke="#eee" />
        <circle class="progress" id="progress-bar" r="27" cy="30" cx="30" stroke-width="4" 					    stroke="blue" stroke-linejoin="round" 
        stroke-linecap="round" fill="none" stroke-dashoffset="0px"  stroke-dasharray="169px" />
    </svg>
    <span id="progress-detail">20%</span>
    <button onclick="reduce()">减少</button>
    <button onclick="add()">增加</button>
</div>


<script>
//上面已经知道圆环的控制参数了 我们根据周长计算出没1%的长度改变dom参数即可
    const bar = document.getElementById('progress-bar')
    const detail = document.getElementById('progress-detail')
    const total = 169 // 圆周长
    const per = total / 100 //一个百分比进度代表的周长 
    let progress = 20 // 当前百分比进度
    update(); //初始值渲染

    function add() {
        if (progress >= 100) {
            return
        }
        progress += 20
        update()
    }

    function reduce() {
        if (progress <= 0) {
            return
        }
        progress -= 20
        update()
    }

    function update() {
        bar.style.strokeDashoffset = (total - per * progress);//控制圆环周长
        detail.innerHTML = `${progress}%`
    }
</script>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要创建一个SVG圆环进度条,可以使用<path>元素和stroke-dasharray属性来定义圆弧的虚线样式。以下是一个简单的示例代码,展示了如何创建一个50%的圆环进度条: ```html <svg width="100" height="100"> <path d="M 50,50 m -50,0 a 50,50 0 1,0 100,0 a 50,50 0 1,0 -100,0" fill="none" stroke="black" stroke-width="10" stroke-dasharray="157 157" stroke-linecap="round" /> </svg> ``` 在这个示例中,我们使用了两个相同的圆弧路径来创建一个完整的圆环。首先,我们使用圆心为(50,50)、半径为50的圆弧路径来绘制圆环的上半部分。然后,我们再次使用相同的路径来绘制圆环的下半部分,但是需要将圆弧的方向改为逆时针方向。 在路径的d属性中,我们使用了小写字母a来绘制圆弧,同时将起点和终点坐标都设置为(50,50)。这里的圆弧半径也是50,所以路径的长度应该是圆的周长,因此我们需要使用两个圆弧路径来绘制完整的圆环。 接下来,我们使用stroke-dasharray属性来定义虚线样式。这里我们将其设置为"157 157",表示第一个线段占总路径长度的50%,第二个线段占总路径长度的另外50%。由于圆弧的周长是π×半径×2,所以这里的总路径长度为157(即π×50×2)。 最后,我们使用stroke-linecap属性来定义线段的端点样式,这里我们将其设置为"round",表示使用圆形端点样式。 上述代码将创建一个50%的圆环进度条,其中圆环的线宽为10px,线段长度为157px,使用圆形端点样式。你可以根据自己的需要来调整这些属性,创建不同样式的圆环进度条
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值