当前位置:首页 > VUE

vue实现环形进度

2026-03-08 23:33:41VUE

Vue 环形进度条实现方法

使用 SVG 和 CSS 实现基础环形进度条

SVG 的 circle 元素可以轻松创建环形进度条。通过计算 stroke-dasharraystroke-dashoffset 属性来实现进度效果。

<template>
  <div class="circular-progress">
    <svg viewBox="0 0 100 100">
      <circle
        cx="50"
        cy="50"
        r="45"
        fill="none"
        stroke="#e6e6e6"
        stroke-width="10"
      />
      <circle
        cx="50"
        cy="50"
        r="45"
        fill="none"
        stroke="#4CAF50"
        stroke-width="10"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="dashOffset"
      />
    </svg>
    <div class="progress-text">{{ progress }}%</div>
  </div>
</template>

<script>
export default {
  props: {
    progress: {
      type: Number,
      default: 0,
      validator: value => value >= 0 && value <= 100
    }
  },
  computed: {
    circumference() {
      return 2 * Math.PI * 45;
    },
    dashOffset() {
      return this.circumference * (1 - this.progress / 100);
    }
  }
};
</script>

<style>
.circular-progress {
  position: relative;
  width: 100px;
  height: 100px;
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 20px;
}
</style>

使用第三方库 vue-circle-progress

对于更复杂的需求,可以使用专门为 Vue 设计的进度条库。

vue实现环形进度

安装依赖:

npm install vue-circle-progress

使用示例:

vue实现环形进度

<template>
  <circle-progress
    :percent="progress"
    :size="200"
    :border-width="15"
    :border-bg-width="15"
    fill-color="#4CAF50"
    empty-color="#e6e6e6"
  />
</template>

<script>
import CircleProgress from "vue-circle-progress";

export default {
  components: {
    CircleProgress
  },
  props: {
    progress: {
      type: Number,
      default: 0
    }
  }
};
</script>

动画效果实现

为进度条添加平滑的过渡动画,可以通过 CSS 过渡或 JavaScript 动画实现。

<template>
  <div class="circular-progress">
    <svg viewBox="0 0 100 100">
      <circle class="progress-bg" ... />
      <circle
        class="progress-bar"
        ...
        :style="{ 'stroke-dashoffset': dashOffset }"
      />
    </svg>
  </div>
</template>

<style>
.progress-bar {
  transition: stroke-dashoffset 0.5s ease-in-out;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
}
</style>

自定义环形进度条

通过 props 允许自定义颜色、大小等属性,使组件更灵活。

<template>
  <div
    class="circular-progress"
    :style="{ width: size + 'px', height: size + 'px' }"
  >
    <svg viewBox="0 0 100 100">
      <circle ... :stroke="backgroundColor" />
      <circle ... :stroke="progressColor" />
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    size: {
      type: Number,
      default: 100
    },
    backgroundColor: {
      type: String,
      default: '#e6e6e6'
    },
    progressColor: {
      type: String,
      default: '#4CAF50'
    }
  }
};
</script>

这些方法提供了从简单到复杂的环形进度条实现方案,可以根据项目需求选择合适的方案。SVG 方案提供了最大的灵活性,而第三方库则可以快速实现标准化的进度条效果。

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

相关文章

css环形图制作

css环形图制作

使用CSS制作环形图 CSS环形图可以通过多种方法实现,包括使用border-radius、conic-gradient以及SVG等技术。以下是几种常见的方法: 使用border-radius和伪元…

php如何实现下载进度

php如何实现下载进度

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

js实现环形

js实现环形

实现环形动画 使用CSS和JavaScript创建一个环形动画效果。通过CSS定义环形的基本样式,JavaScript控制动画的交互逻辑。 <!DOCTYPE html> <htm…

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…