当前位置:首页 > VUE

vue实现多个圆环

2026-02-11 10:31:52VUE

使用 SVG 实现多个圆环

SVG 是绘制圆环的高效方式,适合动态调整样式。以下是一个基于 Vue 的示例:

vue实现多个圆环

<template>
  <svg width="200" height="200">
    <!-- 背景圆环 -->
    <circle
      cx="100"
      cy="100"
      :r="radius"
      fill="none"
      stroke="#eee"
      stroke-width="strokeWidth"
    />
    <!-- 动态圆环 -->
    <circle
      cx="100"
      cy="100"
      :r="radius"
      fill="none"
      stroke="#42b983"
      :stroke-width="strokeWidth"
      :stroke-dasharray="dashArray"
      stroke-linecap="round"
    />
  </svg>
</template>

<script>
export default {
  data() {
    return {
      radius: 80,
      strokeWidth: 10,
      progress: 75, // 百分比
    };
  },
  computed: {
    dashArray() {
      const circumference = 2 * Math.PI * this.radius;
      return `${(circumference * this.progress) / 100} ${circumference}`;
    },
  },
};
</script>

使用 Canvas 绘制多个圆环

Canvas 适合复杂动画或大量圆环场景。以下是一个 Vue 组件示例:

vue实现多个圆环

<template>
  <canvas ref="canvas" width="200" height="200"></canvas>
</template>

<script>
export default {
  mounted() {
    this.drawCircles();
  },
  methods: {
    drawCircles() {
      const ctx = this.$refs.canvas.getContext('2d');
      const center = { x: 100, y: 100 };

      // 背景圆环
      ctx.beginPath();
      ctx.arc(center.x, center.y, 80, 0, Math.PI * 2);
      ctx.strokeStyle = '#eee';
      ctx.lineWidth = 10;
      ctx.stroke();

      // 动态圆环
      ctx.beginPath();
      ctx.arc(center.x, center.y, 80, -Math.PI / 2, (Math.PI * 2 * 75) / 100 - Math.PI / 2);
      ctx.strokeStyle = '#42b983';
      ctx.lineWidth = 10;
      ctx.stroke();
    },
  },
};
</script>

使用 CSS 和伪元素实现

纯 CSS 方案适合静态圆环,结合 Vue 动态绑定样式:

<template>
  <div class="ring-container">
    <div class="ring-background"></div>
    <div 
      class="ring-progress" 
      :style="{
        '--progress': `${progress}%`,
        '--color': '#42b983',
        '--width': '10px'
      }"
    ></div>
  </div>
</template>

<style>
.ring-container {
  position: relative;
  width: 200px;
  height: 200px;
}

.ring-background, .ring-progress {
  position: absolute;
  border-radius: 50%;
  width: 100%;
  height: 100%;
}

.ring-background {
  border: 10px solid #eee;
}

.ring-progress {
  clip: rect(0, 100px, 200px, 0);
  transform: rotate(90deg);
  border: var(--width) solid transparent;
  border-top-color: var(--color);
  border-right-color: var(--color);
  animation: fill 1s linear forwards;
}

@keyframes fill {
  100% {
    transform: rotate(calc(360deg * var(--progress) / 100 + 90deg));
  }
}
</style>

使用第三方库(如 ECharts)

对于复杂需求,ECharts 提供高度可定制的圆环图:

<template>
  <div ref="chart" style="width: 200px; height: 200px;"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  mounted() {
    const chart = echarts.init(this.$refs.chart);
    chart.setOption({
      series: [{
        type: 'pie',
        radius: ['60%', '80%'],
        data: [
          { value: 75, name: 'Progress', itemStyle: { color: '#42b983' } },
          { value: 25, name: 'Remaining', itemStyle: { color: '#eee' } }
        ],
        label: { show: false }
      }]
    });
  },
};
</script>

关键注意事项

  • 性能优化:大量圆环优先选择 Canvas 或 SVG,避免 DOM 过多。
  • 响应式设计:通过 Vue 的 computedwatch 动态调整圆环尺寸。
  • 交互支持:SVG/CSS 方案更适合添加鼠标事件(如 @click)。

以上方法可根据具体需求组合或扩展。

标签: 多个圆环
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 在 Vue 中实现多个 Tab 功能可以通过动态组件、条件渲染或第三方库来完成。以下是几种常见的方法: 使用动态组件和 v-for 循环 通过动态组件和 v-fo…

vue实现多个图表展示

vue实现多个图表展示

使用 Vue 实现多个图表展示 安装图表库 推荐使用 ECharts 或 Chart.js,两者均支持 Vue 集成。以 ECharts 为例,安装依赖: npm install echart…

vue实现多个元素

vue实现多个元素

Vue 实现多个元素的方法 在 Vue 中实现多个元素的渲染或操作有多种方式,具体取决于需求场景。以下是常见的几种方法: 使用 v-for 渲染列表 通过 v-for 指令可以动态渲染多个元素,适…

Vue实现多个echarts排版

Vue实现多个echarts排版

实现多个 ECharts 实例的排版布局 在 Vue 中实现多个 ECharts 实例的排版布局,可以通过 CSS 布局结合 ECharts 的响应式设计来完成。以下是几种常见方法: 使用 Fle…

vue实现多个选框select

vue实现多个选框select

Vue 实现多个选框 Select 在 Vue 中实现多个选框(多选 Select)可以通过原生 <select> 元素结合 multiple 属性,或使用第三方 UI 库(如 Eleme…

vue实现多个商品对比

vue实现多个商品对比

Vue 实现多个商品对比功能 实现多个商品对比功能需要处理商品选择、对比展示以及交互逻辑。以下是具体实现方案: 商品选择组件 创建商品选择组件,允许用户勾选需要对比的商品。可以使用复选框或按钮触发…