• 前端css实现统计圆环


    文章目录


    效果图
    在这里插入图片描述

    css的原理
    左右两边分开制作,左右两边各有两层盒子,外层负责遮挡溢出的部分,里层旋转表示百分比

    template部分

    // 圆环的代码
    <div class="row rel">
      <div class="left">
        <div id="left" ref="left" class="leftCon" :style="{ backgroundColor: data.color }" />
      </div>
      <div class="right">
        <div id="right" ref="right" class="rightCon" :style="{ backgroundColor: data.color }" />
      </div>
      <div class="circle" />
      <div class="centerCircle">
        {{ data.num }}%
      </div>
    </div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    js部分

    <script setup>
    import { onMounted, ref } from 'vue'
    
    const props = defineProps({
      data: {},
    })
    const left = ref(null)
    const right = ref(null)
    onMounted(() => {
      const count = props.data.num
      const totalcount = 100
      left.value.style.transform = `rotate(${getLeftPercent(count, totalcount)}deg)`
      right.value.style.transform = `rotate(${getRightPercent(count, totalcount)}deg)`
    })
    
    function getLeftPercent(count, totalcount) {
      if (totalcount === 0 || count === 0 || count / totalcount <= 0.5)
        return -180
      else
        return 180 * (((count / totalcount - 0.5) * 2) - 1)
    }
    function getRightPercent(count, totalcount) {
      if (totalcount === 0 || count === 0)
        return -180
      else if (count / totalcount >= 0.5)
        return 0
      else
        return 180 * (((count / totalcount) * 2) - 1)
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    css部分

    .left,.right {
        width: 50px;
        height: 100px;
        background: rgb(204, 204, 192);
        overflow: hidden;
    }
    .left {
        border-radius: 50px 0 0 50px;
    }
    .right {
        border-radius: 0 50px 50px 0;
    }
    .rightCon {
        width: 50px;
        height: 100px;
        border-radius: 0 100px 100px 0;
        transform: rotate(-120deg);
        transform-origin: left center;
    }
    .leftCon {
        width: 50px;
        height: 100px;
        border-radius: 50px 0px 0px 50px;
        transform: rotate(20deg);
        transform-origin: right center;
    }
    .circle {
        width: 90px;
        height: 90px;
        position: absolute;
        left: 5px;
        top: 5px;
        border-radius: 50%;
        background: rgb(251, 252, 251);
    }
    .rel {
        position: relative;
    }
    .row {
        display: flex;
        flex-direction: row;
    }
    .centerCircle {
      position     : absolute;
      border-radius: 50%;
      width        : 80px;
      height       : 80px;
      transform    : translate(10px, 10px);
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
  • 相关阅读:
    田忌赛马(贪心算法)
    【每日刷题】Day63
    iOS App上传到苹果应用市场构建版本的图文教程
    分解因数,求有多少种情况
    (ICLR-2021)一幅图像相当于16X16个words:大规模图像识别的Transformer
    189. 轮转数组 --力扣 --JAVA
    感知SOTA模型
    Qt5开发从入门到精通——第九篇二节( Qt5 文件及磁盘处理—— 读写二进制文件)
    python集成乐玩插件
    Netty:入门(1)
  • 原文地址:https://blog.csdn.net/wang13679201813/article/details/132887625