圖片牆想強調焦點時,「聚光燈」是很直覺的表現手法:滑鼠移入某張圖,該圖維持明亮並顯示提示文字,其餘圖片則同步淡出。本文拆解它的實作原理與完整程式碼。

核心思路其實很單純:
1. 為每個圖片容器動態插入一層半透明的黑色遮罩 div,初始透明度為 0(看不見)。
2. 滑鼠移入某張圖時,把所有遮罩的透明度調成 0.2,並隱藏當前這張的遮罩,於是當前圖片保持明亮、其餘變暗。
3. 滑鼠移出整個容器時,把全部遮罩透明度歸零,畫面恢復正常。
版面採絕對定位拼貼,六張圖各自指定座標與尺寸:
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery聚光燈插件制作jquery圖片特效鼠標滑過圖片當前圖片高亮</title>
<meta name="description" content="jquery聚光燈插件制作jquery圖片特效當鼠標滑過圖片時當前圖片高亮顯示其余另外的圖片變暗。" />
</head>
<body>
<style type="text/css">
*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
img{vertical-align:middle}
/* lamp */
.lamp{height:366px;width:960px;position:relative;margin:0 auto;}
.lamp .sublight{position:absolute;display:block;overflow:hidden;}
.lamp .pic1{top:0;left:0;width:240px;height:366px;}
.lamp .pic2{top:0;left:240px;width:480px;height:183px;}
.lamp .pic3{top:0;left:720px;width:240px;height:183px;}
.lamp .pic4{top:183px;left:240px;width:240px;height:183px;}
.lamp .pic5{top:183px;left:480px;width:240px;height:183px;}
.lamp .pic6{top:183px;left:720px;width:240px;height:183px;}
</style>
<div class="lamp">
<a target="_blank" href="https://www.jb51.net/" class="sublight pic1"><img src="圖片URL"/></a>
<a target="_blank" href="https://www.jb51.net/" class="sublight pic2"><img src="圖片URL"/></a>
<a target="_blank" href="https://www.jb51.net/" class="sublight pic3"><img src="圖片URL"/></a>
<a target="_blank" href="https://www.jb51.net/" class="sublight pic4"><img src="圖片URL"/></a>
<a target="_blank" href="https://www.jb51.net/" class="sublight pic5"><img src="圖片URL"/></a>
<a target="_blank" href="https://www.jb51.net/" class="sublight pic6"><img src="圖片URL"/></a>
</div>
這裡用 IIFE 包了一個 blockHighLight 函式,接受三個參數:父容器選擇器、子元素選擇器,以及各張圖的寬高對照陣列。若不傳尺寸,則自動從 img 的 width/height 屬性取值。
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
// 高亮效果
var blockHighLight = (function(window, $, undefined){
var markers = [];
return function(boxCls, itemCls, sizeArr){
var box = $(boxCls);
itemCls = itemCls || "a";
box.find(itemCls).each(function(i){
var self = $(this);
var arr, w, h, marker;
if(sizeArr !== undefined){
arr = sizeArr[i].split(",");
w = arr[0];
h = arr[1];
}else{
w = self.find("img").attr("width");
h = self.find("img").attr("height");
}
marker = $('<div style="cursor:pointer;top:0;left:0;position:absolute;width:'+w+'px;height:'+h+'px;filter:alpha(opacity=0);opacity: 0;background-color:#000;"></div>');
self.append(marker);
self.mouseover(function(){
for(var i=0; i<markers.length; i++){
markers[i].show().css({"opacity":'0.2',"filter":"alpha(opacity=20)"});
}
marker.hide();
});
markers.push(marker);
});
box.mouseout(function(){
for(var i=0; i<markers.length; i++){
markers[i].css({"opacity":'0',"filter":"alpha(opacity=0)"});
}
})
}
})(this, $);
blockHighLight(
".lamp", // 父元素
".sublight", // 子元素集
[
'240,366', // 第一張圖片的寬高
'480,183', // 第二張圖片的寬高
'240,183', // 第三張圖片的寬高
'240,183', // 第四張圖片的寬高
'240,183', // 第五張圖片的寬高
'240,183' // 第六張圖片的寬高
]
);
</script>
</body>
</html>
filter: alpha(opacity=20) 是為了兼顧舊版 IE,標準瀏覽器認 opacity,兩者並存才能跨瀏覽器一致。另外,遮罩層的寬高必須與容器相同,否則會露出沒被壓暗的邊角;尺寸寫死在呼叫參數裡雖然直接,但改成讀取實際渲染寬高會更好維護。若要在位移後仍可點擊,記得遮罩不要吃到 pointer-events,或把連結層級設得更高。