萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

jQuery 實現鼠標懸停圖片放大鏡效果完整教學

jQuery 圖片放大鏡:Mouseover 動態創建大圖預覽

電商商品圖「鼠標懸停顯示大圖」是經典交互。本文實現零插件、純 jQuery的輕量級放大鏡:懸停小圖 → 動態創建大圖元素 → 固定位置顯示 → 移出銷毀。

核心邏輯

  1. 綁定 mouseover / mouseout 到小圖
  2. mouseover:讀取 src、計算放大尺寸、創建 <img id="tip">、追加到固定容器 #imgtest
  3. mouseout:移除 #tip
  4. CSS 固定容器位置 (position:absolute; z-index:999) 避免跟隨滑鼠抖動

完整代碼


<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>圖片放大鏡演示</title>
  <script src="jquery-1.9.1.min.js"></script>
  <script>
    $(function() {
      // 預計算大圖尺寸 (小圖寬度的 3 倍)
      const size = 3.0 * $('#image1').width();
      
      $("#image1").on('mouseover', function(event) {
        const $target = $(event.target);
        if ($target.is('img')) {
          // 動態創建大圖元素
          $('')
            .css({ height: size, width: size })
            .appendTo("#imgtest");
        }
      }).on('mouseout', function() {
        $("#tip").remove(); // 移除大圖
      });
    });
  </script>
  <style>
    #imgtest { 
      position: absolute; 
      top: 100px; left: 400px; 
      z-index: 9999; 
      border: 1px solid #ccc; 
      background: #fff;
      padding: 5px;
      box-shadow: 0 4px 12px rgba(0,0,0,.15);
    }
    table { margin-left: 100px; font-size: 20px; }
  </style>
</head>
<body>
  <div id="imgtest"></div> <!-- 固定位置顯示大圖 -->
  <br><br>
  <table border="1" style="text-align:center;" align="center">
    <thead>
      <tr>
        <td style="width:300px">商品名稱</td>
        <td style="width:60px">圖片</td>
        <td style="width:170px">數量</td>
        <td style="width:170px">價格</td>
        <td style="width:250px">小計</td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="name">商品1</td>
        <td class="image">
          <img src="1.jpg" width="40" height="40" id="image1"/>
        &td>
        <td class="quantity"><input type="number" value="1" min="1"></td>
        <td class="price">¥29.90</td>
        <td class="total">¥29.90</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

進階增強:跟隨鼠標移動的放大鏡


$('#image1').on('mousemove', function(e) {
  const $tip = $('#tip');
  if (!$tip.length) return;
  
  // 容器相對頁面偏移
  const offset = $('#imgtest').offset();
  // 鼠標相對容器位置
  const x = e.pageX - offset.left - $tip.width() / 2;
  const y = e.pageY - offset.top - $tip.height() / 2;
  
  // 限制在容器內
  const maxX = $('#imgtest').width() - $tip.width();
  const maxY = $('#imgtest').height() - $tip.height();
  
  $tip.css({ 
    left: Math.max(0, Math.min(x, maxX)), 
    top: Math.max(0, Math.min(y, maxY)) 
  });
});

專業級放大鏡架構 (參考 jqZoom / CloudZoom)


/*
結構:
邏輯: 1. mousemove 移動 .zoom-lens (跟隨鼠標,邊界限制) 2. 同步移動 .zoom-big img (反方向、倍速移動) 3. 大圖尺寸 = 小圖尺寸 * 放大倍數 */

CSS 關鍵點


.zoom-container { position: relative; display: inline-block; }
.zoom-small { position: relative; overflow: hidden; }
.zoom-lens { 
  position: absolute; border: 1px solid #ddd; 
  background: rgba(255,255,255,.5); cursor: crosshair; 
  display: none; /* hover 時顯示 */
}
.zoom-big { 
  position: absolute; left: 110%; top: 0; 
  overflow: hidden; border: 1px solid #ccc; 
  z-index: 999; display: none;
}
.zoom-big img { position: absolute; }

現代替代:CSS 僅實現簡易放大


.zoom-img { transition: transform .3s ease; }
.zoom-img:hover { transform: scale(2.5); transform-origin: center; }
// 缺點:無法控制顯示區域、會撐大佈局、無鏡頭框引導

根據項目需求選擇:簡易展示用 CSS,電商級體驗用專業插件或自行實現鏡頭同步邏輯。

copyright © 萬盛學電腦網 all rights reserved