京东放大镜

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #box1{
            width: 350px;
            height: 350px;
            border:1px solid black;
            position: relative;
        }
        #box1 img{
            display: block;
            width: 100%;
            height: 100%;
        }
        #mark{
            width: 175px;
            height: 175px;
            background: rgba(0,0,0, .3);
            position: absolute;/* */
            top: 0;
            left: 0;
            cursor: move;
        }
        #box2{
        width: 350px;
        height: 350px;
        border: 1px solid black;
        overflow: hidden;
        position: relative;
        top: -350px;
        left: 400px;
        }
        #box2 img{
            width: 700px;
            height: 700px;
            display: block;
            position: absolute;
            left: 0;
            top: 0;
        }

    </style>
</head>
<body>
    <div id="box1"> 
        <img id="img1" src="./img/iphone.jpg" alt="">
        <!--  -->
    </div>
    <div id="box2">
        <img id="img2" src="./img/iphone_big.jpg" alt="">
    </div>
    <script>
       let box1=document.getElementById("box1");
       let box2=document.getElementById("box2");
       let img2=document.getElementById("img2");

       let mark=null;
       box1.onmouseenter=function(){//移入onmouseover(冒泡引起)
           mark=document.createElement("div");
           mark.id="mark";
           box1.appendChild(mark);//只能放dom
           box2.style.display="block"
       }
       box1.onmousemove=function(e){//移动
         let minL=0;
         let maxL=box1.offsetWidth-mark.offsetWidth;
         let minT=0;
         let maxT=box1.offsetHeight-mark.offsetHeight;
         let left=e.clientX-box1.offsetLeft-87.5;//
         let top=e.clientY-box1.offsetTop-87.5;
           if(left<=minL){
              left=minL;
              
          }
          if(left>=maxL){
             left=maxL;
          }
          if(top<=minT){
              top=minT;
              
          }
          if(top>=maxT){
             top=maxT;
          }
           mark.style.left=left+"px";
           mark.style.top=top+"px";

           img2.style.left=-left*2+"px";
           img2.style.top=-top*2+"px";

       }
       box1.onmouseleave=function(){//移出
           box1.removeChild(mark);
           box2.style.display="none"
       }
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值