当前位置:首页 > VUE

vue实现环形进度组件

2026-02-20 18:50:59VUE

实现环形进度组件的步骤

使用SVG和Vue实现环形进度条

在Vue中,可以通过SVG的<circle>元素结合计算属性动态调整stroke-dasharraystroke-dashoffset来实现环形进度效果。

<template>
  <div class="circle-progress">
    <svg width="120" height="120" viewBox="0 0 120 120">
      <circle
        class="progress-bg"
        cx="60"
        cy="60"
        :r="radius"
        stroke-width="10"
      />
      <circle
        class="progress-bar"
        cx="60"
        cy="60"
        :r="radius"
        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
    }
  },
  data() {
    return {
      radius: 50
    }
  },
  computed: {
    circumference() {
      return 2 * Math.PI * this.radius
    },
    dashOffset() {
      return this.circumference * (1 - this.progress / 100)
    }
  }
}
</script>

<style>
.circle-progress {
  position: relative;
  width: 120px;
  height: 120px;
}

.progress-bg {
  fill: none;
  stroke: #eee;
}

.progress-bar {
  fill: none;
  stroke: #4CAF50;
  stroke-linecap: round;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
  transition: stroke-dashoffset 0.3s ease;
}

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

使用CSS变量实现动态颜色

可以通过CSS变量让进度条颜色根据进度值动态变化:

watch: {
  progress(newVal) {
    const hue = Math.floor(newVal * 1.2)
    this.$el.style.setProperty('--progress-color', `hsl(${hue}, 80%, 50%)`)
  }
}

添加动画效果

在CSS中为进度条添加过渡效果:

.progress-bar {
  transition: stroke-dashoffset 0.5s cubic-bezier(0.65, 0, 0.35, 1);
}

实现多色环形进度条

通过多个<circle>叠加可以实现分段颜色效果:

<circle
  v-for="(segment, index) in segments"
  :key="index"
  class="progress-segment"
  cx="60"
  cy="60"
  :r="radius"
  stroke-width="10"
  :stroke-dasharray="circumference"
  :stroke-dashoffset="getSegmentOffset(index)"
  :stroke="segment.color"
/>

响应式尺寸调整

通过计算属性使组件适应不同尺寸:

computed: {
  viewBoxSize() {
    const size = this.radius * 2 + this.strokeWidth
    return `0 0 ${size} ${size}`
  }
}

注意事项

  1. SVG的viewBox需要与组件尺寸匹配
  2. stroke-dasharraystroke-dashoffset的计算基于圆的周长
  3. 进度条从顶部开始需要旋转-90度
  4. 文本居中可以使用绝对定位配合transform
  5. 过渡效果应加在stroke-dashoffset属性上

vue实现环形进度组件

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

相关文章

vue实现进度圈效果

vue实现进度圈效果

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

vue实现视频进度加载

vue实现视频进度加载

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

vue实现打卡进度组件

vue实现打卡进度组件

Vue 打卡进度组件实现 核心思路 通过动态计算已完成天数与总天数的比例,结合 CSS 动画或过渡效果,实现进度条或日历形式的打卡可视化。 基础进度条实现 模板部分 <templa…

css制作环形

css制作环形

使用 border-radius 和 border 属性 通过设置元素的 border-radius 为 50% 将其变为圆形,再通过 border 属性控制边框宽度和颜色。调整宽度和高度相同,边框透…

css环形图制作

css环形图制作

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

js实现环形

js实现环形

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