练习7_进度条.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .progress-border{
  9. width: 600px;
  10. height: 100px;
  11. border:4px solid black;
  12. border-radius: 10px;
  13. position: relative;
  14. overflow: hidden;
  15. }
  16. .progress-bar{
  17. width: 0;
  18. height: 100px;
  19. background-color: blue;
  20. position: absolute;
  21. top: 0;
  22. left: 0;
  23. }
  24. .progress-border .progress-num{
  25. position: absolute;
  26. top: 0;
  27. left: 50px;
  28. z-index: 1;
  29. font-size: 40px;
  30. font-weight: bolder;
  31. height: 100px;
  32. line-height: 100px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="progress-content">
  38. <div class="progress-border">
  39. <div class="progress-num">0%</div>
  40. <div class="progress-bar"></div>
  41. </div>
  42. <button id="btn">start</button>
  43. </div>
  44. <script>
  45. var oNum = document.getElementsByClassName("progress-num")[0];
  46. var oBar = document.getElementsByClassName("progress-bar")[0];
  47. var oBtn = document.getElementById("btn");
  48. var i = 0;
  49. var timer = null;
  50. oBtn.onclick = function(){
  51. // oBar.style.width = "10px";
  52. timer = setInterval(function(){
  53. // if(oBar.offsetWidth >= 600){
  54. // }
  55. if(i >= 100){
  56. clearInterval(timer);
  57. }else{
  58. oBar.style.width = oBar.offsetWidth + 6 + "px";
  59. oNum.innerText = ++i + "%";
  60. }
  61. },16);
  62. }
  63. </script>
  64. </body>
  65. </html>