123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <!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>
- body{
- margin: 0;
- }
- .small-img{
- width: 400px;
- height: 400px;
- position: relative;
- }
- .small-img img{
- width: 100%;
- height: 100%;
- position: absolute;
- top:0;
- left: 0;
- }
- .box{
- width: 200px;
- height: 200px;
- background-color: #fff;
- position: absolute;
- top:0;
- left: 0;
- opacity: 0.5;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="small-img">
- <img src="./image/niu.png" alt="img">
- <div class="box"></div>
- </div>
- <div class="big-img"></div>
- </div>
- <script>
- var smallImg = document.getElementsByClassName("small-img")[0];
- var oBox = document.getElementsByClassName("box")[0];
-
- smallImg.onmousemove = function(e){
- var _top = e.clientY-100;
- var _left = e.clientX-100;
- // 控制左侧边界
- if(_left <= 0){
- _left = 0
- }
- // 控制顶部边界
- if(_top<=0){
- _top = 0;
- }
- // 控制右侧边界
- if(_left>=200){
- _left = 200;
- }
- // 控制底部
- if(_top>=200){
- _top = 200;
- }
- oBox.style.top = _top + "px";
- oBox.style.left = _left + "px";
- }
- </script>
- </body>
- </html>
|