vue实现百分比占比条效果

 更新时间:2021年09月16日 11:41:40   作者:小包同学666  
这篇文章主要为大家详细介绍了vue实现百分比占比条效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue实现百分比占比条的具体代码,供大家参考,具体内容如下

效果图

1.各自占比

/p>

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Vue3生命周期Hooks原理与调度器Scheduler关系

    Vue3生命周期Hooks原理与调度器Scheduler关系

    这篇文章主要为大家介绍了Vue3生命周期Hooks原理与调度器Scheduler关系详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 傻瓜式vuex语法糖kiss-vuex整理

    傻瓜式vuex语法糖kiss-vuex整理

    kiss-vuex 是一个非常简单的语法糖类库,这篇文章主要介绍了傻瓜式vuex语法糖kiss-vuex整理,非常具有实用价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • vue3实现长列表虚拟滚动的示例代码

    vue3实现长列表虚拟滚动的示例代码

    本文主要介绍了vue3实现长列表虚拟滚动的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-01-01
  • vue3+vite+vant项目下按需引入vant报错Failed to resolve import的原因及解决方案

    vue3+vite+vant项目下按需引入vant报错Failed to resolve import的原因及解决

    这篇文章主要给大家介绍了关于vue3+vite+vant项目下按需引入vant报错Failed to resolve import的原因及解决方案,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • vue 1.x 交互实现仿百度下拉列表示例

    vue 1.x 交互实现仿百度下拉列表示例

    本篇文章主要介绍了vue 1.x 交互实现仿百度下拉列表示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • 如何通过Vue实现@人的功能

    如何通过Vue实现@人的功能

    这篇文章主要介绍了如何通过vue实现微博中常见的@人的功能,同时增加鼠标点击事件和一些页面小优化。感兴趣的小伙伴可以跟随小编一起学习一下
    2021-12-12
  • Vue中provide、inject详解以及使用教程

    Vue中provide、inject详解以及使用教程

    provide和inject主要为高阶插件/组件库提供用例,并不推荐直接用于应用程序代码中,下面这篇文章主要给大家介绍了关于Vue中provide、inject详解以及使用的相关资料,需要的朋友可以参考下
    2022-11-11
  • vue基础ESLint Prettier配置教程详解

    vue基础ESLint Prettier配置教程详解

    这篇文章主要介绍了vue基础ESLint Prettier配置教程详解,本文使用VsCode + Vue + ESLint + Prettier 实现代码格式规范 + 保存自动修复代码js+vue
    2022-07-07
  • Vue集成百度地图实现位置选择功能

    Vue集成百度地图实现位置选择功能

    由于添加门店时,需要选择门店的省、市、区、详细地址、以及门店的经纬度信息,本文给大家分享Vue集成百度地图实现位置选择功能,感兴趣的朋友一起看看吧
    2022-06-06
  • Vue项目中使用better-scroll实现一个轮播图自动播放功能

    Vue项目中使用better-scroll实现一个轮播图自动播放功能

    better-scroll是一个非常非常强大的第三方库 在移动端利用这个库 不仅可以实现一个非常类似原生ScrollView的效果 也可以实现一个轮播图的效果。这篇文章主要介绍了Vue项目中使用better-scroll实现一个轮播图,需要的朋友可以参考下
    2018-12-12

最新评论