当前位置:首页 > VUE

vue实现环形进度组件

2026-01-20 02:49:41VUE

实现环形进度组件

环形进度组件可以通过SVG或Canvas实现,以下是基于SVG的实现方法:

vue实现环形进度组件

定义组件模板

vue实现环形进度组件

<template>
  <div class="circle-progress">
    <svg :width="size" :height="size" viewBox="0 0 100 100">
      <circle 
        cx="50" 
        cy="50" 
        :r="radius" 
        fill="none" 
        :stroke="backgroundColor" 
        :stroke-width="strokeWidth"
      />
      <circle 
        cx="50" 
        cy="50" 
        :r="radius" 
        fill="none" 
        :stroke="progressColor" 
        :stroke-width="strokeWidth"
        :stroke-dasharray="dashArray"
        :stroke-dashoffset="dashOffset"
        stroke-linecap="round"
      />
      <text 
        x="50" 
        y="50" 
        text-anchor="middle" 
        dominant-baseline="middle"
        :fill="textColor"
      >
        {{ progress }}%
      </text>
    </svg>
  </div>
</template>

定义组件逻辑

<script>
export default {
  props: {
    progress: {
      type: Number,
      default: 0,
      validator: value => value >= 0 && value <= 100
    },
    size: {
      type: Number,
      default: 200
    },
    strokeWidth: {
      type: Number,
      default: 10
    },
    backgroundColor: {
      type: String,
      default: '#eee'
    },
    progressColor: {
      type: String,
      default: '#42b983'
    },
    textColor: {
      type: String,
      default: '#333'
    }
  },
  computed: {
    radius() {
      return 50 - this.strokeWidth / 2
    },
    circumference() {
      return 2 * Math.PI * this.radius
    },
    dashArray() {
      return this.circumference
    },
    dashOffset() {
      return this.circumference * (1 - this.progress / 100)
    }
  }
}
</script>

添加样式

<style scoped>
.circle-progress {
  display: inline-block;
  position: relative;
}

.circle-progress circle {
  transition: stroke-dashoffset 0.5s ease;
}
</style>

使用组件示例

<template>
  <div>
    <circle-progress :progress="75" />
  </div>
</template>

<script>
import CircleProgress from './CircleProgress.vue'

export default {
  components: {
    CircleProgress
  }
}
</script>

自定义选项说明

  1. progress:进度值,范围0-100
  2. size:组件大小,单位为px
  3. strokeWidth:圆环宽度
  4. backgroundColor:背景圆环颜色
  5. progressColor:进度圆环颜色
  6. textColor:文本颜色

动画效果

组件已经内置了过渡动画,当progress值变化时会有平滑的动画效果。可以通过调整CSS中的transition属性来修改动画时长和缓动函数。

标签: 环形进度
分享给朋友:

相关文章

vue实现环形进度

vue实现环形进度

Vue 实现环形进度条的方法 使用 SVG 和 CSS 实现 在 Vue 中创建一个环形进度条可以通过 SVG 和 CSS 结合实现。以下是一个简单的实现方式: <template>…

php实现快递进度

php实现快递进度

实现快递进度查询的PHP方法 使用快递鸟API 快递鸟提供物流查询API接口,支持多家快递公司。需注册账号获取API Key和商户ID。 // 配置参数 $appKey = '你的AppKey';…

php如何实现下载进度

php如何实现下载进度

在PHP中实现下载进度显示需要结合服务器端和客户端的协作,以下是几种常见方法: 使用输出缓冲和分块传输 通过分块发送文件数据并计算进度: $file = 'large_file.zip';…

css环形图制作

css环形图制作

使用 CSS 和 SVG 制作环形图 CSS 结合 SVG 可以轻松实现环形图效果。以下是一个简单的方法: <svg viewBox="0 0 100 100" class="donut"&g…

php如何实现下载进度

php如何实现下载进度

使用 cURL 获取下载进度 在 PHP 中,可以通过 cURL 的 CURLOPT_PROGRESSFUNCTION 和 CURLOPT_NOPROGRESS 选项来实现下载进度跟踪。需要启用进度回…

php实现环形链表

php实现环形链表

环形链表的实现原理 环形链表是一种特殊的链表数据结构,其最后一个节点的指针不是指向null,而是指向链表中的某个节点(通常是头节点),形成一个闭环。这种结构常用于实现循环队列、轮询调度等场景。 PH…