vue中封装一个环形进度条组件,根据外部盒子大小自适应变化

不用插件在vue中封装一个环形进度条组件,根据外部盒子大小自适应变化,一开始用的饿了么的组件,发现大小不会自适应,然后自己就封装了一个

直接上代码

复制代码
<template>
  <div class="circle-progress" :style="wrapperStyle">
    <svg class="circle-progress__svg" viewBox="0 0 100 100">
      <!-- 外圈光晕环 -->
      <circle
        class="circle-progress__ring--outer"
        cx="50"
        cy="50"
        r="46"
        :stroke="progressColor"
      />
      <!-- 背景环 -->
      <circle
        class="circle-progress__bg"
        cx="50"
        cy="50"
        r="38"
        :stroke="bgColor"
      />
      <!-- 进度环 -->
      <circle
        class="circle-progress__progress"
        cx="50"
        cy="50"
        r="38"
        :stroke="progressColor"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="offset"
        stroke-linecap="round"
      />
    </svg>
    <div class="circle-progress__text" :style="textStyle">
      {{ percent }}%
    </div>
  </div>
</template>

<script>
export default {
  name: 'CircleProgress',
  props: {
    percent: {
      type: Number,
      default: 0,
      validator(val) {
        return val >= 0 && val <= 100
      }
    },
    progressColor: {
      type: String,
      default: '#0ff'
    },
    bgColor: {
      type: String,
      default: 'rgba(0, 255, 255, 0.2)'
    },
    textColor: {
      type: String,
      default: '#0ff'
    }
  },
  computed: {
    circumference() {
      return 2 * Math.PI * 38
    },
    offset() {
      const progress = this.percent / 100
      return this.circumference * (1 - progress)
    },
    wrapperStyle() {
      return {
        width: '100%',
        height: '100%'
      }
    },
    textStyle() {
      return {
        color: this.textColor,
        textShadow: `0 0 6px ${this.progressColor}, 0 0 12px ${this.progressColor}`
      }
    }
  }
}
</script>

<style scoped>
.circle-progress {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.circle-progress__svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.circle-progress__ring--outer {
  fill: none;
  stroke-width: 2;
  opacity: 0.8;
  filter: drop-shadow(0 0 4px currentColor);
}

.circle-progress__bg {
  fill: none;
  stroke-width: 8;
}

.circle-progress__progress {
  fill: none;
  stroke-width: 8;
  transition: stroke-dashoffset 0.4s ease;
  filter: drop-shadow(0 0 4px currentColor);
}

.circle-progress__text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 18px;
  font-weight: bold;
  font-family: sans-serif;
}
</style>

使用的时候也非常简单,直接父组件中引入就行了

然后传入需要的参数

<CircleProgress

:percent="83.4"

progress-color="#0ff"

bg-color="rgba(0,255,255,0.2)"

text-color="#0ff"

/>

相关推荐
一拳不是超人23 分钟前
DeepSeek Harness 为什么敢说"一切皆插件"?拆透 Cordis 引擎的五大核心机制
前端·人工智能·agent
IT_陈寒35 分钟前
Python的GIL让我多写了500行代码
前端·人工智能·后端
风骏时光牛马35 分钟前
AI智能体能力调度微服务
前端
大龄码农有梦想2 小时前
Vue + BPMN.js + Camunda:搭建企业级流程设计器完整实践
javascript·vue.js·流程设计器·bpmn.js·bpmn·流程审批·工作流设计器
Eloudy2 小时前
ReAct 原理简介
前端·javascript·人工智能·react.js·agent·gpu
ZJU_统一阿萨姆3 小时前
【DSH】如何将 DeepSeek Harness 嵌入生产环境?万字解构 DeepSeek Agent Harness 的运行机制与安全边界
前端·javascript·安全
用户921080262863 小时前
4. 前端地图大量点位实时更新优化:后端推增量,前端做 diff
前端
SoaringHeart4 小时前
Flutter最佳实践:Web项目中文字体首次加载乱码问题解决
前端·flutter
西安小哥4 小时前
破局与重生:大厂前端如何借力 AI 转型“超级全栈“
前端·人工智能
sunly_5 小时前
TypeScript总结:16、面向对象速查
前端·javascript·typescript