当前位置:首页 > VUE

vue实现环形进度组件

2026-01-20 02:49:41VUE

实现环形进度组件

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

定义组件模板

<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实现扇形进度

Vue 实现扇形进度条 使用 SVG 绘制扇形 SVG 的 path 元素可以用于绘制扇形。通过计算扇形的路径数据,结合 Vue 的动态数据绑定实现进度变化。 <template>…

vue实现进度审批

vue实现进度审批

Vue 实现进度审批功能 进度审批功能通常涉及表单提交、状态跟踪和交互反馈。以下是实现的基本思路和代码示例。 审批流程数据结构设计 使用对象数组存储审批步骤,每个步骤包含状态、审批人等信息:…

vue实现环形进度

vue实现环形进度

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

vue实现上传进度

vue实现上传进度

Vue 实现文件上传进度 在 Vue 中实现文件上传进度可以通过 XMLHttpRequest 或 axios 的 onUploadProgress 事件来监听上传进度。以下是两种常见实现方式: 使…

vue实现文件进度

vue实现文件进度

Vue 实现文件上传进度 使用 Vue 实现文件上传进度可以通过结合 axios 或原生 XMLHttpRequest 的进度事件来实现。以下是两种常见方法: 使用 axios 上传文件并显…

vue实现环形菜单

vue实现环形菜单

实现环形菜单的基本思路 环形菜单通常指围绕中心点呈圆形排列的菜单项,点击后可能有展开或选中效果。Vue实现的核心在于计算每个菜单项的位置和动态交互。 基础HTML结构 <template&…