1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <!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;
- }
- .big-img,.small-img{
- float: left;
- margin-right: 20px;
- }
- .big-img{
- width: 400px;
- height: 400px;
- overflow: hidden;
- position: relative;
- }
- .big-img img{
- width: 800px;
- height: 800px;
- position: absolute;
- top:0;
- left: 0;
- }
- </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">
- <img class="img-two" src="./image/niu.png" alt="img">
- </div>
- </div>
- <script>
- var smallImg = document.getElementsByClassName("small-img")[0];
- var oBox = document.getElementsByClassName("box")[0];
- var bigImg = document.getElementsByClassName("img-two")[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";
- bigImg.style.top = -2 * _top + "px";
- bigImg.style.left = -2 * _left + "px";
- }
- </script>
- </body>
- </html>
|