当前位置:首页 > VUE

vue实现圆环进度环

2026-02-24 13:17:34VUE

使用 SVG 实现圆环进度条

SVG 的 <circle> 元素适合绘制圆环进度条。通过 stroke-dasharraystroke-dashoffset 控制进度。

模板代码

<template>
  <div class="progress-ring">
    <svg width="120" height="120">
      <circle
        class="progress-ring__background"
        stroke="#eee"
        stroke-width="8"
        fill="transparent"
        r="52"
        cx="60"
        cy="60"
      />
      <circle
        class="progress-ring__circle"
        stroke="#42b983"
        stroke-width="8"
        fill="transparent"
        r="52"
        cx="60"
        cy="60"
        :style="{
          strokeDasharray: circumference,
          strokeDashoffset: dashOffset,
        }"
      />
    </svg>
    <div class="progress-text">{{ progress }}%</div>
  </div>
</template>

脚本逻辑

vue实现圆环进度环

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

样式调整

<style scoped>
.progress-ring {
  position: relative;
  display: inline-block;
}
.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
}
.progress-ring__circle {
  transition: stroke-dashoffset 0.5s ease;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
}
</style>

使用 Canvas 实现动态圆环

Canvas 适合需要动态渲染的场景,通过 requestAnimationFrame 实现动画效果。

vue实现圆环进度环

模板与脚本

<template>
  <canvas ref="canvas" width="200" height="200"></canvas>
</template>

<script>
export default {
  props: {
    progress: {
      type: Number,
      default: 0,
    },
  },
  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, 2 * Math.PI);
      ctx.strokeStyle = "#eee";
      ctx.lineWidth = lineWidth;
      ctx.stroke();

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

      // 绘制文本
      ctx.fillStyle = "#333";
      ctx.font = "24px Arial";
      ctx.textAlign = "center";
      ctx.fillText(`${this.progress}%`, centerX, centerY + 10);
    },
  },
};
</script>

使用第三方库(如 vue-awesome-progress

安装库后直接调用组件,适合快速集成:

npm install vue-awesome-progress

示例代码

<template>
  <vue-awesome-progress
    type="circle"
    :progress="progress"
    :color="'#42b983'"
    :size="120"
  />
</template>

<script>
import VueAwesomeProgress from "vue-awesome-progress";
export default {
  components: { VueAwesomeProgress },
  data() {
    return {
      progress: 75,
    };
  },
};
</script>

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

相关文章

vue实现环形进度

vue实现环形进度

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

vue实现视频进度加载

vue实现视频进度加载

实现视频进度加载的基本思路 在Vue中实现视频进度加载通常涉及监听视频的timeupdate事件,计算当前播放时间与总时长的比例,并更新进度条显示。核心是通过<video>元素的API和自…

css制作圆环

css制作圆环

使用 border-radius 和 border 属性 通过设置元素的 border-radius 为 50% 使其变为圆形,再通过 border 属性调整边框宽度和颜色即可形成圆环。 .circ…

js怎么实现圆环

js怎么实现圆环

使用 SVG 实现圆环 SVG 是一种矢量图形格式,可以通过 <circle> 或 <path> 标签绘制圆环。以下是一个简单的示例: <svg width="100"…

css制作圆环扇形

css制作圆环扇形

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

php如何实现下载进度

php如何实现下载进度

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