眼睛會了,手也會了之JS原生放大鏡效果

實現照片放大鏡

頁面樣式

<!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: 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>

</body>

</html>

腳本文件

    /**
     * 獲取指定標籤對象
     * @param id 標籤的id屬性值
     * @returns {Element}根據id屬性值返回指定標籤對象
     */
    function my$(id) {
        return document.getElementById(id);
    } 

    //獲取需要的元素
    var box = my$("box");
    //獲取小圖的div
    var small = box.children[0];
    //遮擋層
    var mask = small.children[1];
    //獲取大圖的div
    var big = box.children[1];
    //獲取大圖
    var bigImg = big.children[0];

    // 進入小圖,顯示遮擋層,顯示大圖
    small.onmouseover = function () {
      mask.style.display = 'block'
      big.style.display = 'block'

    }
    // 移除小圖,隱藏遮擋層,隱藏大圖
    // small.onmouseout = function () {
    //   mask.style.display = 'none'
    //   big.style.display = 'none'
    // }

    // 鼠標移動事件
    small.onmousemove = function (e) {

      // 確保鼠標在遮擋層中央
      var x = e.clientX - 100 - mask.offsetWidth / 2
      var y = e.clientY - 100 - mask.offsetHeight / 2
      // 限制邊距範圍
      // x 的最小值
      x = x <= 0 ? 0 : x
      // y 的最小值
      y = y <= 0 ? 0 : y
      // x 的最大值
      x = x >= small.offsetWidth - mask.offsetWidth ? small.offsetWidth - mask.offsetWidth : x
      // y 的最大值
      y = y >= small.offsetHeight - mask.offsetHeight ? small.offsetHeight - mask.offsetHeight : y

      // 遮擋層移動
      mask.style.left = x + 'px'
      mask.style.top = y + 'px'

      // 計算比例
      // 遮擋層的移動距離 / 大圖的移動距離 = 遮擋層的最大移動距離 / 大圖最大移動距離

      // 大圖最大移動距離
      // X
      var bigX = bigImg.offsetWidth - big.offsetWidth
      // Y
      var bigY = bigImg.offsetHeight - big.offsetHeight

      // 大圖移動距離
      // X
      var bigMoveX = x * bigX / (small.offsetWidth - mask.offsetWidth)
      // Y
      var bigMoveY = y * bigY / (small.offsetHeight - mask.offsetHeight)

      // 移動大圖
      bigImg.style.marginLeft = -bigMoveX + 'px'
      bigImg.style.marginTop = -bigMoveY + 'px'

    }

類似與京東進行圖片的縮放,一張大圖,一張小圖

資源文件

small

small

big

bigyanji

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章