放大镜功能

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 350px;
            height: 350px;
            margin: 100px;
            border: 1px solid #ccc;
            position: relative;
        }

        .big {
            width: 400px;
            height: 400px;
            position: absolute;
            top: 0;
            left: 360px;
            border: 1px solid #ccc;
            overflow: hidden;
            display: none;
        }

        .mask {
            width: 175px;
            height: 175px;
            background: rgba(255, 255, 0, 0.4);
            position: absolute;
            top: 0;
            left: 0;
            cursor: move;
            display: none;
        }

        .small {
            position: relative;
        }

        .box img {
            vertical-align: top;
        }

        #bigBox img {
            position: absolute;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <!--我们一来就看到的盒子-->
    <div id="smallBox" class="small">
        <!--smallbox里面的一张小图片-->
        <img src="images/001.jpg" width="350" alt=""/>
        <!--mask: 黄色的div,一开始是隐藏的-->
        <div id="mask" class="mask"></div>
    </div>

    <!--smallBox旁边的大盒子,一开始隐藏的-->
    <div id="bigBox" class="big">
        <!--大图片,会在大盒子里面移动。我们只能看到图片的一部分。-->
        <img src="images/0001.jpg" width="800" alt=""/>
    </div>
</div>

<script>

  var box = document.getElementById("box");
  var smallBox = document.getElementById("smallBox");
  var mask = document.getElementById("mask");
  var bigBox = document.getElementById("bigBox");
  var bigImg = bigBox.children[0];

  //1. 给smallBox注册鼠标经过事件, 让mask和bigBox显示出来
  smallBox.onmouseover = function () {
    mask.style.display = "block";
    bigBox.style.display = "block";
  }

  smallBox.onmouseout = function () {
    mask.style.display = "none";
    bigBox.style.display = "none";
  }


  //产品经理
  smallBox.onmousemove = function (e) {
    //让mask跟着鼠标移动, 鼠标在smallBox中的位置

    //spaceX:鼠标在smallbox中的x的位置. offsetLeft获取的是 盒子距离有定位的父元素的距离
    var spaceX = e.pageX - box.offsetLeft;
    var spaceY = e.pageY - box.offsetTop;
    var x = spaceX - mask.offsetWidth/2;
    var y = spaceY- mask.offsetHeight/2;
    //x的最小值:0    最大值: smallBox.offsetWidth - mask.offsetWidth
    if(x <= 0){
      x = 0;
    }
    if(x >= smallBox.offsetWidth - mask.offsetWidth){
      x = smallBox.offsetWidth - mask.offsetWidth;
    }
    if(y <= 0){
      y = 0;
    }
    if(y >= smallBox.offsetHeight - mask.offsetHeight){
      y = smallBox.offsetHeight - mask.offsetHeight;
    }
    //设置x和y必须在判断后面
    mask.style.left = x + "px";
    mask.style.top = y + "px";

 

 

    //移动大图片
    // 假设吃馒头的速度是匀速的
    // 助教 10分钟 能够吃100个馒头
    // 班主任10分钟 能吃300个馒头
    // 助教 吃完了50个馒头了
    // 问:班主任吃完了 多少个馒头
    // 助教当前吃的馒头数/助教能吃的总馒头数 = 班主任当前吃的馒头数/班主任能吃的总馒头数
    // mask当前移动的距离/smallBox的宽度 = 大图片移动的距离/大图片的宽度
    bigImg.style.left = - x/smallBox.offsetWidth * bigImg.offsetWidth + "px";
    bigImg.style.top = - y/smallBox.offsetHeight * bigImg.offsetHeight + "px";
  }

</script>

</body>
</html>

posted @ 2017-09-08 08:54  ZBB0304  阅读(455)  评论(0编辑  收藏  举报