亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于JavaScript實(shí)現(xiàn)永遠(yuǎn)加載不滿的進(jìn)度條

 更新時(shí)間:2023年04月09日 10:55:43   作者:xiao_shan  
各位開(kāi)發(fā)大佬,平時(shí)肯定見(jiàn)到過(guò)這種進(jìn)度條吧,一直在加載,但等了好久都是在99%,那如何用JavaScript實(shí)現(xiàn)這一效果呢,下面就來(lái)和大家詳細(xì)講講

前言

各位開(kāi)發(fā)大佬,平時(shí)肯定見(jiàn)到過(guò)這種進(jìn)度條吧,一直在加載,但等了好久都是在99%

如下所示:

有沒(méi)有好奇這個(gè)玩意兒咋做的呢?細(xì)聽(tīng)分說(shuō) (需要看使用:直接看實(shí)踐即可)

fake-progress

如果需要實(shí)現(xiàn)上面的這個(gè)需求,其實(shí)會(huì)涉及到fake-progress這個(gè)庫(kù),具體是干嘛的呢?

這個(gè)庫(kù)會(huì)提供一個(gè)構(gòu)造函數(shù),創(chuàng)建一個(gè)實(shí)例對(duì)象后,里面的屬性會(huì)給我們進(jìn)度條需要的數(shù)據(jù)等信息。

如圖所示:

fake-progress庫(kù)的源碼如下:

/**
 * 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)開(kāi)始
  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ù),通過(guò)閱讀源碼可以知道timeConstant相當(dāng)于分母,分母越大則加的越少,而autoStart則是一個(gè)開(kāi)關(guān),如果開(kāi)啟了直接執(zhí)行start方法,開(kāi)啟累計(jì)的定時(shí)器。通過(guò)這個(gè)庫(kù),我們實(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)度條)來(lái)實(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ù)無(wú)窮次方 趨近于0。所以1-e^-x永遠(yuǎn)到不了1,但趨近于1

核心原理就是:用時(shí)間做分子,傳入的timeConstant做分母,通過(guò)Math.exp((-1 * this._time) / this.timeConstant) 可知,如果時(shí)間不斷累積且為負(fù)值,那么Math.exp((-1 * this._time) / this.timeConstant) 就無(wú)限趨近于0。所以1 - Math.exp((-1 * this._time) / this.timeConstant) 就可以得到無(wú)限趨近于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)文章

最新評(píng)論