萬盛學電腦網

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

jQuery Revealing 插件實現圖片點擊拉伸放大展示效果

效果描述

縮略圖整齊排列,點擊任一圖片 → 該圖片平滑放大至原始尺寸,顯示詳細資訊區域;點擊「關閉」或空白處 → 平滑縮回縮略圖位置。類似燈箱但保持佈局流,視覺連貫性極佳。

核心 HTML 結構

<div class="photo_slider">
    <img src="thumb1.jpg" alt="圖片標題 1" />
    <div class="info_area">詳細描述內容...</div>
</div>
<div class="photo_slider">
    <img src="thumb2.jpg" alt="圖片標題 2" />
    <div class="info_area">詳細描述內容...</div>
</div>

CSS 關鍵樣式

.photo_slider {
    float: left; margin: 10px; cursor: pointer;
    border: 1px solid #ccc; background: #fff;
    position: relative; overflow: hidden;
    width: 150px; height: 150px; /* 縮略圖固定尺寸 */
}
.photo_slider img { width: 100%; height: 100%; object-fit: cover; }
.photo_slider .info_area { display: none; padding: 10px; background: #f9f9f9; border-top: 1px solid #eee; }
.photo-area { /* JS 動態添加:放大狀態 */ position: absolute; z-index: 100; }

核心 JS 邏輯 (Revealing 插件核心)

$(document).ready(function() {
    $('.photo_slider').each(function() {
        var $this = $(this).addClass('photo-area');
        var $img = $this.find('img');
        var $info = $this.find('.info_area');
        var opts = {};

        // 圖片載入後記錄原始尺寸
        $img.on('load', function() {
            opts.imgW = $img.width();
            opts.imgH = $img.height();
        }).each(function() {
            if (this.complete) $(this).trigger('load'); // 快取情況手動觸發
        });

        // 記錄縮略態尺寸
        opts.orgW = $this.width();
        opts.orgH = $this.height();

        // 初始狀態:圖片置中偏移 (視覺上居中裁剪)
        $img.css({ marginLeft: '-150px', marginTop: '-150px' });

        // 包裝圖片層
        var $wrap = $('
').append($img).prependTo($this); // 更多資訊按鈕 var $open = $('More Info >').appendTo($this); // 關閉按鈕 (在 info_area 內) var $close = $('Close').appendTo($info); opts.wrapW = $wrap.width(); opts.wrapH = $wrap.height(); // 打開動畫 $open.click(function(e) { e.preventDefault(); $this.animate({ width: opts.imgW, height: opts.imgH + 30, borderWidth: '10' }, 600); $open.fadeOut(); $wrap.animate({ width: opts.imgW, height: opts.imgH }, 600); $info.fadeIn(); $img.animate({ marginTop: 0, marginLeft: 0 }, 600); // 圖片歸位 }); // 關閉動畫 $close.click(function(e) { e.preventDefault(); $this.animate({ width: opts.orgW, height: opts.orgH, borderWidth: '1' }, 600); $open.fadeIn(); $wrap.animate({ width: opts.wrapW, height: opts.wrapH }, 600); $img.animate({ marginTop: '-150px', marginLeft: '-150px' }, 600); $info.fadeOut(); }); }); });

關鍵技巧解析

  1. 尺寸預測:`$img.load()` 取得原始寬高,供放大動畫目標值使用。
  2. 視覺連貫:縮略態時 `margin: -150px` 將大圖置中裁剪顯示;放大時 `margin: 0` 還原完整圖片,無跳躍感。
  3. 層級管理:`z-index` 確保放大時覆蓋其他元素;`overflow: hidden` 容器裁剪動畫中超出部分。
  4. 資訊區延遲顯示:`fadeIn`/`fadeOut` 配合容器尺寸動畫,資訊區自然展開/收合。

擴展建議

  • 鍵盤支援:`Esc` 關閉,左右鍵切換上一張/下一張。
  • 響應式:放大尺寸改為 `max-width: 90vw; max-height: 80vh`。
  • 圖片懶載:`data-src` + `IntersectionObserver` 載入原圖。
  • 無障礙:`role="dialog" aria-modal="true"`、焦點陷阱管理。
  • 手勢支援:雙指縮放、滑動關閉 (Hammer.js / 原生 Touch Events)。

現代替代:CSS-only 方案 (無 JS)

.photo_slider input[type=checkbox] { display: none; }
.photo_slider input:checked + .wrap {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
    width: auto; height: auto; max-width: 90vw; max-height: 80vh; z-index: 1000;
    border: 10px solid #fff; box-shadow: 0 0 30px rgba(0,0,0,0.5);
}
.photo_slider input:checked ~ .info_area { display: block; animation: fadeIn .3s; }
.photo_slider input:checked + .wrap img { margin: 0; width: auto; height: auto; }
<label class="photo_slider">
    <input type="checkbox">
    <div class="wrap"><img src="..."></div>
    <div class="info_area">...</div>
</label>

純 CSS `:checked` 偽類實現同樣效果,零 JS、效能極佳,但無法實現「從縮略圖位置平滑展開」的位移動畫(僅能淡入/縮放)。

copyright © 萬盛學電腦網 all rights reserved