练习2_BOM倒计时.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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: 300px;
  10. height: 300px;
  11. background-color: #ccc;
  12. box-shadow: 0 0 10px rgba(0,0,0,0.5);
  13. border-radius: 10px;
  14. position: fixed;
  15. top:50%;
  16. left:50%;
  17. margin-top: -150px;
  18. margin-left: -150px;
  19. text-align: center;
  20. line-height: 300px;
  21. }
  22. .box #num{
  23. font-size: 50px;
  24. font-weight: bold;
  25. color: #333;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="box">
  31. <span id="num">10</span>
  32. </div>
  33. <script>
  34. // 第一步 获取到数字标签
  35. // getElementById 返回的是具体的标签不用加下标
  36. var num = document.getElementById("num");
  37. // 第二步 倒计时
  38. // 通过定义变量的方式实现倒计时
  39. // var i = 10;
  40. // setInterval(function(){
  41. // i--;
  42. // num.innerText = i;
  43. // },1000)
  44. // 通过innerText 获取数字
  45. // 获取回来的是字符串 转换成数值型再运算
  46. var timer = setInterval(function(){
  47. // console.log(num.innerText);
  48. // 判断数字是否为0
  49. if(parseInt(num.innerText) == 0){
  50. // 如果为0 那么清楚定时器
  51. clearInterval(timer);
  52. }else{
  53. num.innerText = parseInt(num.innerText) - 1;
  54. }
  55. },1000)
  56. </script>
  57. </body>
  58. </html>