当前位置:首页 > 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 接受外部传入的尺寸参数

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

相关文章

vue前端实现下载进度

vue前端实现下载进度

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

h5所能实现的效果

h5所能实现的效果

H5能实现的效果 H5(HTML5)作为最新的HTML标准,具备强大的功能和丰富的API支持,能够实现多种现代Web应用效果。以下是H5的主要能力分类: 多媒体支持 H5原生支持音频和视频播放,无需…

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 Vue.js 提供了多种方式来实现动态效果展示,以下是一些常见的方法: 使用 Vue 的过渡和动画系统 Vue 内置了 <transition> 和 <…

h5实现左右滚动效果

h5实现左右滚动效果

使用CSS实现横向滚动 在HTML5中实现左右滚动效果,可以通过CSS的overflow-x属性配合white-space或flex布局实现。以下是一个基础示例: <div class="sc…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果通常涉及动态样式绑定、动画过渡或第三方库的使用。以下是几种常见实现方式: 动态类名绑定 通过 v-bind:class 或简写 :class 实…

vue实现烟花效果

vue实现烟花效果

Vue 实现烟花效果的方法 在Vue中实现烟花效果可以通过结合Canvas绘图和动画技术来完成。以下是一种常见的实现方式: 创建Vue组件 新建一个名为Fireworks.vue的组件,核心逻辑如下…