萬盛學電腦網

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

JavaScript 實作圖片縮圖滑鼠懸停預覽大圖 (Tooltip/Zoom 效果)

核心需求

商品列表、圖庫頁面:滑鼠移至縮圖上方,絕對定位顯示大圖預覽;移出隱藏。需處理邊界檢測、圖片比例縮放、載入狀態。

現代純 CSS 方案 (無 JS、效能最佳)

<style>
.zoom-wrapper { position: relative; display: inline-block; }
.zoom-wrapper .zoom-large {
    position: absolute; left: 100%; top: 0; margin-left: 10px;
    max-width: 400px; max-height: 300px;
    border: 1px solid #ddd; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    opacity: 0; visibility: hidden; pointer-events: none;
    transition: opacity 0.2s, visibility 0.2s;
    z-index: 100;
}
.zoom-wrapper:hover .zoom-large {
    opacity: 1; visibility: visible;
}
/* 邊界修正:靠近右邊緣時顯示在左側 */
.zoom-wrapper:has(.zoom-large):hover .zoom-large {
    /* 需 JS 協助判斷邊界,純 CSS 難以完美處理 */
}
</style>

<div class="zoom-wrapper">
  <img src="thumb.jpg" alt="商品縮圖" width="100" height="100">
  <img class="zoom-large" src="large.jpg" alt="大圖預覽">
</div>

JavaScript 增強版 (支援邊界自動翻轉、延遲載入、跟隨滑鼠)

class ImagePreview {
    constructor(options = {}) {
        this.selector = options.selector || '.img-preview';
        this.maxWidth = options.maxWidth || 400;
        this.maxHeight = options.maxHeight || 300;
        this.offsetX = options.offsetX || 15;
        this.offsetY = options.offsetY || 0;
        this.delay = options.delay || 100; // 顯示延遲 ms
        this.$preview = null;
        this.timer = null;
        this._init();
    }
    
    _init() {
        // 建立預覽層 (單例)
        this.$preview = $('
') .css({ position: 'fixed', display: 'none', zIndex: 9999, maxWidth: this.maxWidth, maxHeight: this.maxHeight, border: '1px solid #ddd', boxShadow: '0 4px 12px rgba(0,0,0,0.15)', pointerEvents: 'none' // 避免阻擋滑鼠事件 }) .appendTo('body'); $(document).on('mouseenter', this.selector, (e) => this._show(e)); $(document).on('mousemove', this.selector, (e) => this._move(e)); $(document).on('mouseleave', this.selector, () => this._hide()); } _show(e) { clearTimeout(this.timer); this.timer = setTimeout(() => { var $thumb = $(e.currentTarget); var largeSrc = $thumb.data('large') || $thumb.attr('src').replace(/thumb|small/, 'large'); this.$preview.html('') .find('img').on('load', () => this._position(e)); // 圖片載入失敗處理 this.$preview.find('img').on('error', () => { this.$preview.html('
圖片載入失敗
'); this._position(e); }); }, this.delay); } _move(e) { if (this.$preview.is(':visible')) this._position(e); } _position(e) { var vpW = $(window).width(), vpH = $(window).height(); var pw = this.$preview.outerWidth(), ph = this.$preview.outerHeight(); var left = e.clientX + this.offsetX; var top = e.clientY + this.offsetY; // 右邊界超出 → 顯示在左側 if (left + pw > vpW) left = e.clientX - pw - this.offsetX; // 下邊界超出 → 顯示在上方 if (top + ph > vpH) top = e.clientY - ph - this.offsetY; this.$preview.css({ left, top }).show(); } _hide() { clearTimeout(this.timer); this.$preview.hide(); } destroy() { $(document).off('mouseenter mousemove mouseleave', this.selector); this.$preview.remove(); } } // 初始化 new ImagePreview({ selector: '.product-thumb img', maxWidth: 500, maxHeight: 400 });

HTML 使用範例

<img class="product-thumb" src="img/thumb_01.jpg" data-large="img/large_01.jpg" width="120" height="120">
<img class="product-thumb" src="img/thumb_02.jpg" data-large="img/large_02.jpg" width="120" height="120">

進階功能擴充

  • 點擊放大燈箱:整合 fancybox、lightgallery 等燈箱庫。
  • 內容不只是圖片:預覽層可放 HTML (規格表、價格、按鈕)。
  • 觸控裝置:長按/雙擊觸發,或改用點擊切換顯示狀態。
  • 虛擬清單優化:大量圖片時配合 IntersectionObserver 延遲實例化。

效能關鍵點

  1. 預覽層 **單例複用**,不重複建立 DOM。
  2. pointer-events: none 避免預覽層攔截滑鼠事件導致閃爍。
  3. 顯示延遲 setTimeout 避免滑過即觸發。
  4. 大圖 onload 後再定位顯示,避免尺寸為 0 導致位置錯誤。
copyright © 萬盛學電腦網 all rights reserved