svg圆弧进度条(窃)

<style>
  body {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
    background-color: #f5f5f5;
  }

  .dashboard {
    position: relative;
    width: 200px;
    height: 200px;
  background-size: 100% 100%;
  }

  .circle-background {
    fill: none;           /* 不填充 */
    stroke: #fff;          /* 圆环的颜色 */
    stroke-width: 10;      /* 圆环的宽度 */
  stroke-dasharray: 200, 52; /* 圆环断开部分的长度,总长度为周长 */
  stroke-dashoffset: 163;
  stroke-linecap: round;
  border-radius: 10;
  transition: all 1s; /* 过渡效果时间 */
  }

  .circle-progress {
    fill: none;           /* 不填充 */
    stroke: url(#gradient);       /* 圆环的颜色 */
    stroke-width: 10;      /* 圆环的宽度 */
    stroke-dasharray: 252, 0; /* 圆环断开部分的长度,总长度为周长 */
    stroke-dashoffset: 163;
    stroke-linecap: round; /* 圆滑断点 */
    transition: all 1s; /* 过渡效果时间 */
  }

  .percentage {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 24px;
    color: #3498db;
  }
</style>
</head>
<body>

<svg class="dashboard" viewBox="0 0 100 100">
<!-- 定义渐变色 -->
  <defs>
   <linearGradient id="gradient" gradientUnits="userSpaceOnUse" x1="50" y1="0" x2="50" y2="100%">
          <stop offset="0%" style="stop-color: rgba(111, 232, 191, 1)" />
          <stop offset="33%" style="stop-color: rgba(255, 175, 19, 1)" />
          <stop offset="70%" style="stop-color: rgba(222, 19, 80, 1)" />
          <stop offset="100%" style="stop-color: rgba(133, 14, 205, 1)" />
        </linearGradient>
  </defs>

<!-- 背景圆环 -->
<circle class="circle-background" cx="50" cy="50" r="40"></circle>

<!-- 进度圆环 -->
<circle class="circle-progress" cx="50" cy="50" r="40"></circle>

</svg>

<!-- 进度百分比显示 -->
<div class="percentage" id="percentage">0%</div>

<script>
function setProgress(percentage) {
  const circleProgress = document.querySelector('.circle-progress');
const circleBackground = document.querySelector('.circle-background');
  const percentageText = document.getElementById('percentage');

  const circumference = 2 * Math.PI * 40; // 圆的周长
const circumNewLength = (percentage / 100) * (circumference - 52);
  const dashOffset = 163 - circumNewLength;


  // 设置进度圆环的样式
  circleBackground.style.strokeDashoffset = dashOffset;
circleBackground.style.strokeDasharray = `${200 - circumNewLength}, ${ 52 + circumNewLength }`
circleProgress.style.strokeDasharray = `${circumNewLength}, ${ circumference - circumNewLength }`
  
  // 更新百分比文本
  percentageText.textContent = `${percentage}%`;
}

// 设置初始进度为0%
setProgress(0);

// 模拟过渡效果,从0%到50%
setTimeout(() => {
  setProgress(90);
}, 1000); // 过渡时间为1秒,你可以根据需要调整这个值
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值