Vue realizes the percentage bar effect

Vue realizes the percentage bar effect

This article shares the specific code of Vue to realize the percentage bar for your reference. The specific content is as follows

Rendering

1. Respective proportion

/p>

2. Left 100%

3. 100% right

insert image description here

Code Implementation

<template>
  <div class="about">
    <!-- <h1>This is an about page</h1> -->
    <div class="step">
      <!-- When the left side is 100%, the hypotenuse triangle is not displayed, and the right angle is increased-->
      <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>Total: {{ totalNum }}</p>
          <p>Green percentage: {{ 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>Total: {{ totalNum }}</p>
          <p>Gray percentage: {{ 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;
  }
  // This pseudo class is not displayed 100% of the time.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>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Details on how to write react in a vue project
  • Vue+element implements drop-down menu with local search function example
  • How to use wangEditor in vue and how to get focus by echoing data
  • Comparison of the advantages of vue3 and vue2
  • Vue realizes dynamic progress bar effect
  • Vue implements dynamic circular percentage progress bar
  • How to implement draggable components in Vue
  • Eight ways to implement communication in Vue

<<:  Use of Linux ls command

>>:  Analysis of the implementation process of three modes of VMWare network adapter

Recommend

MySQL master-slave replication principle and practice detailed explanation

Table of contents Introduction effect principle f...

Detailed steps for setting up host Nginx + Docker WordPress Mysql

environment Linux 3.10.0-693.el7.x86_64 Docker ve...

Detailed explanation of vite2.0 configuration learning (typescript version)

introduce You Yuxi’s original words. vite is simi...

Pay attention to the use of HTML tags in web page creation

This article introduces some issues about HTML ta...

Summary of 28 common JavaScript string methods and usage tips

Table of contents Preface 1. Get the length of a ...

CSS3 uses transform to create a moving 2D clock

Now that we have finished the transform course, l...

HTML 5.1 learning: 14 new features and application examples

Preface As we all know, HTML5 belongs to the Worl...

Implementation of CSS dynamic height transition animation effect

This question originated from a message on Nugget...

Sample code for automatic web page refresh and automatic jump

Automatic web page refresh: Add the following code...

Simple example of adding and removing HTML nodes

<br />Simple example of adding and removing ...

Pure CSS header fixed implementation code

There are two main reasons why it is difficult to...

How to use JS to implement waterfall layout of web pages

Table of contents Preface: What is waterfall layo...

Example of converting webpack images to base64

Download url-loader yarn add -D url-loader module...

Complete steps to build a squid proxy server in linux

Preface This article mainly introduces the releva...