半圆进度条 vue

<template>
  <svg :viewBox="`0 0 ${2*radius + srtokeWidth} ${radius + srtokeWidth}`">
    <path
      :d="`M ${srtokeWidth/2} ${radius + srtokeWidth/2} a ${radius} ${radius} 0 1 1 ${radius * 2} 0`"
      :stroke-width="srtokeWidth"
      :stroke="emptyColor"
      fill="none"
      stroke-linecap="round"
    ></path>
    <path
      :d="`M ${ srtokeWidth/2} ${radius + srtokeWidth/2} a ${radius} ${radius} 0 1 1 ${radius * 2} 0`"
      :stroke-width="srtokeWidth"
      :stroke="valueColor"
      fill="none"
      stroke-linecap="round"
      :stroke-dasharray="strokeDasharray"
      :stroke-dashoffset="strokeDashoffset"
    >
      <animate
        attributeName="stroke-dashoffset"
        :dur="`${durTime}ms`"
        fill="freeze"
        :from="strokeDasharray"
        :to="strokeDashoffset"
      ></animate>
    </path>
  </svg>
</template>
<script>
export default {
  data () {
    return {
      defaultDurTime: 1200,
      defaultEmptyColor: '#EEEEEE',
      defaultNegativeColor: '#FF4456',
      defaultPositiveColor: '#56D5E4',
      defaultSrtokeWidth: 10,
      defaultRadius: 50
    }
  },
  props: ['options', 'value'],
  computed: {
    durTime () {
      return this.options
        ? this.options.durTime || this.defaultDurTime
        : this.defaultDurTime
    },
    radius () {
      return this.options
        ? this.options.radius || this.defaultRadius
        : this.defaultRadius
    },
    emptyColor () {
      return this.options
        ? this.options.emptyColor || this.defaultEmptyColor
        : this.defaultEmptyColor
    },
    srtokeWidth () {
      return this.options
        ? this.options.srtokeWidth || this.defaultSrtokeWidth
        : this.defaultSrtokeWidth
    },
    valueColor () {
      if (this.value < 0) {
        return this.options
          ? this.options.negativeColor || this.defaultNegativeColor
          : this.defaultNegativeColor
      } else {
        return this.options
          ? this.options.positiveColor || this.defaultPositiveColor
          : this.defaultPositiveColor
      }
    },
    strokeDasharray () {
      return 3.1415926 * this.radius
    },
    strokeDashoffset () {
      return this.strokeDasharray - this.strokeDasharray * this.value
    }
  }
}
</script>

复制代码

转载于:https://juejin.im/post/5cc7f1fde51d456e5b66ae01

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中实现半圆进度条可以通过以下步骤来完成: 1. 首先,在Vue组件中创建一个变量来存储进度(0-100之间的数字),例如:`progress: 50`。 2. 在模板中使用SVG元素创建一个半圆形,可以使用`<path>`元素来绘制半圆形路径。 3. 使用`stroke-dasharray`和`stroke-dashoffset`属性来控制路径的显示范围和偏移量。例如,将`stroke-dasharray`设置为半圆的周长,并根据进度计算`stroke-dashoffset`的值。 4. 使用样式来调整路径的颜色,线宽等。 下面是一个简单的示例代码: ```vue <template> <div class="progress-bar"> <svg class="circle"> <path :d="circlePath" :stroke-dasharray="circleLength" :stroke-dashoffset="circleOffset" fill="transparent" stroke="#00ff00" stroke-width="10" /> </svg> </div> </template> <script> export default { data() { return { progress: 50, radius: 50, }; }, computed: { circlePath() { const x = this.radius; const y = this.radius; const startAngle = -Math.PI / 2; // 半圆的起始角度 const endAngle = (this.progress / 100) * Math.PI + startAngle; // 根据进度计算终止角度 const largeArcFlag = endAngle - startAngle <= Math.PI ? 0 : 1; // 是否大于半圆 const sweepFlag = 1; // 绘制方向,时针为1,逆时针为0 // 生成SVG路径字符串 return `M ${x} ${y} L ${x} 0 A ${this.radius} ${this.radius} 0 ${largeArcFlag} ${sweepFlag} ${x + this.radius * Math.cos(endAngle)} ${y + this.radius * Math.sin(endAngle)} Z`; }, circleLength() { return Math.PI * this.radius; }, circleOffset() { const circumference = this.circleLength; const progressOffset = (100 - this.progress) * circumference / 100; return progressOffset; }, }, }; </script> <style scoped> .progress-bar { width: 100px; /* 进度条容器的宽度 */ height: 100px; /* 进度条容器的高度 */ } .circle { transform: rotate(-90deg); /* 将半圆旋转到水平位置 */ } </style> ``` 这个示例代码会根据`progress`变量的值来显示相应的进度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值