JavaScript实现一个简单的进度条(有进度含百分比)

傷城~ 2021-07-24 11:14 503阅读 0赞

效果图:

20201217201811429.png

代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=GBK">
  5. <style>
  6. *{margin:0;padding:0;}
  7. .box{
  8. width: 300px;
  9. height:10px;
  10. border:1px solid #9e9e9e;
  11. }
  12. .load{
  13. width:0px;
  14. height:10px;
  15. background:#325976;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div style="left:40%;position:absolute;top: 20%;font-size:70px">
  21. <div class="box" id="div_box"><div class="load" id="load"></div></div>
  22. <span id='result'></span>
  23. </div>
  24. </body>
  25. <script>
  26. var speed=110;
  27. var n=0;
  28. var result = document.getElementById('result');
  29. var load = document.getElementById('load');
  30. var timer = setInterval(function(){
  31. if(n<=100){
  32. n+=Math.floor(Math.round(Math.random()*(100-10)+10)/10);// 这里采用随机的两位数来模拟
  33. if(n>100)n=100;
  34. result.innerText=n+"%";
  35. load.style.width=n*3+'px';
  36. }else{
  37. clearInterval(timer);
  38. }
  39. },speed)
  40. </script>
  41. </html>

发表评论

表情:
评论列表 (有 0 条评论,503人围观)

还没有评论,来说两句吧...

相关阅读