当前位置:首页 > VUE

vue实现圆环进度

2026-01-17 03:48:29VUE

Vue 实现圆环进度的方法

使用 SVG 实现圆环进度

SVG 是实现圆环进度的常用方法,通过 stroke-dasharraystroke-dashoffset 属性可以轻松控制进度条的显示。

vue实现圆环进度

<template>
  <div class="progress-ring">
    <svg width="120" height="120">
      <circle
        class="progress-ring__background"
        stroke="#eee"
        stroke-width="10"
        fill="transparent"
        r="50"
        cx="60"
        cy="60"
      />
      <circle
        class="progress-ring__circle"
        stroke="#4CAF50"
        stroke-width="10"
        fill="transparent"
        r="50"
        cx="60"
        cy="60"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="offset"
      />
    </svg>
  </div>
</template>

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

<style>
.progress-ring {
  display: inline-block;
}
.progress-ring__circle {
  transition: stroke-dashoffset 0.35s;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
}
</style>

使用 Canvas 实现圆环进度

Canvas 也可以用于绘制圆环进度,适合需要动态渲染的场景。

vue实现圆环进度

<template>
  <div class="progress-ring">
    <canvas ref="canvas" width="120" height="120"></canvas>
  </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 = 50;
      const startAngle = -Math.PI / 2;
      const endAngle = startAngle + (2 * Math.PI * this.progress / 100);

      ctx.clearRect(0, 0, canvas.width, canvas.height);

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

      // 绘制进度圆环
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, startAngle, endAngle);
      ctx.strokeStyle = '#4CAF50';
      ctx.lineWidth = 10;
      ctx.stroke();
    }
  }
};
</script>

使用 CSS 实现圆环进度

CSS 的 conic-gradient 可以用于实现圆环进度,但兼容性较差。

<template>
  <div class="progress-ring">
    <div class="progress-ring__container">
      <div class="progress-ring__fill" :style="{ '--progress': progress }"></div>
    </div>
  </div>
</template>

<style>
.progress-ring__container {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: conic-gradient(#4CAF50 calc(var(--progress) * 3.6deg), #eee 0deg);
  display: flex;
  align-items: center;
  justify-content: center;
}
.progress-ring__fill {
  width: 100px;
  height: 100px;
  background: white;
  border-radius: 50%;
}
</style>

使用第三方库(如 Vuetify)

如果项目中使用了 Vuetify,可以直接使用其内置的 v-progress-circular 组件。

<template>
  <v-progress-circular
    :value="progress"
    :size="120"
    :width="10"
    color="primary"
  >
    {{ progress }}%
  </v-progress-circular>
</template>

<script>
export default {
  props: {
    progress: {
      type: Number,
      default: 0,
      validator: value => value >= 0 && value <= 100
    }
  }
};
</script>

以上方法可以根据项目需求选择适合的实现方式。SVG 和 Canvas 适合自定义需求,而第三方库可以快速集成。

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

相关文章

vue实现进度圈效果

vue实现进度圈效果

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

php实现快递进度

php实现快递进度

实现快递进度查询的PHP方法 使用快递鸟API 快递鸟提供物流查询API接口,支持多家快递公司。需注册账号获取API Key和商户ID。 // 配置参数 $appKey = '你的AppKey';…

php如何实现下载进度

php如何实现下载进度

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

js怎么实现圆环

js怎么实现圆环

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

js实现圆环

js实现圆环

使用 SVG 实现圆环 SVG(Scalable Vector Graphics)是一种基于 XML 的矢量图形格式,适合绘制圆环等图形。 <svg width="200" heigh…

vue实现多个圆环

vue实现多个圆环

使用 SVG 实现多个圆环 SVG 是绘制圆环的高效方式,适合动态调整样式。以下是一个基于 Vue 的示例: <template> <svg width="200"…