纯JS progressBar简单模拟

效果图片,兼容IE FireFox Chrome

运行效果

progressbar.html

<html>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<head>
  <title>bar</title>
  <style>
   
   .barWidth{
      width:400px;
    }
		
   .bar{
      background-color:gray;     
      height:20px;
     
   }

   
   .bar0{
    background-color:blue;
    width:0px;
    height:20px;
   }

  .textBar{
    text-align:center;height:20px;line-height:20px;
    position:absolute;
  }
  
   
  </style>
<script src="bar.js"></script>
</head>


<body>

<div id="wq" class="bar barWidth">
<div id="nq" class="bar0"></div>
</div>
<div id="tx" class="textBar barWidth"></div>
<span id="zt">请点击start启动更新程序</span><br/><br/>
<input id="startbtn" type="button" value="start"/>

</body>
</html>

 javascript bar.js

 

function $(id){
   return document.getElementById(id);
}
function getRandomNum(Min,Max)
{   
var Range = Max - Min;   
var Rand = Math.random();   
return(Min + Math.round(Rand * Range));   
}   
 function getTop(e){  
        var offset=e.offsetTop;  
        if(e.offsetParent!=null) offset+=getTop(e.offsetParent);  
        return offset;  
        }  
function getLeft(e){  
        var offset=e.offsetLeft;  
        if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);  
        return offset;  
}  
function clock(){
       var n = getRandomNum(1,5);
      var width = $("nq").offsetWidth;
      var len = width+n;
      if (len>wqLen){
         len = wqLen;
      }
      showBar(len);        
      if (len == wqLen){
	 $("zt").innerHTML="系统更新成功!";    
	 window.clearInterval(int);   
      }
 }
function showBar(len){
     $("nq").style.width = len +"px";
     var percent = parseInt(len/wqLen*100);
     $("tx").innerHTML =  percent +"%";
}
var int,wqLen;
window.onload = function(e){
   var wq = $("wq");
   wqLen = wq.offsetWidth;
   $("tx").style.top = getTop(wq)+"px";
   $("tx").style.left  = getLeft(wq)+"px";
  $("startbtn").onclick = function(){
      $("nq").style.width = 0 +"px";
      $("zt").innerHTML="正在更新系统,请稍候...";
      int=self.setInterval("clock()", 100);
  };
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值