JS环形进度条

HTML

<div class="container">
    <svg width="200" height="200" viewBox="0 0 200 200">
      <circle cx="100" cy="100" r="50" fill="none" stroke-width="10" stroke="gray" />
      <circle id="circle" stroke-linecap="round"  cx="100" cy="100" r="50" fill="none" stroke-width="10" stroke="yellowgreen" stroke-dasharray="0 314" transform="rotate(-90, 100, 100)"/>
  
      <text x="100" y="100" fill="#6b778c" text-anchor="middle" dominant-baseline="central">
        <tspan id="percent">0</tspan>%
      </text> 
    </svg>
    <div>
    <button onclick="add()">add</button>
    <button onclick="minus()">minus</button>
    </div>
  </div> 

CSS

.container {
      margin: 0 auto;
      width: 50%;
      text-align: center;
    }	

    #circle{
	    transition: all .5s;
    }

JavaScript

let circle = document.getElementById('circle');
  let percent = document.getElementById('percent');

  let radius = 50;
  let v = 0.5;

  function minus() {
    v = v - 0.01
    if (v <= 0) {
      v = 0;
    }
    set(v);
  }
  function add() {
    v = v + 0.01;
    if (v >= 1) {
      v = 1;
    }
    set(v);
  }

  function set(p) {
    let total = Math.PI * 2 * radius;
    percent.innerHTML = Math.round(p * 100);
    let str = `${p * total} ${total * (1-p)}`;
    console.log('stroke-dasharray:', str);
    circle.style['stroke-dasharray'] = str;
  }

  setTimeout(() => {
    set(v)
  }, 1000);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值