成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

原生 js 實(shí)現(xiàn)一個有動畫效果的進(jìn)度條插件 progress

用戶84 / 555人閱讀

摘要:效果圖項(xiàng)目地址效果體驗(yàn)地址原理一個用于裝載進(jìn)度條內(nèi)容的且叫做。然后在里面動態(tài)生成三個元素,一個是做為背景的且叫做,一個是做為顯示進(jìn)度的且叫做,還有一個是顯示文字的且叫做。的寬從逐漸增加到的值的過程比如,給這個過程添加一個逐漸加快的動畫。

效果圖:

項(xiàng)目地址:https://github.com/biaochenxuying/progress

效果體驗(yàn)地址: https://biaochenxuying.github.io/progress/index.html

1. 原理

一個用于裝載進(jìn)度條內(nèi)容的 div (且叫做 container)。

然后在 container 里面動態(tài)生成三個元素,一個是做為背景的 div (且叫做 progress),一個是做為顯示進(jìn)度的 div (且叫做 bar),還有一個是顯示文字的 span (且叫做 text)。

progress 的寬為 100%,bar 的寬根據(jù)傳入數(shù)值 target 的值來定( 默認(rèn)為 0 ,全部占滿的值為 100 ),text 展示的文字為 bar 的寬占 progress 寬的百分比。

bar 的寬從 0 逐漸增加到的 target 值的過程( 比如: 0 > 80 ),給這個過程添加一個逐漸加快的動畫。

2. 代碼實(shí)現(xiàn)

具體的過程請看代碼:

/*
* author: https://github.com/biaochenxuying
*/

(function() {
  function Progress() {
    this.mountedId = null;
    this.target = 100;
    this.step = 1;
    this.color = "#333";
    this.fontSize = "18px";
    this.borderRadius = 0;
    this.backgroundColor = "#eee";
    this.barBackgroundColor = "#26a2ff";
  }

  Progress.prototype = {
    init: function(config) {
      if (!config.mountedId) {
        alert("請輸入掛載節(jié)點(diǎn)的 id");
        return;
      }

      this.mountedId = config.mountedId;
      this.target = config.target || this.target;
      this.step = config.step || this.step;
      this.fontSize = config.fontSize || this.fontSize;
      this.color = config.color || this.color;
      this.borderRadius = config.borderRadius || this.borderRadius;
      this.backgroundColor = config.backgroundColor || this.backgroundColor;
      this.barBackgroundColor =
        config.barBackgroundColor || this.barBackgroundColor;

      var box = document.querySelector(this.mountedId);
      var width = box.offsetWidth;
      var height = box.offsetHeight;
      var progress = document.createElement("div");
      progress.style.position = "absolute";
      progress.style.height = height + "px";
      progress.style.width = width + "px";
      progress.style.borderRadius = this.borderRadius;
      progress.style.backgroundColor = this.backgroundColor;

      var bar = document.createElement("div");
      bar.style.float = "left";
      bar.style.height = "100%";
      bar.style.width = "0";
      bar.style.lineHeight = height + "px";
      bar.style.textAlign = "center";
      bar.style.borderRadius = this.borderRadius;
      bar.style.backgroundColor = this.barBackgroundColor;

      var text = document.createElement("span");
      text.style.position = "absolute";
      text.style.top = "0";
      text.style.left = "0";
      text.style.height = height + "px";
      text.style.lineHeight = height + "px";
      text.style.fontSize = this.fontSize;
      text.style.color = this.color;

      progress.appendChild(bar);
      progress.appendChild(text);
      box.appendChild(progress);

      this.run(progress, bar, text, this.target, this.step);
    },
    /**
     * @name 執(zhí)行動畫
     * @param progress 底部的 dom 對象
     * @param bar 占比的 dom 對象
     * @param text 文字的 dom 對象
     * @param target 目標(biāo)值( Number )
     * @param step 動畫步長( Number )
     */
    run: function(progress, bar, text, target, step) {
      var self = this;
      ++step;
      var endRate = parseInt(target) - parseInt(bar.style.width);
      if (endRate <= step) {
        step = endRate;
      }
      var width = parseInt(bar.style.width);
      var endWidth = width + step + "%";
      bar.style.width = endWidth;
      text.innerHTML = endWidth;

      if (width >= 94) {
        text.style.left = "94%";
      } else {
        text.style.left = width + 1 + "%";
      }

      if (width === target) {
        clearTimeout(timeout);
        return;
      }
      var timeout = setTimeout(function() {
        self.run(progress, bar, text, target, step);
      }, 30);
    },
  };

  // 注冊到 window 全局
  window.Progress = Progress;
})();
3. 使用方法
  var config = {
    mountedId: "#bar",
    target: 8,
    step: 1,
    color: "green",
    fontSize: "20px",
    borderRadius: "5px",
    backgroundColor: "#eee",
    barBackgroundColor: "red",
  };
  var p = new Progress();
  p.init(config);
4. 最后

筆者的博客后花圓地址如下:

github :https://github.com/biaochenxuying/blog
個人網(wǎng)站 :http://biaochenxuying.cn/main.html

如果您覺得這篇文章不錯或者對你有所幫助,請給個贊唄,你的點(diǎn)贊就是對我最大的鼓勵,謝謝。

微信公眾號:BiaoChenXuYing
分享 前端、后端開發(fā)等相關(guān)的技術(shù)文章,熱點(diǎn)資源,隨想隨感,全棧程序員的成長之路。
關(guān)注公眾號并回復(fù) 福利 便免費(fèi)送你視頻資源,絕對干貨。
福利詳情請點(diǎn)擊: 免費(fèi)資源分享--Python、Java、Linux、Go、node、vue、react、javaScript

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/102155.html

相關(guān)文章

  • 手把手教你實(shí)現(xiàn)一個 Vue 進(jìn)度組件!

    摘要:添加的屬性進(jìn)度條長度顯示和隱藏是否是成功的狀態(tài)有了這些屬性,這個進(jìn)度條就要根據(jù)這些屬性的變化來自己動。大家可以自己動手實(shí)踐一下,起一個項(xiàng)目,使用的聲明周期鉤子,或者寫一個定時器模擬異步來測試一下。 showImg(https://segmentfault.com/img/bVbjDzm?w=900&h=383);最近在個人的項(xiàng)目中,想對頁面之間跳轉(zhuǎn)的過程進(jìn)行優(yōu)化,想到了很多文檔或 np...

    zgbgx 評論0 收藏0
  • javascript功能插件大集合 前端常用插件 js常用插件

    摘要:轉(zhuǎn)載來源包管理器管理著庫,并提供讀取和打包它們的工具。能構(gòu)建更好應(yīng)用的客戶端包管理器。一個整合和的最佳思想,使開發(fā)者能快速方便地組織和編寫前端代碼的下一代包管理器。很棒的組件集合。隱秘地使用和用戶數(shù)據(jù)。 轉(zhuǎn)載來源:https://github.com/jobbole/aw... 包管理器管理著 javascript 庫,并提供讀取和打包它們的工具。?npm – npm 是 javasc...

    netmou 評論0 收藏0
  • javascript功能插件大集合 前端常用插件 js常用插件

    摘要:轉(zhuǎn)載來源包管理器管理著庫,并提供讀取和打包它們的工具。能構(gòu)建更好應(yīng)用的客戶端包管理器。一個整合和的最佳思想,使開發(fā)者能快速方便地組織和編寫前端代碼的下一代包管理器。很棒的組件集合。隱秘地使用和用戶數(shù)據(jù)。 轉(zhuǎn)載來源:https://github.com/jobbole/aw... 包管理器管理著 javascript 庫,并提供讀取和打包它們的工具。?npm – npm 是 javasc...

    Hydrogen 評論0 收藏0
  • javascript功能插件大集合,寫前端親們記得收藏

    摘要:一個專注于瀏覽器端和兼容的包管理器。一個整合和的最佳思想,使開發(fā)者能快速方便地組織和編寫前端代碼的下一代包管理器。完全插件化的工具,能在中識別和記錄模式。健壯的優(yōu)雅且功能豐富的模板引擎。完整的經(jīng)過充分測試和記錄數(shù)據(jù)結(jié)構(gòu)的庫。 【導(dǎo)讀】:GitHub 上有一個 Awesome – XXX 系列的資源整理。awesome-javascript 是 sorrycc 發(fā)起維護(hù)的 JS 資源列表...

    cfanr 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<