fengchuanyu 4 months ago
parent
commit
9c20508f45
1 changed files with 89 additions and 0 deletions
  1. 89 0
      5_DOM/练习11_放大镜.html

+ 89 - 0
5_DOM/练习11_放大镜.html

@@ -0,0 +1,89 @@
+<!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>
+        .small-img{
+            width: 600px;
+            height: 600px;
+            position: relative;
+            float: left;
+        }
+        .small-img img{
+            width: 600px;
+            height: 600px;
+            position: absolute;
+            top: 0;
+            left: 0;
+        }
+        .small-img .box{
+            width: 300px;
+            height: 300px;
+            background-color: #fff;
+            opacity: 0.5;
+            position: absolute;
+            top: 0;
+            left:0;
+        }
+        .big-img{
+            margin-left: 100px;
+            float: left;
+            width: 600px;
+            height: 600px;
+            overflow: hidden;
+            position: relative;
+        }
+        .big-img img{
+            width: 1200px;
+            height: 1200px;
+            position: absolute;
+            top: 0;
+            left: 0;
+        }
+    </style>
+</head>
+<body>
+    <div class="small-img">
+        <img src="./image/niu.png" alt="img">
+        <div class="box"></div>
+    </div>
+    <div class="big-img">
+        <img class="big-pic" src="./image/niu.png" alt="img">
+    </div>
+    <script>
+        var oSmallImg = document.getElementsByClassName("small-img")[0];
+        var oBox = document.getElementsByClassName("box")[0];
+        var oBigImg = document.getElementsByClassName("big-pic")[0];
+
+        oSmallImg.onmousemove = function(e){
+            var x = e.clientX - 150;
+            var y = e.clientY - 150;
+
+            // 控制半透明蒙版左侧最小值范围
+            if(x<0){
+                x = 0;
+            }
+            // 控制半透明蒙版顶部最小值范围
+            if(y<0){
+                y = 0;
+            }
+            // 控制半透明蒙版右侧最大值范围
+            if(x>=300){
+                x = 300
+            }
+            // 控制半透明蒙版顶部最大值范围
+            if(y>=300){
+                y = 300
+            }
+
+
+            oBox.style.left = x + "px";
+            oBox.style.top = y + "px";
+            oBigImg.style.left = -2*x + "px";
+            oBigImg.style.top = -2*y + "px";
+        }
+    </script>
+</body>
+</html>