当前位置:首页 > VUE

vue实现圆环进度

2026-03-09 08:51:02VUE

Vue 实现圆环进度的方法

使用 SVG 实现圆环进度

SVG 的 circlepath 元素可以轻松绘制圆环进度条。通过动态计算 stroke-dasharraystroke-dashoffset 实现进度效果。

<template>
  <div class="circle-progress">
    <svg width="200" height="200" viewBox="0 0 200 200">
      <circle
        cx="100"
        cy="100"
        r="80"
        fill="none"
        stroke="#e6e6e6"
        stroke-width="10"
      />
      <circle
        cx="100"
        cy="100"
        r="80"
        fill="none"
        stroke="#42b983"
        stroke-width="10"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="offset"
      />
    </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 * 80;
    },
    offset() {
      return this.circumference * (1 - this.progress / 100);
    }
  }
};
</script>

<style>
.circle-progress {
  position: relative;
  width: 200px;
  height: 200px;
}
.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
}
</style>

使用 Canvas 实现圆环进度

Canvas 提供了更灵活的绘图方式,适合需要复杂动画效果的场景。

<template>
  <div class="circle-progress">
    <canvas ref="canvas" width="200" height="200"></canvas>
    <div class="progress-text">{{ progress }}%</div>
  </div>
</template>

<script>
export default {
  props: {
    progress: {
      type: Number,
      default: 0,
      validator: value => value >= 0 && value <= 100
    }
  },
  mounted() {
    this.drawProgress();
  },
  watch: {
    progress() {
      this.drawProgress();
    }
  },
  methods: {
    drawProgress() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      const centerX = canvas.width / 2;
      const centerY = canvas.height / 2;
      const radius = 80;
      const lineWidth = 10;

      // 清除画布
      ctx.clearRect(0, 0, canvas.width, canvas.height);

      // 绘制背景圆
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
      ctx.strokeStyle = '#e6e6e6';
      ctx.lineWidth = lineWidth;
      ctx.stroke();

      // 绘制进度圆
      const startAngle = -Math.PI / 2;
      const endAngle = startAngle + (Math.PI * 2 * this.progress / 100);
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, startAngle, endAngle);
      ctx.strokeStyle = '#42b983';
      ctx.lineWidth = lineWidth;
      ctx.stroke();
    }
  }
};
</script>

使用第三方库实现圆环进度

对于更复杂的需求,可以使用专门的可视化库如 vue-awesome-progressecharts

安装 vue-awesome-progress

npm install vue-awesome-progress

使用示例:

<template>
  <div>
    <circle-progress
      :percent="progress"
      :size="200"
      :border-width="10"
      :border-bg-width="10"
      fill-color="#42b983"
      empty-color="#e6e6e6"
    />
  </div>
</template>

<script>
import CircleProgress from 'vue-awesome-progress';

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

动画效果优化

为进度条添加平滑的动画过渡效果,可以使用 CSS 过渡或 JavaScript 动画库。

vue实现圆环进度

/* 为 SVG 实现的圆环添加过渡 */
circle {
  transition: stroke-dashoffset 0.5s ease-in-out;
}

/* 为 Canvas 实现的圆环添加动画 */
@keyframes progress {
  from { transform: rotate(-90deg); }
  to { transform: rotate(calc(-90deg + 360deg * var(--progress))); }
}

以上方法提供了多种实现圆环进度的方案,可以根据项目需求选择最适合的方式。

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

相关文章

圆环css制作

圆环css制作

圆环CSS制作方法 使用CSS制作圆环可以通过多种方式实现,以下是几种常见的方法: 方法一:使用border-radius和border 通过设置元素的border-radius为50%使其成为圆…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度显示的方法 在Vue中实现下载进度显示,可以通过监听XMLHttpRequest或fetch的progress事件来获取下载进度数据。以下是具体实现方式: 使用XMLHttpReques…

css制作圆环

css制作圆环

使用 border-radius 和 border 属性 通过设置元素的 border-radius 为 50% 将其变为圆形,再通过 border 属性控制圆环的宽度和颜色。调整 width 和 h…

css圆环制作

css圆环制作

使用 border-radius 和 border 属性 通过设置元素的宽度和高度相等,并添加 border-radius: 50% 可以创建一个圆形。通过调整 border 的宽度和颜色,可以形成圆…

css制作圆环扇形

css制作圆环扇形

使用CSS制作圆环扇形 方法一:利用border和transform 通过设置元素的border属性并结合transform旋转,可以创建圆环扇形效果。 <div class="sector-…

php下载进度实现

php下载进度实现

PHP 下载进度实现 在 PHP 中实现下载进度显示通常需要结合前端和后端技术,以下是一种常见的实现方法: 使用 PHP 的 readfile() 或 fread() 结合 JavaScript 这…