练习7_进度条.html 1.7 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. .box{
  9. width: 600px;
  10. height: 80px;
  11. border:3px solid black;
  12. border-radius: 20px;
  13. overflow: hidden;
  14. position: relative;
  15. }
  16. .progress{
  17. width: 0;
  18. height: 100%;
  19. background-color: #ff6700;
  20. }
  21. .text{
  22. position: absolute;
  23. height: 80px;
  24. line-height: 80px;
  25. left: 20px;
  26. top: 0;
  27. color: green;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="box">
  33. <div class="progress"></div>
  34. <div class="text">0%</div>
  35. </div>
  36. <button>开始</button>
  37. <script>
  38. // 获取按钮
  39. var oBtn = document.getElementsByTagName("button")[0];
  40. // 获取进度条
  41. var oProgress = document.getElementsByClassName("progress")[0];
  42. // 获取文字
  43. var oText = document.getElementsByClassName("text")[0];
  44. // 给按钮绑定点击事件
  45. oBtn.onclick = function(){
  46. // 获取元素宽度
  47. var width = oProgress.offsetWidth;
  48. // 定义数字变量
  49. var num = 0;
  50. // 设置定时器
  51. var timer = setInterval(function(){
  52. num++;
  53. width += 6;
  54. oProgress.style.width = width + "px";
  55. oText.innerText = num + "%";
  56. if(width >= 600){
  57. clearInterval(timer)
  58. }
  59. },16)
  60. }
  61. </script>
  62. </body>
  63. </html>