zheng 11 stundas atpakaļ
vecāks
revīzija
c7f411ba17

+ 1 - 3
原型/4.原型和原型链.html

@@ -63,10 +63,8 @@
             ,然后让每一个对象的 __proto__存储这个「共用属性组成的对象」的地址。
             而这个共用属性就是原型,原型出现的目的就是为了减少不必要的内存消耗。
             而原型链就是对象通过__proto__向当前实例所属类的原型上查找属性或方法的机制
-            ,如果找到Object的原型上还是没有找到想要的属性或者是方法则查找结束,
+            ,如果找到,则去Object的原型上找,还是没有找到想要的属性或者是方法则查找结束,
             最终会返回undefined
-
-
         */
     </script>
 </body>

+ 76 - 0
原型/6.下落的树叶.html

@@ -0,0 +1,76 @@
+<!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>
+        img {
+            position: absolute;
+            /* top: 0; */
+        }
+    </style>
+</head>
+
+<body>
+    <!-- <img src="./img/1.png" alt=""> -->
+    <script>
+        var screenWidth = document.body.clientWidth || document.documentElement.clientWidth;
+        var screenHeight = document.body.clientHeight || document.documentElement.clientHeight;
+        // 树叶的属性
+        // 树叶的宽度
+        // 树叶距离顶部的位置
+        // 树叶距离左侧的位置
+        // 树叶图片的路径
+        function Leaf() {
+            this.width = Math.round(Math.random() * 100 + 100);//100-200
+            this.top = 0;
+            this.left = Math.random() * (screenWidth - this.width);
+            this.newImg = './img/' + Math.floor(Math.random() * 4 + 1) + '.png';
+        }
+        // 绘制树叶
+        Leaf.prototype.init = function() {
+            var imgs = document.createElement("img");
+            imgs.src = this.newImg;
+            imgs.style.top = this.top + 'px';
+            imgs.style.left = this.left + 'px';
+            imgs.style.width = this.width + 'px';
+            this.img = imgs;
+            document.body.appendChild(imgs);
+        }
+        // 树叶下落
+        Leaf.prototype.fall = function(){
+            console.log(this)
+            setTimeout(function() {
+                console.log("触发",this)
+               var timer = setInterval(function() {
+                console.log(this,'哈哈哈')
+                    if(this.img.offsetTop < screenHeight - this.img.offsetHeight) {
+                        this.img.style.top = this.img.offsetTop + 10 +'px';
+                    } else {
+                        clearInterval(timer)
+                    }
+                }.bind(this),20)
+            }.bind(this),Math.random()*2000)
+        }
+        // 存放所有树叶
+        let newArr = [];
+        // 展示图片
+        for(var i=0;i<20;i++) {
+            var newLeaf = new Leaf();
+            newArr.push(newLeaf);
+            newLeaf.init();
+            // newLeaf.fall()
+        }
+        // 点击树叶下落
+        document.onclick = function() {
+            for(var i=0;i<newArr.length;i++) {
+                newArr[i].fall()
+            }
+        }
+        // 随机数范围:Math.random()*(y-x)+x
+    </script>
+</body>
+
+</html>

+ 24 - 0
原型/7.距离.html

@@ -0,0 +1,24 @@
+<!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: 300px;
+            background: #f00;
+            margin-top: 19px;
+            margin-left: 30px;
+        }
+    </style>
+</head>
+<body>
+    <div id="box"></div>
+    <script>
+        var box = document.getElementById("box");
+        console.log(box.offsetLeft)
+    </script>
+</body>
+</html>

+ 60 - 0
原型/8.防抖.html

@@ -0,0 +1,60 @@
+<!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;
+            font-size: 30px;
+            font-weight: 800;
+            text-align: center;
+            line-height: 200px;
+            color: #f00;
+            background: #ff0;
+        }
+    </style>
+</head>
+
+<body>
+    <div id="box"></div>
+    <!-- 
+        防抖:
+            当事件触发后,等待一段时间在执行回调函数,
+            如果在这段时间内,再次触发事件,则重新计时
+            触发事件在n秒内只执行最后一次触发的函数
+            执行回调函数 / 延迟时间 
+            输入框输入内容
+            滚动事件
+            按钮点击:防止用户多次快速点击
+    -->
+    <script>
+        var box = document.getElementById("box");
+        console.log(box, box.innerHTML)
+        // count +1
+        let i = 1
+        function Count() {
+            box.innerText = i++;
+        }
+        // Count()
+        // Count()
+        // Count()
+        // Count()
+        // 防抖函数
+        function debounce(fn, delay) {
+            var timer = null;
+            return function () {
+                if(timer) clearTimeout(timer);
+                timer = setTimeout(function () {
+                    fn()
+                }, delay)
+            }
+        }
+        box.addEventListener('click', debounce(Count, 3000))
+    </script>
+</body>
+
+</html>

+ 58 - 0
原型/9.节流.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: 200px;
+            height: 200px;
+            color: #ff0;
+            font-size: 30px;
+            font-weight: 800;
+            text-align: center;
+            line-height: 200px;
+            background: #f00;
+        }
+    </style>
+</head>
+
+<body>
+    <div id="box"></div>
+    <!-- 
+        节流:
+            限制函数在一定时间内的执行次数
+            规定n秒内连续点击事件但只执行一次
+            鼠标移动 鼠标跟踪 窗口大小 频繁点击按钮
+    -->
+    <script>
+        var box = document.getElementById("box");
+        console.log(box, box.innerHTML)
+        // count +1
+        let i = 1
+        function Count() {
+            box.innerText = i++;
+        }
+        function throttle(fn, delay) {
+            var timer = null;
+            return function () {
+                if (!timer) {
+                    timer = setTimeout(function () {
+                        fn();
+                        timer = null;
+                    }, delay)
+                }
+            }
+        }
+        box.addEventListener('click', throttle(Count, 3000))
+        /**
+         * 防抖与节流的区别:
+         *  节流 保证函数在固定时间间隔内至少执行一次
+         *  防抖 在事件停止触发一段时间后才执行
+        */
+    </script>
+</body>
+
+</html>