fengchuanyu il y a 4 mois
Parent
commit
31df970361
1 fichiers modifiés avec 58 ajouts et 0 suppressions
  1. 58 0
      5_DOM/练习2_倒计时.html

+ 58 - 0
5_DOM/练习2_倒计时.html

@@ -0,0 +1,58 @@
+<!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: 400px;
+            height: 400px;
+            background-color: #999;
+            border-radius: 10px;
+            font-size: 50px;
+            text-align: center;
+            line-height: 400px;
+            position: absolute;
+            left: 50%;
+            top: 50%;
+            margin-left: -200px;
+            margin-top: -200px;
+        }
+    </style>
+</head>
+<body>
+    <div class="box">10</div>
+    <script>
+        // 获取元素
+        var oBox = document.getElementsByClassName("box");
+        // 获取到的内容是多个元素还是一个 如果是个容器那么我们需要将要控制的元素取出来
+        oBox = oBox[0];
+        // console.log(oBox)
+        // oBox.innerText = 9;
+
+        // 倒计时方法一
+        // var num = 10;
+        // var timer = setInterval(function(){
+        //     if(num == 0){
+        //         clearInterval(timer);
+        //     }else{
+        //         num--;
+        //         console.log(num)
+        //         oBox.innerText = num;
+        //     }
+        // },1000);
+
+        // 倒计时方法二
+        var timer = setInterval(function(){
+            //oBox.innerText 获取到的值是字符串型
+            if(oBox.innerText == 0){
+                clearInterval(timer);
+            }else{
+                oBox.innerText = oBox.innerText - 1;
+            }
+        },1000);
+
+    </script>
+</body>
+</html>