商品列表、圖庫頁面:滑鼠移至縮圖上方,絕對定位顯示大圖預覽;移出隱藏。需處理邊界檢測、圖片比例縮放、載入狀態。
<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>
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
});
<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 等燈箱庫。pointer-events: none 避免預覽層攔截滑鼠事件導致閃爍。setTimeout 避免滑過即觸發。onload 後再定位顯示,避免尺寸為 0 導致位置錯誤。