vue實(shí)現(xiàn)百分比占比條效果
本文實(shí)例為大家分享了vue實(shí)現(xiàn)百分比占比條的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
1.各自占比
/p>
2.左百分百

3.右百分百

代碼實(shí)現(xiàn)
<template>
<div class="about">
<!-- <h1>This is an about page</h1> -->
<div class="step">
<!-- 左邊100%的時(shí)候不顯示斜邊三角形,并且增加右邊角 -->
<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>總數(shù):{{ 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>總數(shù):{{ 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;
}
// 百分百的時(shí)候不顯示該偽類
.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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3生命周期Hooks原理與調(diào)度器Scheduler關(guān)系
這篇文章主要為大家介紹了Vue3生命周期Hooks原理與調(diào)度器Scheduler關(guān)系詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue3實(shí)現(xiàn)長(zhǎng)列表虛擬滾動(dòng)的示例代碼
本文主要介紹了vue3實(shí)現(xiàn)長(zhǎng)列表虛擬滾動(dòng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯(cuò)Failed?to?resolve?import的原因及解決
這篇文章主要給大家介紹了關(guān)于vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯(cuò)Failed?to?resolve?import的原因及解決方案,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例
本篇文章主要介紹了vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
如何通過(guò)Vue實(shí)現(xiàn)@人的功能
這篇文章主要介紹了如何通過(guò)vue實(shí)現(xiàn)微博中常見(jiàn)的@人的功能,同時(shí)增加鼠標(biāo)點(diǎn)擊事件和一些頁(yè)面小優(yōu)化。感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
vue基礎(chǔ)ESLint?Prettier配置教程詳解
這篇文章主要介紹了vue基礎(chǔ)ESLint?Prettier配置教程詳解,本文使用VsCode?+?Vue?+?ESLint?+?Prettier?實(shí)現(xiàn)代碼格式規(guī)范?+?保存自動(dòng)修復(fù)代碼js+vue2022-07-07
Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)一個(gè)輪播圖自動(dòng)播放功能
better-scroll是一個(gè)非常非常強(qiáng)大的第三方庫(kù) 在移動(dòng)端利用這個(gè)庫(kù) 不僅可以實(shí)現(xiàn)一個(gè)非常類似原生ScrollView的效果 也可以實(shí)現(xiàn)一個(gè)輪播圖的效果。這篇文章主要介紹了Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)一個(gè)輪播圖,需要的朋友可以參考下2018-12-12

