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

有沒有好奇這個(gè)玩意兒咋做的呢?細(xì)聽分說 (需要看使用:直接看實(shí)踐即可)
fake-progress
如果需要實(shí)現(xiàn)上面的這個(gè)需求,其實(shí)會(huì)涉及到fake-progress這個(gè)庫,具體是干嘛的呢?
這個(gè)庫會(huì)提供一個(gè)構(gòu)造函數(shù),創(chuàng)建一個(gè)實(shí)例對(duì)象后,里面的屬性會(huì)給我們進(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 = {};
}
// 時(shí)間快慢
this.timeConstant = opts.timeConstant || 1000;
// 自動(dòng)開始
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這兩個(gè)參數(shù),通過閱讀源碼可以知道timeConstant相當(dāng)于分母,分母越大則加的越少,而autoStart則是一個(gè)開關(guān),如果開啟了直接執(zhí)行start方法,開啟累計(jì)的定時(shí)器。通過這個(gè)庫,我們實(shí)現(xiàn)一個(gè)虛擬的進(jìn)度條,永遠(yuǎn)到達(dá)不了100%的進(jìn)度條。
但是如果這時(shí)候像接口數(shù)據(jù)或其他什么資源加載完了,要到100%了怎么辦呢?可以看到代碼中有end()方法,因此顯示的調(diào)用下實(shí)例的end()方法即可。
實(shí)踐
上面講了這么多下面結(jié)合圓形進(jìn)度條(后面再出個(gè)手寫圓形進(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)一個(gè)永遠(yuǎn)不滿的進(jìn)度條,那么你可以借助fake-progress核心是1 - Math.exp((-1 * this._time) / this.timeConstant) 這個(gè)公式
涉及到一個(gè)數(shù)據(jù)公式: e的負(fù)無窮次方 趨近于0。所以1-e^-x永遠(yuǎn)到不了1,但趨近于1
核心原理就是:用時(shí)間做分子,傳入的timeConstant做分母,通過Math.exp((-1 * this._time) / this.timeConstant) 可知,如果時(shí)間不斷累積且為負(fù)值,那么Math.exp((-1 * this._time) / this.timeConstant) 就無限趨近于0。所以1 - Math.exp((-1 * this._time) / this.timeConstant) 就可以得到無限趨近于1 的值
總結(jié),如果需要使用的話,在使用的地方創(chuàng)建一個(gè)實(shí)例即可(配置autoStart之后就會(huì)自動(dòng)累加):
new FakeProgress({
timeConstant : 6000,
autoStart : true
})如果需要操作停止或介紹使用其實(shí)例下的對(duì)應(yīng)方法即可
this.fake.end() this.fake.stop()
以上就是基于JavaScript實(shí)現(xiàn)永遠(yuǎn)加載不滿的進(jìn)度條的詳細(xì)內(nèi)容,更多關(guān)于JavaScript進(jìn)度條的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript中日常收集常見的10種錯(cuò)誤(推薦)
本文是小編給大家日常收集整理的js中常見的10種錯(cuò)誤,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
JavaScript手寫源碼之omit函數(shù)的實(shí)現(xiàn)
最近突然有個(gè)新的想法,想去看看前端的小庫來提升自己的編碼能力。但是又不知道怎么去證明自己是否真的看懂了,那就實(shí)現(xiàn)一個(gè)omit函數(shù)吧2023-02-02
JS實(shí)現(xiàn)掃雷項(xiàng)目總結(jié)
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)掃雷項(xiàng)目總結(jié),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
讓bootstrap的carousel支持滑動(dòng)滾屏的實(shí)現(xiàn)代碼
這篇文章主要介紹了讓bootstrap的carousel支持滑動(dòng)滾屏的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-11-11
js統(tǒng)計(jì)頁面的來訪次數(shù)實(shí)現(xiàn)代碼
這篇文章主要介紹了如何使用js統(tǒng)計(jì)頁面的來訪次數(shù),需要的朋友可以參考下2014-05-05
Flexigrid在IE下不顯示數(shù)據(jù)的處理的解決方法
Flexigrid在IE下不顯示數(shù)據(jù)的情況,想必大家都有遇到過吧,下面有個(gè)不錯(cuò)的解決方法,感興趣的朋友可以參考下2013-10-10
JavaScript 選中文字并響應(yīng)獲取的實(shí)現(xiàn)代碼
當(dāng)鼠標(biāo)選擇一段文字時(shí),對(duì)這個(gè)事件產(chǎn)生響應(yīng),并且將選中的文字傳遞出去。2011-08-08

