「聚光燈效果」是一種經典的圖片交互:鼠標移入某張圖,該圖保持高亮,其他圖片疊加半透明黑色遮罩變暗,營造聚焦感。本文提供完整的 HTML/CSS/jQuery 實現,採用**遮罩層動態生成 + 事件委派**架構,支援任意佈局、響應式尺寸、多組獨立實例。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery 聚光燈效果</title>
<style>
* { margin:0; padding:0; box-sizing:border-box; }
.lamp {
width: 960px; height: 366px;
margin: 40px auto;
position: relative;
font-size: 0; /* 消除 inline-block 間隙 */
}
.sublight {
display: inline-block;
position: relative;
overflow: hidden;
vertical-align: top;
}
.sublight img { display: block; width: 100%; height: 100%; }
/* 圖片佈局尺寸(可改為響應式 %) */
.pic1 { width: 240px; height: 366px; }
.pic2 { width: 480px; height: 183px; }
.pic3 { width: 240px; height: 183px; }
.pic4 { width: 240px; height: 183px; }
.pic5 { width: 240px; height: 183px; }
.pic6 { width: 240px; height: 183px; }
/* 遮罩層樣式由 JS 動態注入,這裡預留類名供參考 */
.spotlight-mask {
position: absolute; top: 0; left: 0;
background: #000; opacity: 0;
pointer-events: none; /* 關鍵:遮罩不阻擋滑鼠事件 */
transition: opacity 0.3s ease;
}
</style>
</head>
<body>
<div class="lamp">
<a class="sublight pic1" href="#"><img src="https://picsum.photos/seed/1/240/366" alt=""></a>
<a class="sublight pic2" href="#"><img src="https://picsum.photos/seed/2/480/183" alt=""></a>
<a class="sublight pic3" href="#"><img src="https://picsum.photos/seed/3/240/183" alt=""></a>
<a class="sublight pic4" href="#"><img src="https://picsum.photos/seed/4/240/183" alt=""></a>
<a class="sublight pic5" href="#"><img src="https://picsum.photos/seed/5/240/183" alt=""></a>
<a class="sublight pic6" href="#"><img src="https://picsum.photos/seed/6/240/183" alt=""></a>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
/**
* 聚光燈效果封裝
* @param {string} boxSel 容器選擇器
* @param {string} itemSel 子項選擇器
* @param {Array} sizeArr 可選:['寬,高', ...] 對應每個子項尺寸
*/
var Spotlight = (function($) {
function Spotlight(boxSel, itemSel, sizeArr) {
this.$box = $(boxSel);
this.$items = this.$box.find(itemSel);
this.sizeArr = sizeArr;
this.masks = [];
this.init();
}
Spotlight.prototype.init = function() {
var self = this;
this.$items.each(function(i) {
var $item = $(this);
var w, h;
if (self.sizeArr && self.sizeArr[i]) {
var arr = self.sizeArr[i].split(',');
w = parseInt(arr[0], 10);
h = parseInt(arr[1], 10);
} else {
// 嘗試從圖片獲取尺寸
var $img = $item.find('img');
w = $img.attr('width') || $img.width() || $item.width();
h = $img.attr('height') || $img.height() || $item.height();
}
// 建立遮罩層
var $mask = $('
三、核心實現邏輯拆解
1. 遮罩層動態生成
- 遍歷每個
.sublight,根據尺寸建立 <div class="spotlight-mask">
pointer-events: none 確保遮罩**不攔截滑鼠事件**,否則會觸發閃爍
- 預設
opacity: 0; display: none 完全隱藏
2. 事件流設計
// 進入某項
mouseenter → 遍歷所有遮罩
非當前項 → show() + opacity: 0.2 (淡入)
當前項 → hide() (保持高亮)
// 離開容器
mouseleave → 所有遮罩 opacity: 0 + hide()
使用 mouseleave 而非 mouseout 避免冒泡導致的誤觸發。
3. 尺寸自適應策略
- 優先使用傳入的
sizeArr(確保初始化即正確,無佈局抖動)
- 回退讀取
img[width/height] 屬性或 .width()/.height()
- 支援響應式:若容器百分比佈局,需在
resize 事件中重新計算並更新遮罩尺寸
四、進階擴展建議
- 淡入淡出動畫:CSS
transition: opacity 0.3s 已內建,無需 JS 動畫
- 顯示標題/描述:在
mouseenter 時顯示對應 data-title 文字層
- 多組獨立實例:類封裝天然支援,
new Spotlight('.lamp2', '.item', [...])
- 無障礙:添加
role="img"、鍵盤 Tab 聚焦時同樣觸發高亮
五、常見問題排查
現象 原因 解決
遮罩不顯示/位置錯位 父元素非 position: relative 確保 .sublight { position: relative }
滑鼠移入閃爍 遮罩攔截事件導致 mouseout 觸發 遮罩加 pointer-events: none
圖片載入前尺寸為 0 JS 執行時圖片未載入 用 sizeArr 固定尺寸,或 img.onload 後初始化
響應式斷點遮罩尺寸不更新 未監聽 resize 加 $(window).on('resize', () => instance.refresh())
結語
聚光燈效果的核心在於**遮罩層的動態管理與事件流控制**。本文提供的封裝類結構清晰、支援多實例、易於擴展,可直接用於生產環境。理解「動態生成遮罩 + pointer-events:none + mouseenter/leave 事件委派」這一模式,同樣適用於工具提示、圖片放大鏡、卡片懸停等多種交互場景。