放大镜效果

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>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: 0px;
      left: 0px;
      cursor: move;
      display: none;
    }

    .small {
      position: relative;
    }
  </style>
</head>
<body>
<div class="box" id="box">
  <div class="small"><!--小层-->
    <img src="images/small.png" width="350" alt=""/>
    <div class="mask"></div><!--遮挡层-->
  </div><!--小图-->
  <div class="big"><!--大层-->
    <img src="images/big.jpg" width="800" alt=""/><!--大图-->
  </div><!--大图-->
</div>
<!--导入外部的js文件-->
<script src="common.js"></script>
<script>
  var box = my$("box");
  var small = box.children[0];
  var big = box.children[1];
  var mask = small.children[1];
  var bigImage = big.children[0];
  box.onmouseover = function () {
    mask.style.display = "block";
    big.style.display = "block";
  };
  box.onmouseout = function () {
    mask.style.display = "none";
    big.style.display = "none";
  };
  small.onmousemove = function (e) {
    var x = e.clientX - mask.offsetWidth / 2;
    var y = e.clientY - mask.offsetHeight / 2;
    x = x - 100;
    y = y - 100;

    x = (x < 0)? 0:x;
    y = (y < 0)?0:y;

    x = (x > small.offsetWidth - mask.offsetWidth)? small.offsetWidth - mask.offsetWidth: x;
    y = (y > small.offsetHeight - mask.offsetHeight)? small.offsetHeight - mask.offsetHeight: y;

    mask.style.left = x + "px";
    mask.style.top = y + "px";

    var ratio = (bigImage.offsetWidth - big.offsetWidth) / (small.offsetWidth - mask.offsetWidth);

    var moveX = parseInt(x * ratio);
    var moveY = parseInt(y * ratio);

    bigImage.style.marginLeft = -moveX + "px";
    bigImage.style.marginTop = -moveY + "px";
  };

</script>


</body>
</html>

 

上一篇:PAT Basic 1019 数字黑洞 (20 分)


下一篇:Logstash 的安装与数据导入(阿里云)