HTML5/CSS3超酷进度条 不同进度多种颜色

本文作者html5tricks,转载请注明出处

这是一款线条状的CSS3进度条,这款进度条有两个特点:一是随着进度条的进度更新,都会有数字百分比实时显示,让数据更加直观;二是在不同的进度阶段,进度条的颜色会有所变化,这样能更好的反应当前的进度状态。加上黑色的背景,让这款CSS3进度条更加鲜亮起来。

css3-progress-bar-line

下面我们来看看实现这款进度条的过程和源码,代码主要由HTML、CSS以及jQuery组成,实现过程也相对比较简单。

HTML代码:

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

这里定义了进度条的容器,以及进度百分比。

CSS代码:

.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);
}
.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;
}
.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: "%";
}
@keyframes loader {
  0% {
    width: 100%;
  }
  100% {
    width: 0%;
  }
}
@keyframes meter {
  0% {
    color: #5bd8ff;
  }
  100% {
    color: #ff0000;
  }
}

这里利用了CSS3的动画属性,定义了从0%到100%的动画效果。

jQuery代码:

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
});

用jQuery代码实现了进度条的实时更新,并且百分比数字也实时更新。

在线演示源码下载

热门推荐

HTML5/CSS3超酷进度条 不同进度多种颜色》上有4条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>