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"

/>

相关推荐
光影少年3 分钟前
RN 路由栈管理、页面销毁、返回拦截
javascript·react native·react.js
_lucas7 分钟前
做了一个glsl在线调试工具
前端·javascript·three.js
To_OC8 小时前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
腻害兔8 小时前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 商城模块:从商品管理到交易引擎,50 张表撑起一整套电商系统
java·大数据·vue.js·产品经理·ai编程
不好听6138 小时前
从一行 JSX 到屏幕像素:前端开发者必须懂的浏览器渲染管线
前端
恒拓高科WorkPlus8 小时前
企业级内网即时通讯建设模型:BeeWorks内网IM四层架构
前端
前端小李子9 小时前
前端环境变量裸奔?我用 EnvShield 给它穿了件防弹衣
前端
youtootech9 小时前
HarmonyOS《柚兔学伴》项目实战25-我的页面、Web 嵌入与项目总结
前端·华为·harmonyos
小林ixn10 小时前
从零到一理解 React 父子组件通信:手写一个 Todo 应用带你彻底搞懂单向数据流
前端·javascript·react.js
醇氧11 小时前
CountDownLatch / CyclicBarrier / Semaphore 面试高频问答清单
前端·面试·职场和发展