vue項目之數(shù)量占比進度條實現(xiàn)方式
vue數(shù)量占比進度條實現(xiàn)
功能說明:
1、點擊開始,滾動條自動加;
2、點擊停止,滾動條停止加;
封裝如下
<template>
<div>
<div class="progress-bar">
<div
class="progress"
:style="{
width: progressRatio + '%'
}"
>
{{ surplusProgress }}
</div>
{{ totalProgress }}
</div>
{{ surplusProgress }}
<button @click="start()">開始</button>
<button @click="stop()">停止</button>
</div>
</template>
<script>
export default {
name: 'index',
components: {},
props: {
total: {
type: Number
},
surplus: {
type: Number
}
},
data() {
return {
totalProgress: 0,
surplusProgress: 0,
progressRatio: 0,
clearInt: null // 定時器名
}
},
computed: {
// 用于監(jiān)聽兩個值同時存在的情況
progress() {
const { surplus, total } = this
return {
surplus,
total
}
}
},
watch: {
progress(newVal) {
this.totalProgress = newVal.total
this.surplusProgress = newVal.surplus
// 注意這里是當前剩余的值比上總值的一個比例顯示
this.progressRatio = (newVal.surplus / newVal.total) * 100
}
},
methods: {
start() {
let _this = this
// 增加的值 = 總數(shù)量 / 100
let addNum = _this.totalProgress / 100
clearInterval(_this.clearInt)
_this.clearInt = setInterval(function () {
_this.progressRatio = _this.progressRatio + 1
// 這里剩余的值 必須按照比例去增加 才可以最終到達總值
_this.surplusProgress = ((_this.surplusProgress * 100 + addNum * 100) / 100).toFixed(1)
if (_this.progressRatio >= 100) {
clearInterval(_this.clearInt)
_this.progressRatio = _this.progressRatio
_this.surplusProgress = parseInt(_this.surplusProgress)
}
}, 100)
},
stop() {
clearInterval(this.clearInt)
}
}
}
</script>
<style lang="scss" scoped>
.progress-bar {
width: 500px;
height: 30px;
color: #000;
text-align: center;
line-height: 30px;
box-sizing: border-box;
margin-top: 50px;
margin-bottom: 20px;
border: 1px solid gray;
box-shadow: -1px -1px 1px #000;
background: rgb(177, 174, 174);
position: relative;
.progress {
height: 100%;
background: #3165c5;
color: #fff;
overflow: hidden;
position: absolute;
opacity: 0.5;
text-align: center;
}
}
</style>
組件使用
<template>
<div class="content-box">
<div class="container">
<Progress :total="total1" :surplus="surplus1" />
<Progress :total="total2" :surplus="surplus2" />
</div>
</div>
</template>
<script>
import Progress from '@/components/ProgressBar'
export default {
name: 'record',
components: {
Progress
},
data() {
return {
surplus1: 0,
total1: 0,
surplus2: 0,
total2: 0
}
},
activated() {
let _this = this
// 模擬請求情況
setTimeout(() => {
_this.surplus1 = 30
_this.total1 = 100
_this.surplus2 = 60
_this.total2 = 220
}, 3000)
},
methods: {},
mounted() {}
}
</script>
<style scoped>
</style>
效果如下:

遇到問題
在到達終點時,總數(shù)和剩余值不一致問題,查明問題原因是小數(shù)點的問題,目前保留一位小數(shù)點;
vue實現(xiàn)進度條效果
這里實現(xiàn)進度條用到了es6語法的模版字符串,通過更改css樣式實現(xiàn)正常顯示進度的進度條和一種顯示剩余余額的進度條:
html代碼不需要改變:
<div class="scheduleCont_1_left_1" :class="10 >= 100 ? 'nobg' : ''">
//10為展示的進度,100為總的進度數(shù)量
<div :style="{ width: `${(10 / 100).toFixed(2) * 100}%` }" class="un_xg_1_3_1"></div>
</div>正常顯示進度的進度條:

.scheduleCont_1_left_1{
position: relative;
width: 100%;
height: 0.36rem;
background: #E9BBFF;
border-radius: 50px;
overflow: hidden;
.un_xg_1_3_1 {
position: absolute;
top: 0;
left: -1px;
width: 0;
height: 0.36rem;
border-radius: 50px;
background-image: linear-gradient(to right, #B72DF7);
}
}
顯示剩余余額的進度條:

.scheduleCont_1_left_1 {
position: relative;
width: 100%;
height: .24rem;
background: #ebebeb;
border-radius: 50px;
overflow: hidden;
.un_xg_1_3_1 {
float: right;
height: .24rem;
border-radius: 50px;
background-image: linear-gradient(to right, #ff6666);
}
}
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue2.x如何解決Element組件el-tooltip滾動時錯位不消失的問題
這篇文章主要介紹了Vue2.x如何解決Element組件el-tooltip滾動時錯位不消失的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue Element前端應用開發(fā)之常規(guī)的JS處理函數(shù)
在我們使用Vue Element處理界面的時候,往往碰到需要利用JS集合處理的各種方法,如Filter、Map、reduce等方法,也可以涉及到一些對象屬性賦值等常規(guī)的處理或者遞歸的處理方法,本篇隨筆列出一些在VUE+Element 前端開發(fā)中經(jīng)常碰到的JS處理場景,供參考學習。2021-05-05
vue實現(xiàn)下拉框的多選功能(附后端處理參數(shù))
本文介紹了如何使用Vue實現(xiàn)下拉框的多選功能,實現(xiàn)了在選擇框中選擇多個選項的功能,文章詳細介紹了實現(xiàn)步驟和示例代碼,對于想要了解如何使用Vue實現(xiàn)下拉框多選功能的讀者具有一定的參考價值2023-08-08
Vue導入excel文件的兩種方式(form表單和el-upload)
最近開發(fā)遇到一個點擊導入按鈕讓excel文件數(shù)據(jù)導入的需求,下面這篇文章主要給大家介紹了關于Vue導入excel文件的兩種方式,分別是form表單和el-upload兩種方法,需要的朋友可以參考下2022-11-11
vue3+vite中使用import.meta.glob的操作代碼
在vue2的時候,我們一般引入多個js或者其他文件,一般使用? require.context 來引入多個不同的文件,但是vite中是不支持 require的,他推出了一個功能用import.meta.glob來引入多個,單個的文件,下面通過本文介紹vue3+vite中使用import.meta.glob,需要的朋友可以參考下2022-11-11

