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"

/>

相关推荐
无心使然1 小时前
Openlayers调用ArcGis影像服务之一动态地图、地图切片(/exportImage)
前端·javascript·数据可视化
唯火锅不可辜负1 小时前
uniapp开发公众号订阅功能踩坑小记
前端·vue.js
opteOG2 小时前
游览器跨域问题详解
前端
SameX2 小时前
后台 GPS 记录从半天掉电 30% 到全天 8%,我的三版方案演进
前端
Cder2 小时前
用 React + Ink 在终端里「优雅搜索」:开源 CLI 设计与非交互模式实践
前端·agent
像我这样帅的人丶你还2 小时前
前端监控体系与实践(二):全局监控
前端·javascript·vue.js
颜酱2 小时前
LLM为核,上下文为限:拆解AI Agent生态的底层逻辑
前端·人工智能
前端那点事2 小时前
Vue3 超全复盘!30+前端高频核心知识点(开发+面试全覆盖)
前端·vue.js
顾随2 小时前
(二)kettle--输入与输出
javascript·数据库·kettle