当前位置:首页 > 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实现多行多个表单

vue实现多行多个表单

Vue 实现多行多个表单 在 Vue 中实现多行多个表单,通常需要动态生成表单字段,并确保数据绑定和提交逻辑正确。以下是实现方法: 动态表单数据绑定 使用 v-for 动态渲染多行表单,并通过 v-…

vue实现多个商品对比

vue实现多个商品对比

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

react如何渲染多个页面

react如何渲染多个页面

渲染多个页面的方法 在React中渲染多个页面通常涉及路由配置和组件组织。以下是几种常见方法: 使用React Router React Router是处理多页面应用最流行的库。安装后配置路由:…

react一个弹窗多个表单如何处理

react一个弹窗多个表单如何处理

处理多个表单的弹窗设计 在React中实现一个弹窗包含多个表单,需要合理管理表单状态、切换逻辑以及数据提交。以下是具体实现方法: 状态管理与结构设计 使用useState或useReducer管理当…

js怎么实现圆环

js怎么实现圆环

使用 SVG 实现圆环 SVG 是一种矢量图形格式,可以通过 <circle> 或 <path> 标签绘制圆环。以下是一个简单的示例: <svg width="100"…

uniapp多个吸顶

uniapp多个吸顶

实现多个吸顶效果的方法 在UniApp中实现多个吸顶效果,可以通过以下方式完成: 使用uni-sticky组件 UniApp官方提供了uni-sticky组件,可以轻松实现吸顶效果。对于多个…