HTML5中的进度条简介

    在Web开发中,进度条是很常见的一种表示工作进度的方式。在过往的Web设计中,都必须使用第三方类库等去实现进度条。而在HTML5的世界中,已经在不少的浏览器的内置实现了进度条。在本文中,将讲解如何在页面中使用HTML5的进度条,目前HTML5的进度条只支持在Firefox,Chrome和Opera中得到支持,在IE和Safari中还没得到支持。
 

为了演示方便,在第一个例子中,我们通过Javascript去控制时间从而不断地更新进度,并且允许用户通过点击按钮开始进度条的更新,一旦进度条开始更新,则按钮变得不可点击。如果进度条完成的话,则又可以点击按钮,整个进度条在Firefox的效果如下图:

下面我们开始一步步实做这个效果:

1)创建HTML5 页

首先创建基本的HTML 5基本框架页

   
   
  1.    <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <script type="text/javascript"> 
  5.  
  6. </script> 
  7. </head> 
  8. <body> 
  9.  
  10. </body> 
  11. </html> 

2) 增加进度条标签

首先,在body部分,添加如下的进度条的标签:

   
   
  1. <p>Task progress:</p> 
  2. progress id="prog" value="0" max="100"></progress> 

HTML 5中的进度条使用的是<progress>标签,在这里,设置了开始的值value为0,最大的值为100,当任务完成后,进度条的值就会变成100了。我们将通过Javascript去更新这个值,所以以上的标签只是对进度条进行了初始化工作。

3) 点击按钮的编写
现在,我们开始编写点击按钮的事件,代码如下:

   
   
  1. <input id="startBtn" type="button" value="start" onclick="startProgress()"/> 
  2. <div id="numValue">0%</div> 

其中startProcess()的代码如下:

   
   
  1. //当前进度  
  2. var currProgress = 0;  
  3. //进度条是否完成  
  4. var done = false;  
  5. //进度条计数的最大数值  
  6. var total = 100;  

在声明了上面的变量后,就可以编写startProgress()方法了,代码如下:

   
     
     
  1. function startProgress() { 
  2.  
  3.  //获得进度条的标签 
  4. var prBar = document.getElementById("prog"); 
  5. //获得开始按键 
  6. var startButt = document.getElementById("startBtn"); 
  7. //显示的进度百分比数值 
  8. var val = document.getElementById("numValue"); 

接下来,当用户点了开始按钮后,需要将按钮设置为不可用,并且要更新进度条的数值:

    
    
  1. startButt.disabled=true
  2.  
  3. prBar.value = currProgress; 

并且要显示出进度条当前完成的百分比并显示出来,使用如下的代码:

   
   
  1. val.innerHTML =Math.round((currProgress/total)*100)+"%";

接着就可以对进度条的数字进行累加了:

   
   
  1. currProgress++; 

并且要判断如果进度数值达到100的话,则停止,设置done=false的标识,否则每0.1秒通过Javascript的setimeout方法进行延时,如下:

   
   
  1.  if(currProgress>100) done=true
  2. //如果还没到进度条100的数值,则继续累加 
  3. if(!done) 
  4.     setTimeout("startProgress()", 100); 
  5.  
  6. //如果进度条已经达到100的数值,则重新设置按钮可用,重新设置currProgrss=0 
  7. else     
  8.     document.getElementById("startBtn").disabled = false
  9.     done = false
  10.     currProgress = 0; 

最后完成的代码如下:

   
   
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <title>Developer Drive | Displaying the Progress of Tasks with HTML5 | Demo</title> 
  5. <script type="text/javascript"> 
  6. var currProgress = 0
  7. var done = false
  8. var total = 100
  9.  
  10. function startProgress() { 
  11. var prBar = document.getElementById("prog"); 
  12. var startButt = document.getElementById("startBtn"); 
  13. var val = document.getElementById("numValue"); 
  14. startButt.disabled=true
  15. prBar.value = currProgress
  16. val.innerHTML = Math.round((currProgress/total)*100)+"%"; 
  17.  
  18. currProgress++; 
  19. if(currProgress>100) done=true
  20. if(!done) 
  21.     setTimeout("startProgress()", 100); 
  22. else     
  23.     document.getElementById("startBtn").disabled = false
  24.     done = false
  25.     currProgress = 0
  26. </script> 
  27. </head> 
  28. <body> 
  29.  
  30. <p>This is a demo to accompany the following tutorial: <a href="http://www.developerdrive.com/2012/07/displaying-the-progress-of-tasks-with-html5">Displaying the Progress of Tasks with HTML5</a></p><hr/> 
  31.  
  32. <p>Task progress:</p> 
  33. <progress id="prog" value="0" max="100"></progress>  
  34. <input id="startBtn" type="button" value="start" onclick="startProgress()"/> 
  35. <div id="numValue">0%</div> 
  36.  
  37. </body> 
  38. </html>
非常精美的h5 进度条 |DEMO_jQuery之家-自由分享jQuery、html5、css3的插件库 <!----> .ClassyCountdownDemo { margin:0 auto 30px auto; max-width:800px; width:calc(100%); padding:30px; display:block } #countdown2 { background:#FFF } #countdown3 { background:rgb(52, 73, 94) } #countdown4 { background:#222 } #countdown5 { background:#222 } #countdown6 { background:#222 } #countdown7 { background:#222 } #countdown8 { background:#222 } #countdown9 { background:#FFF } #countdown10 { background:#3498db } jQuery炫酷图片预览Lightbox插件 A jQuery plugin designed to provide gallery view for images jQuery之家 返回下载页 Example $(document).ready(function() { $('#countdown15').ClassyCountdown({ theme: "flat-colors", end: $.now() + 10000 }); $('#countdown16').ClassyCountdown({ theme: "flat-colors-wide", end: $.now() + 10000 }); $('#countdown17').ClassyCountdown({ theme: "flat-colors-very-wide", end: $.now() + 10000 }); $('#countdown18').ClassyCountdown({ theme: "flat-colors-black", end: $.now() + 10000 }); $('#countdown1').ClassyCountdown({ theme: "white", end: $.now() + 645600 }); $('#countdown5').ClassyCountdown({ theme: "white", end: $.now() + 10000 }); $('#countdown6').ClassyCountdown({ theme: "white-wide", end: $.now() + 10000 }); $('#countdown7').ClassyCountdown({ theme: "white-very-wide", end: $.now() + 10000 }); $('#countdown8').ClassyCountdown({ theme: "white-black", end: $.now() + 10000 }); $('#countdown11').ClassyCountdown({ theme: "black", style: { secondsElement: { gauge: { fgColor: "#F00" } } }, end: $.now() + 10000 }); $('#countdown12').ClassyCountdown({ theme: "black-wide", labels: false, end: $.now() + 10000 }); $('#countdown13').ClassyCountdown({ theme: "black-very-wide", labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown14').ClassyCountdown({ theme: "black-black", labelsOptions: { style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown4').ClassyCountdown({ end: $.now() + 10000, labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, hours: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, minutes: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, seconds: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown2').ClassyCountdown({ end: '1388468325', now: '1378441323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown9').ClassyCountdown({ end: '1388468325', now: '1380501323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#1abc9c", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#2980b9", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#8e44ad", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#f39c12", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown10').ClassyCountdown({ end: '1397468325', now: '1388471324', labels: true, labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown3').ClassyCountdown({ end: '1390868325', now: '1388461323', labels: true, labelsOptions: { lang: { days: 'Zile', hours: 'Ore', minutes: 'Minute', seconds: 'Secunde' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' } }, onEndCallback: function() { console.log("Time out!"); } }); }); 如果你喜欢这个插件,那么你可能也喜欢: html5+jquery通过鼠标控制的圆形进度条 jQuery和css3旋钮控制按钮-knobKnob
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值