当前位置:首页 > 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 提供了更灵活的绘图方式,适合需要复杂动画效果的场景。

vue实现圆环进度

<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

vue实现圆环进度

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 动画库。

/* 为 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))); }
}

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

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

相关文章

vue前端实现下载进度

vue前端实现下载进度

Vue 前端实现下载进度的方法 使用 axios 的 onDownloadProgress 回调 在 axios 请求中,可以通过 onDownloadProgress 回调函数实时获取下载进度。该回…

vue实现上传进度

vue实现上传进度

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

vue实现进度圈效果

vue实现进度圈效果

使用 SVG 和 CSS 动画实现进度圈 在 Vue 中实现进度圈效果可以通过 SVG 结合 CSS 动画或 JavaScript 动态计算完成。以下是两种常见方法: 方法一:基于 SVG 的环形进…

vue实现视频进度加载

vue实现视频进度加载

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

圆环css制作

圆环css制作

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

css制作圆环

css制作圆环

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