练习题1_倒计时.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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: 200px;
  10. height: 200px;
  11. background-color: #aaa;
  12. color: #fff;
  13. font-size: 50px;
  14. font-weight: bold;
  15. text-align: center;
  16. line-height: 200px;
  17. position: fixed;
  18. top:50%;
  19. left: 50%;
  20. margin-top: -100px;
  21. margin-left: -100px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="box">10</div>
  27. <script>
  28. // 倒计时
  29. // 第一步:获取要控制的元素
  30. var div1 = document.getElementsByClassName("box")[0];
  31. // 第二步:设置倒计时的时间
  32. var timer = setInterval(function(){
  33. // 第三步:获取box中的数字
  34. // 注意:innerText属性返回的是字符串
  35. var num = div1.innerText;
  36. num *= 1;
  37. // 第四步:将数字减1
  38. num--;
  39. // 第五步:将数字赋值给box
  40. div1.innerText = num;
  41. // 第六步:判断数字是否小于0
  42. if(num <= 0){
  43. // 第七步:清除倒计时器
  44. clearInterval(timer);
  45. }
  46. },1000);
  47. </script>
  48. </body>
  49. </html>