成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

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

用戶84 / 593人閱讀

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

效果圖:

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

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

1. 原理

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

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

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

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

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

具體的過程請(qǐng)看代碼:

/*
* 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("請(qǐng)輸入掛載節(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í)行動(dòng)畫
     * @param progress 底部的 dom 對(duì)象
     * @param bar 占比的 dom 對(duì)象
     * @param text 文字的 dom 對(duì)象
     * @param target 目標(biāo)值( Number )
     * @param step 動(dòng)畫步長(zhǎng)( 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);
    },
  };

  // 注冊(cè)到 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
個(gè)人網(wǎng)站 :http://biaochenxuying.cn/main.html

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

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

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/102155.html

相關(guān)文章

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

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

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

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

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

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

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

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

    cfanr 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<