| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- .box{
- width: 200px;
- height: 200px;
- background-color: #aaa;
- color: #fff;
- font-size: 50px;
- font-weight: bold;
- text-align: center;
- line-height: 200px;
- position: fixed;
- top:50%;
- left: 50%;
- margin-top: -100px;
- margin-left: -100px;
- }
- </style>
- </head>
- <body>
- <div class="box">10</div>
- <script>
- // 倒计时
- // 第一步:获取要控制的元素
- var div1 = document.getElementsByClassName("box")[0];
-
- // 第二步:设置倒计时的时间
- var timer = setInterval(function(){
- // 第三步:获取box中的数字
- // 注意:innerText属性返回的是字符串
- var num = div1.innerText;
- num *= 1;
- // 第四步:将数字减1
- num--;
- // 第五步:将数字赋值给box
- div1.innerText = num;
- // 第六步:判断数字是否小于0
- if(num <= 0){
- // 第七步:清除倒计时器
- clearInterval(timer);
- }
- },1000);
- </script>
- </body>
- </html>
|