百分比占比条的实现

效果图

1.各自占比
在这里插入图片描述
2.左百分百
在这里插入图片描述
3.右百分百
在这里插入图片描述

代码实现
<template>
  <div class="about">
    <!-- <h1>This is an about page</h1> -->
    <div class="step">
      <!-- 左边100%的时候不显示斜边三角形,并且增加右边角 -->
      <div
        class="left"
        v-show="leftPercent"
        :class="[{ 'full-left': !rightPercent }, { 'tringle': rightPercent }]"
        :style="{ width: leftPercent+'%' }"
        @mouseover="onMouseTooltip(LEFT_BAR, SHOW_TIP)"
        @mouseleave="onMouseTooltip(LEFT_BAR, HIDE_TIP)"
      >
        <div class="bar-tip-box" v-show="leftBar.isShowTip">
          <p>总数:{{ totalNum }}</p>
          <p>绿色所占比例:{{ leftPercent }}%</p>
        </div>
        <div class="tip-arrow" v-show="leftBar.isShowTip"></div>
        {{ leftPercent }}%
      </div>
      <div
        class="right"
        v-show="rightPercent"
        :class="[{ 'full-right': !leftPercent }]"
        @mouseover="onMouseTooltip(RIGHT_BAR, SHOW_TIP)"
        @mouseleave="onMouseTooltip(RIGHT_BAR, HIDE_TIP)"
      >
        <div class="bar-tip-box" v-show="rightBar.isShowTip">
          <p>总数:{{ totalNum }}</p>
          <p>灰色所占比例:{{ rightPercent }}%</p>
        </div>
        <div class="tip-arrow" v-show="rightBar.isShowTip"></div>
        {{ rightPercent }}%
      </div>
    </div>
  </div>
</template>

<script>
const LEFT_BAR = "left";
const RIGHT_BAR = "right";
const SHOW_TIP = "show";
const HIDE_TIP = "hide";

export default {
  data() {
    return {
      LEFT_BAR: LEFT_BAR,
      RIGHT_BAR: RIGHT_BAR,
      SHOW_TIP: SHOW_TIP,
      HIDE_TIP: HIDE_TIP,
      totalNum: 1000,
      leftPercent: 100,
      leftBar: {
        isShowTip: false,
        delayOut: null
      },
      rightBar: {
        isShowTip: false,
        delayOut: null
      }
    };
  },
  methods: {
    onMouseTooltip(tipType, actionType) {
      let bar = null;
      if (tipType == LEFT_BAR) {
        bar = this.leftBar;
      } else if (tipType == RIGHT_BAR) {
        bar = this.rightBar;
      } else {
        return;
      }
      if (actionType === SHOW_TIP) {
        this.showBarTooltip(bar);
      } else if (actionType === HIDE_TIP) {
        this.hideBarTooltip(bar);
      } else {
        return;
      }
    },
    showBarTooltip(bar) {
      if (bar.delayOut != null) {
        clearTimeout(bar.delayOut);
      }
      bar.delayOut = null;
      bar.isShowTip = true;
    },
    hideBarTooltip(bar) {
      clearTimeout(bar.delayOut);
      bar.delayOut = setTimeout(function() {
        bar.isShowTip = false;
      }, 100);
    },
  },
  computed: {
    rightPercent: function(){
      return 100 - this.leftPercent;
    }
  }
};
</script>

<style lang="less" scoped>
.step {
  position: relative;
  display: flex;
  margin: 100px;
  width: 200px;
  font-size: 0;
  .left {
    flex-grow: 0;
    position: relative;
    display: inline-block;
    background: #62c87f;
    color: #fff;
    text-align: center;
    font-weight: bold;
    width: 70%;
    font-size: 12px;
    line-height: 20px;
    height: 20px;
    min-width: 30px;
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
  }
  // 百分百的时候不显示该伪类
  .tringle::after {
    content: " ";
    position: absolute;
    top: 0;
    right: -8px;
    border-width: 20px 8px;
    border-style: solid;
    border-color: #62c87f transparent transparent transparent;
    z-index: 10;
  }

  .right {
    flex-grow: 1;
    position: relative;
    display: inline-block;
    /* width:30%; */
    background: #d0d4dc;
    color: #333;
    font-weight: bold;
    text-align: center;
    font-size: 12px;
    line-height: 20px;
    height: 20px;
    text-align: center;
    min-width: 35px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
  }

  .full-left{
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
  }

  .full-right{
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
  }

  .tip-arrow {
    position: absolute;
    left: 50%;
    top: -10px;
    display: inline-block;
    width: 7px;
    height: 7px;
    transform: rotateZ(45deg);
    -webkit-transform: rotateZ(45deg);
    background-color: #7f7f7f;
    z-index: 10;
  }

  .bar-tip-box {
    position: absolute;
    top: -5px;
    right: 50%;
    transform: translate(50%, -100%);
    text-align: left;
    padding: 5px 10px;
    width: max-content;
    color: #fff;
    font-size: 12px;
    font-weight: 400;
    border-radius: 3px;
    background-color: #7f7f7f;
    z-index: 10;

    p {
      margin: 0;
      padding-bottom: 5px;
    }
  }
}
</style>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题已经收到。这里是使用Angular开发分三段进度条,每一段颜色背景颜色用红蓝绿,且显示百分比比的实现思路: 1. 创建组件: 首先,我们需要创建一个分段进度条的组件。使用以下命令来创建一个名为progress-bar的组件: ng generate component progress-bar 2. 添加进度条: 在progress-bar组件的HTML模板中添加一个进度条。我们需要使用CSS样式来定义进度条的样式。按照以下步骤来创建进度条: 2.1 创建一个带有3个span元素的div 2.2 将每个span元素的宽度设置为 33.33%(或任何您想要的百分比) 2.3 将第一个span元素的背景色设置为红色,第二个为蓝色,第三个为绿色 3. 根据数据调整进度条: 我们将使用@Input装饰器将进度数据传递到progress-bar组件。当我们接收到prop传递的数据时,我们需要通过JS代码来调整进度条的宽度。按照以下步骤来完成这个过程: 3.1 在组件类中定义一个名为“progressValue”的Input属性,并设置默认值为0 3.2 在ngOnInit()生命周期钩子方法中编写代码,根据数据来调整进度条宽度。 3.3 根据传递的数据计算进度条每个部分的宽度。可以通过以下公式计算:宽度 = 总宽度 * 百分比 / 100 3.4 使用JavaScript代码将进度条每个部分的宽度调整为计算出的宽度。 综上所述,我们可以使用Angular来轻松创建一个分三段进度条,每一段颜色背景颜色用红蓝绿,且显示百分比比。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值