基礎進度條
通用html代碼
<div id="box">
<div id="boxBar">0%</div>
<div id="boxText">0%</div>
</div>
通用方法
function progressFn(cent) { //cent傳百分比
var box = document.getElementById('box')
var box2 = document.getElementById('box2');
var boxBar = document.getElementById('boxBar')
var boxText = document.getElementById('boxText')
var allWidth = parseInt(getStyle(box, 'width')); //500
boxBar.innerHTML = cent + '%'
boxText.innerHTML = cent + '%'
boxBar.style.clip = 'rect(0px,' + (cent / 100) * allWidth + 'px,40px,0px)';//改變第一層的寬度
}
通用css
#box{ width: 500px; height: 40px; border: 1px solid #c8c8c8; background: #fff; position: relative; margin-left: auto; margin-right: auto;}
#boxBar{ position: absolute; left: 0; top: 0; z-index: 2; height: 40px; width: 100%; line-height: 40px; color: #ffffff; text-align: center; font-size: 20px; font-weight: bold; clip: rect(0px,0,40px,0px); background: #00a1f5;}
#boxText{ position: absolute; left: 0; top: 0; z-index: 1; width: 100%; height: 40px; line-height: 40px; color: #000000; text-align: center; font-size: 20px; font-weight: bold;}
下面是硬編碼方式例子
<div id="box2" style="display: none;">
<img src="http://b.zol-img.com.cn/desk/bizhi/image/6/1920x1080/1436965788437.jpg" rel="nofollow"/>
<img src="http://attach.bbs.miui.com/forum/201405/24/093305l2tuzftkogegn1if.jpg" rel="nofollow"/>
<img src="http://img.ivsky.com/img/bizhi/pic/201506/26/dal_shabet-005.jpg" rel="nofollow"/>
<img src="http://i1.shouyou.itc.cn/2014/news/2014/09/11/1920x1080bz0912_06.jpg" rel="nofollow"/>
<img src="http://i2.download.fd.pchome.net/g1/M00/10/18/ooYBAFWyD6WIMSmsAAeRvXnu1PwAACnFwIjKkkAB5HV807.jpg" rel="nofollow"/>
</div>
//硬編碼方式
var box = document.getElementById('box')
var box2 = document.getElementById('box2');
var img = box2.getElementsByTagName('img');
var iNow = 0; //當前百分比
for (var i = 0; i < img.length; i++) {
(function (i) {
var yimg = new Image();//圖片預加載對象 //頁面在window.onload之後執行,若生效則不加window.onload
yimg.onload = function () {
iNow++;
progressFn(parseInt(iNow / img.length * 100));
if (iNow == img.length) {
box2.style.display = 'block'
box.style.display = '';
}
}
yimg.src = img[i].src;
})(i)
}