js手写环形进度条

使用效果示例:(圆弧部分)
在这里插入图片描述

组件使用

字段描述是否必填类型默认值
color圆弧颜色string#409eff
value圆弧的值numbernull
bShowRadius是否显示圆角booleantrue
sTrokeWidth圆弧的线宽number4
<processBox :value="progress" :color="lineColor" :bShowRadius="false"/>

组件代码

<template>
    <div class="process-wrap" :id="id">
        <svg width="100%" height="100%">
            <circle
                    cx="50%"
                    cy="50%"
                    r="45%"
                    fill-opacity="0"
                    :stroke-width="defaultLength"
                    stroke="#eee"
            />
            <circle
                    cx="50%"
                    cy="50%"
                    r="45%"
                    fill-opacity="0"
                    :stroke-width="defaultLength"
                    :stroke="defaultColor"
                    :stroke-dasharray="`${processLength} ${sumLength}`"
                    :stroke-linecap="bShowRadius ? 'round' : 'butt'"
                    style="transform: rotate(-90deg); transform-origin: 50% 50%;transition: all .6s;"
            />
        </svg>
    </div>
</template>

<script lang="ts" setup>
import {defineProps, computed, nextTick, onMounted, ref} from 'vue'

const props = defineProps<{
    color?: string, // 圆弧背景颜色
    value: number, // 数值,限制在 0 - 100
    bShowRadius?: boolean, // 是否显示圆角
    sTrokeWidth?: number, // 圆弧线宽
}>()

const bShowRadius = ref<boolean>( typeof props.bShowRadius === 'boolean' ? props.bShowRadius : true)
const defaultColor = ref<string>( props.color || '#409eff' )
const defaultLength = ref<number>( props.sTrokeWidth || 4)

// 动态id
let id = ref<string>( 'process-color' + Math.ceil(Math.random() * 1000) )

// 总长度
let sumLength = ref<number>(1000)

// 渲染圆弧长度
let processLength = computed(() => {
    if (sumLength.value === 1000) return 0
    return sumLength.value * (props.value / 100) || 0
})

onMounted(() => {
    sumLength.value = document.getElementById(id.value).offsetWidth * 0.45 * Math.PI * 2
})

</script>

<style scoped>
.process-wrap {
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 1/1;
}
</style>

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值