因业务需求,用户自己的网速很慢,需要在页面加一个加载的百分比数据条
在html头部
<script type="text/javascript">
var LoadingHtml = '<div class="wrapper progress_out" id="loadingDiv" style="position:relative;">'+
'<div class="progress_box" style="position:fixed;left:50%;margin-left:-43%;top:50%;margin-top:-20px;z-index:999;">'+
'<div class="progress_inbox">'+
'<div class="progress_bar" id="bar" style="width: 0%"></div>'+
'<label id="total">0%</label>'+
'</div>'+
'</div>'+
'</div>';
document.write(LoadingHtml);
document.onreadystatechange = completeLoading;
//加载状态为complete时移除loading效果
function completeLoading() {
if (document.readyState == "complete") {
setTimeout(function () {
var loadingMask = document.getElementById('loadingDiv');
loadingMask.parentNode.removeChild(loadingMask);
},100)
}
}
function run(){
var bar = document.getElementById("bar");
var total = document.getElementById("total");
if((bar!=null) && bar.style.width == "99%"){
window.clearTimeout(timeout);
return;
}
if((bar!=null) && document.readyState == "complete"){
bar.style.width = "100%";
total.innerHTML = bar.style.width;
total.style.position = 'absolute';
total.style.left = bar.style.width;
total.style.top = '0';
}else if((bar!=null)){
bar.style.width=parseInt(bar.style.width) + 1 + "%";
total.innerHTML = bar.style.width;
total.style.position = 'absolute';
total.style.left = bar.style.width;
total.style.top = '0';
}
timeout=window.setTimeout("run()",100);
}
run();
<!--加载条end -->
</script>
因为无法控制加载速度,只好做了个百分之99就不动了,等到页面加载完成再直接变为100%;第二种是加载条数据还在加载,页面已加载完成,把数据长度和数直接变为100%;功能待完善