当前位置:首页 > VUE

vue实现进度圈效果

2026-02-22 13:26:51VUE

Vue 实现进度圈效果

使用 SVG 和 CSS 动画

通过 SVG 的 circle 元素和 CSS 动画实现动态进度圈效果。以下是一个基础实现示例:

<template>
  <div class="progress-circle">
    <svg width="100" height="100" viewBox="0 0 100 100">
      <circle
        class="progress-background"
        cx="50"
        cy="50"
        r="45"
        fill="none"
        stroke="#e6e6e6"
        stroke-width="8"
      />
      <circle
        class="progress-bar"
        cx="50"
        cy="50"
        r="45"
        fill="none"
        stroke="#42b983"
        stroke-width="8"
        :stroke-dasharray="dashArray"
        :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
    }
  },
  computed: {
    dashArray() {
      return Math.PI * 90;
    },
    dashOffset() {
      return this.dashArray * (1 - this.progress / 100);
    }
  }
};
</script>

<style>
.progress-circle {
  position: relative;
  width: 100px;
  height: 100px;
}
.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
}
.progress-bar {
  transition: stroke-dashoffset 0.5s ease;
}
</style>

使用第三方库(如 vue-progress-circle)

对于更复杂的需求,可以使用现成的 Vue 组件库:

vue实现进度圈效果

安装依赖:

npm install vue-progress-circle

使用示例:

vue实现进度圈效果

<template>
  <vue-progress-circle
    :progress="progress"
    :size="100"
    :line-width="8"
    :fill-color="'#e6e6e6'"
    :progress-color="'#42b983'"
  />
</template>

<script>
import VueProgressCircle from 'vue-progress-circle';

export default {
  components: {
    VueProgressCircle
  },
  data() {
    return {
      progress: 75
    };
  }
};
</script>

自定义动画效果

通过 Vue 的过渡和动画系统,可以添加更丰富的交互效果:

<template>
  <div class="animated-circle">
    <svg width="120" height="120" viewBox="0 0 120 120">
      <circle
        class="base-circle"
        cx="60"
        cy="60"
        r="50"
      />
      <circle
        class="progress-circle"
        cx="60"
        cy="60"
        r="50"
        :style="circleStyle"
      />
    </svg>
  </div>
</template>

<script>
export default {
  props: ['progress'],
  computed: {
    circleStyle() {
      const circumference = 2 * Math.PI * 50;
      return {
        strokeDasharray: circumference,
        strokeDashoffset: circumference * (1 - this.progress / 100),
        transition: 'stroke-dashoffset 0.8s cubic-bezier(0.4, 0, 0.2, 1)'
      };
    }
  }
};
</script>

<style>
.animated-circle {
  position: relative;
  width: 120px;
  height: 120px;
}
.base-circle {
  fill: none;
  stroke: #eee;
  stroke-width: 8;
}
.progress-circle {
  fill: none;
  stroke: #ff7043;
  stroke-width: 8;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
}
</style>

响应式设计技巧

确保进度圈在不同设备上表现一致:

.progress-container {
  width: 100%;
  max-width: 200px;
  margin: 0 auto;
}
.progress-circle {
  width: 100%;
  height: auto;
}
@media (max-width: 768px) {
  .progress-circle {
    width: 80px;
    height: 80px;
  }
}

以上方法提供了从基础到进阶的多种实现方式,可根据项目需求选择合适方案。SVG 方案具有最佳的性能和灵活性,而第三方库则能快速实现标准化效果。

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

相关文章

vue实现multiselect效果

vue实现multiselect效果

Vue 实现多选(Multiselect)效果 使用第三方库 vue-multiselect 安装 vue-multiselect 库: npm install vue-multiselec…

vue实现回弹效果

vue实现回弹效果

Vue 实现回弹效果的方法 回弹效果(Bounce Effect)常见于移动端交互,可以通过 CSS 动画或 JavaScript 实现。以下是几种在 Vue 中实现回弹效果的方案: 使用 CSS…

vue实现选中效果

vue实现选中效果

实现选中效果的方法 在Vue中实现选中效果可以通过多种方式,常见的有使用v-bind:class动态绑定类名、利用v-model与表单元素结合,或通过状态管理控制选中样式。以下是几种典型实现方案:…

vue实现文字闪烁效果

vue实现文字闪烁效果

实现文字闪烁效果的方法 在Vue中实现文字闪烁效果可以通过CSS动画或JavaScript定时器来控制。以下是几种常见的方法: 使用CSS动画 通过定义CSS的@keyframes规则实现周期性闪烁…

vue怎么实现隐藏效果

vue怎么实现隐藏效果

使用v-show指令 v-show通过控制CSS的display属性实现隐藏效果。元素始终保留在DOM中,仅切换显示状态。 <div v-show="isHidden">内容<…

vue实现弹幕移动效果

vue实现弹幕移动效果

Vue 实现弹幕移动效果 弹幕移动效果可以通过 CSS 动画结合 Vue 的动态渲染实现。以下是具体实现方法: 基本实现思路 数据驱动弹幕内容 使用 Vue 的 v-for 动态渲染弹幕列表,每条…