基于JavaScript實(shí)現(xiàn)永遠(yuǎn)加載不滿的進(jìn)度條
前言
各位開發(fā)大佬,平時肯定見到過這種進(jìn)度條吧,一直在加載,但等了好久都是在99%
如下所示:

有沒有好奇這個玩意兒咋做的呢?細(xì)聽分說 (需要看使用:直接看實(shí)踐即可)
fake-progress
如果需要實(shí)現(xiàn)上面的這個需求,其實(shí)會涉及到fake-progress這個庫,具體是干嘛的呢?
這個庫會提供一個構(gòu)造函數(shù),創(chuàng)建一個實(shí)例對象后,里面的屬性會給我們進(jìn)度條需要的數(shù)據(jù)等信息。
如圖所示:

fake-progress庫的源碼如下:
/**
* Represents a fakeProgress
* @constructor
* @param {object} options - options of the contructor
* @param {object} [options.timeConstant=1000] - the timeConstant in milliseconds (see https://en.wikipedia.org/wiki/Time_constant)
* @param {object} [options.autoStart=false] - if true then the progress auto start
*/
const FakeProgress = function (opts) {
if (!opts) {
opts = {};
}
// 時間快慢
this.timeConstant = opts.timeConstant || 1000;
// 自動開始
this.autoStart = opts.autoStart || false;
this.parent = opts.parent;
this.parentStart = opts.parentStart;
this.parentEnd = opts.parentEnd;
this.progress = 0;
this._intervalFrequency = 100;
this._running = false;
if (this.autoStart) {
this.start();
}
};
/**
* Start fakeProgress instance
* @method
*/
FakeProgress.prototype.start = function () {
this._time = 0;
this._intervalId = setInterval(
this._onInterval.bind(this),
this._intervalFrequency
);
};
FakeProgress.prototype._onInterval = function () {
this._time += this._intervalFrequency;
this.setProgress(1 - Math.exp((-1 * this._time) / this.timeConstant));
};
/**
* Stop fakeProgress instance and set progress to 1
* @method
*/
FakeProgress.prototype.end = function () {
this.stop();
this.setProgress(1);
};
/**
* Stop fakeProgress instance
* @method
*/
FakeProgress.prototype.stop = function () {
clearInterval(this._intervalId);
this._intervalId = null;
};
/**
* Create a sub progress bar under the first progres
* @method
* @param {object} options - options of the FakeProgress contructor
* @param {object} [options.end=1] - the progress in the parent that correspond of 100% of the child
* @param {object} [options.start=fakeprogress.progress] - the progress in the parent that correspond of 0% of the child
*/
FakeProgress.prototype.createSubProgress = function (opts) {
const parentStart = opts.start || this.progress;
const parentEnd = opts.end || 1;
const options = Object.assign({}, opts, {
parent: this,
parentStart: parentStart,
parentEnd: parentEnd,
start: null,
end: null,
});
const subProgress = new FakeProgress(options);
return subProgress;
};
/**
* SetProgress of the fakeProgress instance and updtae the parent
* @method
* @param {number} progress - the progress
*/
FakeProgress.prototype.setProgress = function (progress) {
this.progress = progress;
if (this.parent) {
this.parent.setProgress(
(this.parentEnd - this.parentStart) * this.progress + this.parentStart
);
}
};我們需要核心關(guān)注的參數(shù)只有timeConstant,autoStart這兩個參數(shù),通過閱讀源碼可以知道timeConstant相當(dāng)于分母,分母越大則加的越少,而autoStart則是一個開關(guān),如果開啟了直接執(zhí)行start方法,開啟累計(jì)的定時器。通過這個庫,我們實(shí)現(xiàn)一個虛擬的進(jìn)度條,永遠(yuǎn)到達(dá)不了100%的進(jìn)度條。
但是如果這時候像接口數(shù)據(jù)或其他什么資源加載完了,要到100%了怎么辦呢?可以看到代碼中有end()方法,因此顯示的調(diào)用下實(shí)例的end()方法即可。
實(shí)踐
上面講了這么多下面結(jié)合圓形進(jìn)度條(后面再出個手寫圓形進(jìn)度條)來實(shí)操一下,效果如下:

代碼如下所示:
<template>
<div ref="main" class="home">
</br>
<div>{{ fake.progress }}</div>
</br>
<Progress type="circle" :percentage="parseInt(fake.progress*100)"/>
</br></br>
<el-button @click="stop">停止</el-button>
</br></br>
<el-button @click="close">關(guān)閉</el-button>
</div>
</template>
<script>
import FakeProgress from "fake-progress";
export default {
data() {
return {
fake: new FakeProgress({
timeConstant : 6000,
autoStart : true
})
};
},
methods:{
close() {
this.fake.end()
},
stop() {
this.fake.stop()
}
},
};
</script>總結(jié)
如果需要實(shí)現(xiàn)一個永遠(yuǎn)不滿的進(jìn)度條,那么你可以借助fake-progress核心是1 - Math.exp((-1 * this._time) / this.timeConstant) 這個公式
涉及到一個數(shù)據(jù)公式: e的負(fù)無窮次方 趨近于0。所以1-e^-x永遠(yuǎn)到不了1,但趨近于1
核心原理就是:用時間做分子,傳入的timeConstant做分母,通過Math.exp((-1 * this._time) / this.timeConstant) 可知,如果時間不斷累積且為負(fù)值,那么Math.exp((-1 * this._time) / this.timeConstant) 就無限趨近于0。所以1 - Math.exp((-1 * this._time) / this.timeConstant) 就可以得到無限趨近于1 的值
總結(jié),如果需要使用的話,在使用的地方創(chuàng)建一個實(shí)例即可(配置autoStart之后就會自動累加):
new FakeProgress({
timeConstant : 6000,
autoStart : true
})如果需要操作停止或介紹使用其實(shí)例下的對應(yīng)方法即可
this.fake.end() this.fake.stop()
以上就是基于JavaScript實(shí)現(xiàn)永遠(yuǎn)加載不滿的進(jìn)度條的詳細(xì)內(nèi)容,更多關(guān)于JavaScript進(jìn)度條的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解JavaScript中if語句優(yōu)化和部分語法糖小技巧推薦
在前端日常開發(fā)過程中,if?else判斷語句使用的次數(shù)應(yīng)該是比較頻繁的了,一些較為復(fù)雜的場景,可能會用到很多判斷,本文給大家介紹JavaScript中if語句優(yōu)化和部分語法糖小技巧,感興趣的朋友一起看看吧2022-05-05
Bootstrap教程JS插件滾動監(jiān)聽學(xué)習(xí)筆記分享
這篇文章主要為大家分享了Bootstrap教程JS插件滾動監(jiān)聽學(xué)習(xí)筆記,內(nèi)容很詳細(xì),感興趣的小伙伴們可以參考一下2016-05-05
JavaScript中的閉包(Closure)詳細(xì)介紹
這篇文章主要介紹了JavaScript中的閉包(Closure)詳細(xì)介紹,函數(shù)調(diào)用對象與變量的作用域鏈、什么是閉包等內(nèi)容,并給出了實(shí)例,需要的朋友可以參考下2014-12-12
js/jquery控制頁面動態(tài)加載數(shù)據(jù) 滑動滾動條自動加載事件的方法
下面小編就為大家?guī)硪黄猨s/jquery控制頁面動態(tài)加載數(shù)據(jù) 滑動滾動條自動加載事件的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

