当前位置:首页 > VUE

vue实现进度圈效果

2026-01-21 22:20:40VUE

使用 SVG 和 CSS 动画实现进度圈

在 Vue 中实现进度圈效果可以通过 SVG 结合 CSS 动画或 JavaScript 动态计算完成。以下是两种常见方法:

方法一:基于 SVG 的环形进度条

通过 SVG 的 <circle> 元素绘制环形路径,利用 stroke-dasharraystroke-dashoffset 控制进度:

<template>
  <div class="progress-circle">
    <svg width="100" height="100" viewBox="0 0 100 100">
      <!-- 背景圆 -->
      <circle
        cx="50"
        cy="50"
        r="45"
        fill="none"
        stroke="#eee"
        stroke-width="10"
      />
      <!-- 进度圆 -->
      <circle
        cx="50"
        cy="50"
        r="45"
        fill="none"
        stroke="#42b983"
        stroke-width="10"
        :stroke-dasharray="circumference"
        :stroke-dashoffset="offset"
        transform="rotate(-90 50 50)"
      />
    </svg>
    <span>{{ progress }}%</span>
  </div>
</template>

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

<style>
.progress-circle {
  position: relative;
  display: inline-block;
}
.progress-circle span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
}
</style>

方法二:使用第三方库(如 vue-progress-path)

对于更复杂的场景,可以集成专门库简化实现:

  1. 安装库:

    npm install vue-progress-path
  2. 在组件中使用:

    
    <template>
    <progress-circle :progress="progress" :size="100" :thickness="10"/>
    </template>
import { ProgressCircle } from 'vue-progress-path'; import 'vue-progress-path/dist/vue-progress-path.css';

export default { components: { ProgressCircle }, data() { return { progress: 75 } } };

```

方法三:动态过渡效果

通过 Vue 的过渡系统实现平滑动画:

vue实现进度圈效果

<template>
  <svg width="100" height="100">
    <circle ... />
    <circle
      :style="{ 
        'stroke-dashoffset': animatedOffset,
        transition: 'stroke-dashoffset 0.5s ease-out'
      }"
    />
  </svg>
</template>

<script>
export default {
  watch: {
    progress(newVal) {
      this.animatedOffset = this.circumference * (1 - newVal / 100);
    }
  }
};
</script>

关键参数说明

  • stroke-dasharray: 定义虚线的间距(此处设置为周长实现连续效果)
  • stroke-dashoffset: 控制虚线起始偏移量(动态计算实现进度变化)
  • rotate(-90 50 50): 将进度起点从默认的3点钟方向调整为12点钟方向

扩展功能建议

  1. 添加颜色渐变:通过 <linearGradient> 定义 SVG 渐变
  2. 多状态样式:根据进度值动态切换 CSS 类名
  3. 插槽支持:允许自定义中心文本内容
  4. 响应式尺寸:通过 props 接受外部传入的尺寸参数

标签: 进度效果
分享给朋友:

相关文章

jquery 效果

jquery 效果

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。jQuery 提供了丰富的效果(Effects)功能,可以轻松实现元素的动态显…

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

vue实现基金效果

vue实现基金效果

Vue 实现基金效果 在 Vue 中实现基金效果,通常指的是模拟基金的增长、波动或可视化展示。以下是几种常见的实现方式: 数据绑定与动态更新 通过 Vue 的数据绑定特性,可以动态展示基金净值的变化…

vue实现滚动效果

vue实现滚动效果

Vue 实现滚动效果的方法 使用原生滚动方法 通过 Vue 的 ref 获取 DOM 元素,调用原生 scrollTo 或 scrollIntoView 方法实现滚动。 <template&g…

vue实现凤凰效果

vue实现凤凰效果

Vue 实现凤凰效果 凤凰效果通常指一种视觉动画,模拟凤凰展翅、火焰燃烧等动态效果。在 Vue 中实现这种效果,可以结合 CSS 动画、Canvas 或第三方动画库(如 GSAP)。以下是几种实现方式…

react如何添加效果

react如何添加效果

React 添加动画效果的方法 使用 CSS 过渡动画 在 React 组件中直接通过 className 或 style 属性应用 CSS 过渡效果。定义 CSS 类包含 transition 属性…