首页  ·  知识 ·  前端
用javascript实现进度条
孙钰佳的博客  http://blog.csdn.net/sunyujia/archive/2008/06/15/2  综合  编辑:dezai  图片来源:网络
>>html > <
  1.   
  2.   
  3.        
  4.   
  5.         进度条   
  6.   
  7.   
  8.         "text/css">   
  9.   
  10.             .bgBar{   
  11.   
  12.                 background: #FFFFFF;   
  13.   
  14.                 font-family: Arial,Verdana;   
  15.   
  16.                 border: 1px solid #000000;   
  17.   
  18.                 font-size: 17;   
  19.   
  20.                 font-weight: bold;   
  21.   
  22.             }   
  23.   
  24.             .bgBar div{   
  25.   
  26.                 background: #DAECC8;   
  27.   
  28.                 border: 1px solid #FFFFFF;   
  29.   
  30.                 color:  #308040;   
  31.   
  32.                 text-align: right;   
  33.   
  34.                 overflow: hidden;   
  35.   
  36.             }   
  37.   
  38.            
  39.   
  40.         "text/javascript">   
  41.   
  42.             /****************************************************************************************/  
  43.   
  44.             //下面代码为进度条的封装   
  45.   
  46.             if (syj == nullvar syj = {};   
  47.   
  48.             //进度条,parent进度条的父控件对象,width进度条的宽度,barClass进度条的css,display是否显示进度条   
  49.   
  50.             syj.ProgressBar=function(parent, width , barClass, display) {   
  51.   
  52.                 this.parent=parent;   
  53.   
  54.                 this.pixels = width;   
  55.   
  56.                 this.parent.innerHTML="
    ";   
  57.   
  58.                 this.outerDIV = this.parent.childNodes[0];   
  59.   
  60.                 this.outerDIV.innerHTML="
    ";   
  61.   
  62.                 this.fillDIV = this.outerDIV.childNodes[0];   
  63.   
  64.                 this.fillDIV.innerHTML = "0";   
  65.   
  66.                 this.fillDIV.style.width = "0px";   
  67.   
  68.                 this.outerDIV.className = barClass;   
  69.   
  70.                 this.outerDIV.style.width = (width + 2) + "px";   
  71.   
  72.                 this.parent.style.display = display==false?'none':'';   
  73.   
  74.             }   
  75.   
  76.             //更新进度条进度 pct的值要介于0和1之间   
  77.   
  78.             syj.ProgressBar.prototype.setPercent = function(pct) {   
  79.   
  80.                 var fillPixels;   
  81.   
  82.                 if (pct < 1.0){   
  83.   
  84.                     fillPixels = Math.round(this.pixels * pct);   
  85.   
  86.                 }else {   
  87.   
  88.                     pct = 1.0;   
  89.   
  90.                     fillPixels = this.pixels;   
  91.   
  92.                 }   
  93.   
  94.                 this.fillDIV.innerHTML = Math.round(100 * pct) + "%";   
  95.   
  96.                 this.fillDIV.style.width = fillPixels + "px";   
  97.   
  98.             }   
  99.   
  100.             //控制进度条的 显示/隐藏   
  101.   
  102.             syj.ProgressBar.prototype.display= function(v){   
  103.   
  104.                 this.parent.style.display = v==true?'':'none';   
  105.   
  106.             }   
  107.   
  108.             //初始化进度条   
  109.   
  110.             function init(){   
  111.   
  112.                 window.jtProBar = new syj.ProgressBar(document.getElementById("progressBar"), 450 , "bgBar");   
  113.   
  114.             }   
  115.   
  116.             /****************************************************************************************/  
  117.   
  118.             //下面代码为演示程序   
  119.   
  120.             //开始演示   
  121.   
  122.             function startAutoDemo(){   
  123.   
  124.                 if(window.thread==null)   
  125.   
  126.                     window.thread=window.setInterval("updatePercent()",60);   
  127.   
  128.             }   
  129.   
  130.             //停止演示   
  131.   
  132.             function stopAutoDemo(){   
  133.   
  134.                 window.clearInterval(window.thread);   
  135.   
  136.                 window.thread=null;   
  137.   
  138.             }   
  139.   
  140.             //演示程序   
  141.   
  142.             function updatePercent(){   
  143.   
  144.                 if(window.count==null)window.count=0;   
  145.   
  146.                 window.count=count%100   
  147.   
  148.                 jtProBar.setPercent(window.count/100);   
  149.   
  150.                 window.count++;   
  151.   
  152.             }   
  153.   
  154.             /****************************************************************************************/  
  155.   
  156.            
  157.   
  158.        
  159.   
  160.     "init()">   
  161.   
  162.         "button" value="100%效果" onclick="window.stop=false;jtProBar.setPercent(1);"/>   
  163.   
  164.         "button" value="开始自动演示" onclick="startAutoDemo()"/>   
  165.   
  166.         "button" value="停止自动演示" onclick="stopAutoDemo()"/>   
  167.   
  168.         "button" value="显示" onclick="jtProBar.display(true)"/>   
  169.   
  170.         "button" value="隐藏" onclick="jtProBar.display(false)"/>   
  171.   
  172.            
  173.   
  174.         "progressBar">
  
  •   
  •        
  •   
  •   
  • 本文作者:孙钰佳的博客 来源:http://blog.csdn.net/sunyujia/archive/2008/06/15/2
    CIO之家 www.ciozj.com 微信公众号:imciow
        >>频道首页  >>网站首页   纠错  >>投诉
    版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
    延伸阅读