练习2_倒计时.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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: 400px;
  10. height: 400px;
  11. background-color: #999;
  12. border-radius: 10px;
  13. font-size: 50px;
  14. text-align: center;
  15. line-height: 400px;
  16. position: absolute;
  17. left: 50%;
  18. top: 50%;
  19. margin-left: -200px;
  20. margin-top: -200px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="box">10</div>
  26. <script>
  27. // 获取元素
  28. var oBox = document.getElementsByClassName("box");
  29. // 获取到的内容是多个元素还是一个 如果是个容器那么我们需要将要控制的元素取出来
  30. oBox = oBox[0];
  31. // console.log(oBox)
  32. // oBox.innerText = 9;
  33. // 倒计时方法一
  34. // var num = 10;
  35. // var timer = setInterval(function(){
  36. // if(num == 0){
  37. // clearInterval(timer);
  38. // }else{
  39. // num--;
  40. // console.log(num)
  41. // oBox.innerText = num;
  42. // }
  43. // },1000);
  44. // 倒计时方法二
  45. var timer = setInterval(function(){
  46. //oBox.innerText 获取到的值是字符串型
  47. if(oBox.innerText == 0){
  48. clearInterval(timer);
  49. }else{
  50. oBox.innerText = oBox.innerText - 1;
  51. }
  52. },1000);
  53. </script>
  54. </body>
  55. </html>