超讚的CSS3進度條 能夠隨進度顯示不一樣顏色

如今的WEB已經不是之前的WEB了,傳輸更大的數據量,有着更加複雜的計算,這就須要利用進度條來提升用戶體驗,必要時能夠讓用戶耐心等待,不至於因操做卡了而關掉你的網頁。前幾天我在網上看到一款和通常進度條不太同樣的玩意,它的外觀是一條直線,末端有個小球,在進度變化時能夠顯示數字百分比,更能夠用不一樣顏色來表示當前進度的狀態。來看看效果圖。css

一看進度條外觀,還不錯吧。html

同時,咱們也能夠在這裏看到進度條的DEMO演示html5

接下來咱們來分析一下這款HTML5進度條的實現源碼,篇幅有限,咱們只挑核心的代碼來講。css3

HTML代碼很簡單,構造一個進度條容器和數字百分比容器:web

<div id="wrapper">
<div class="loader-container">
  <div class="meter">0</div>
  <span class="runner"></span>
</div>
</div>

首先咱們來對進度條的容器進行樣式渲染,利用CSS3的漸變屬性來實現不一樣進度變換顏色的效果:app

.loader-container {
  height: 6px;
  width: 600px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -3px;
  margin-left: -300px;
  background-color: transparent;
  background-image: -webkit-linear-gradient(left, #5bd8ff, #ff0000);
  background-image: -moz-linear-gradient(left, #5bd8ff, #ff0000);
  background-image: -o-linear-gradient(left, #5bd8ff, #ff0000);
  background-image: -ms-linear-gradient(left, #5bd8ff, #ff0000);
  background-image: linear-gradient(left, #5bd8ff, #ff0000);
  box-shadow: inset 0 -2px 2px rgba(0, 0, 0, 0.4);
  border-radius: 3px 0 0 3px;
}
.loader-container:after {
  content: "";
  display: block;
  position: absolute;
  right: 0;
  top: 50%;
  width: 1em;
  height: 1em;
  border-radius: 50%;
  margin-top: -0.5em;
  margin-right: -1em;
  background-image: -webkit-linear-gradient(top, #000000, #212121);
  background-image: -moz-linear-gradient(top, #000000, #212121);
  background-image: -o-linear-gradient(top, #000000, #212121);
  background-image: -ms-linear-gradient(top, #000000, #212121);
  background-image: linear-gradient(top, #000000, #212121);
}

接下來是末端小圓球的樣式:this

.loader-container.done:after {
  background: Red;
}
.run .runner {
  content: "";
  position: absolute;
  right: 0;
  height: 100%;
  width: 0%;
  background-color: transparent;
  background-image: -webkit-linear-gradient(top, #000000, #212121);
  background-image: -moz-linear-gradient(top, #000000, #212121);
  background-image: -o-linear-gradient(top, #000000, #212121);
  background-image: -ms-linear-gradient(top, #000000, #212121);
  background-image: linear-gradient(top, #000000, #212121);
  animation: loader 10s linear;
}

這裏也是利用的CSS3的漸變屬性。spa

而後是數字百分比的樣式屬性,這裏隨着進度變化,數字百分比的顏色也會發生變化。、code

.meter {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 2em;
  margin-top: .3em;
  color: #ff0000;
  animation: meter 10s linear;
  text-shadow: 0 -1px 0 #333333;
}
.meter:after {
  content: "%";
}

最後,咱們再來看看JS代碼,其實js要完成的工做很是簡單,只須要將CSS3渲染好的進度條動起來就好,看代碼:htm

var Loader = function () {    
  var loader = document.querySelector('.loader-container'),
      meter = document.querySelector('.meter'),
      k, i = 1,
      counter = function () {
        if (i <= 100) {   
          meter.innerHTML = i.toString();
          i++;
        } else {
          window.clearInterval(k);
        }
      };

    return {
      init: function (options) {
      options = options || {};
      var time = options.time ? options.time : 0,
            interval = time/100;
      
        loader.classList.add('run');
      k = window.setInterval(counter, interval); 
      setTimeout(function () {        
          loader.classList.add('done');
      }, time);
    },
  }
}();

Loader.init({
      // If you have changed the @time in LESS, update this number to the corresponding value. Measured in miliseconds.
      time: 10000
});

初次寫博客,能夠代碼分析的不是很好,有什麼問題能夠留言指正,不過,這款進度條我的真的很喜歡,老外的創意真是無限啊。

相關文章
相關標籤/搜索