圖片列表預設半透明 (opacity: 0.6);滑鼠移入某張圖片,該圖淡入至高亮 (opacity: 0.9),其餘維持半透明;滑鼠離開列表區域,所有圖片恢復全不透明 (opacity: 1)。
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>jQuery 滑鼠滑過高亮遮罩</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
ul, li { list-style: none; }
.gallery { display: flex; flex-wrap: wrap; gap: 4px; padding: 4px; }
.gallery li { width: 190px; height: 160px; overflow: hidden; }
.gallery li a { display: block; width: 100%; height: 100%; }
.gallery li img { width: 100%; height: 100%; object-fit: cover; transition: opacity 0.3s, transform 0.3s; }
</style>
</head>
<body>
<ul class="gallery" id="gallery">
<li><a href="#"><img src="https://picsum.photos/190/160?1" alt=""></a></li>
<li><a href="#"><img src="https://picsum.photos/190/160?2" alt=""></a></li>
<li><a href="#"><img src="https://picsum.photos/190/160?3" alt=""></a></li>
<li><a href="#"><img src="https://picsum.photos/190/160?4" alt=""></a></li>
<li><a href="#"><img src="https://picsum.photos/190/160?5" alt=""></a></li>
<li><a href="#"><img src="https://picsum.photos/190/160?6" alt=""></a></li>
<li><a href="#"><img src="https://picsum.photos/190/160?7" alt=""></a></li>
<li><a href="#"><img src="https://picsum.photos/190/160?8" alt=""></a></li>
</ul>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function() {
const $gallery = $('#gallery');
const $links = $gallery.find('a');
const $imgs = $links.find('img');
// 初始狀態:半透明
$imgs.css('opacity', 0.6);
$links.on('mouseenter', function() {
const $thisImg = $(this).find('img');
// 其他圖片保持半透明 (已是 0.6),當前淡入高亮
$imgs.not($thisImg).stop(true).animate({ opacity: 0.6 }, 200);
$thisImg.stop(true).animate({ opacity: 0.95 }, 200);
});
$gallery.on('mouseleave', function() {
// 離開列表區域,全部恢復不透明
$imgs.stop(true).animate({ opacity: 1 }, 300);
});
});
</script>
</body>
</html>
| 細節 | 處理方式 |
|---|---|
| 初始半透明 | JS 設定 $imgs.css('opacity', 0.6),避免 CSS 預設 1 造成閃爍 |
| 動畫衝突處理 | .stop(true) 清除動畫佇列,防止快速移入移出疊加 |
| 效能優化 | 快取 $imgs,避免重複選擇器查詢 |
| 滑鼠離開判定 | 綁定在容器 #gallery 的 mouseleave,而非單張圖片,避免在圖片間移動時閃爍 |
| CSS Transition 替代 | 若不需 jQuery 動畫佇列控制,可改用純 CSS transition: opacity 0.3s + :hover 更高效 |
.gallery li img {
opacity: 0.6;
transition: opacity 0.3s ease, transform 0.3s ease;
}
.gallery li a:hover img {
opacity: 0.95;
transform: scale(1.02); /* 輕微放大增強手感 */
}
.gallery:hover li img {
opacity: 0.6; /* 容器 hover 時所有半透明 */
}
.gallery li a:hover img {
opacity: 0.95; /* 但當前 hover 的保持高亮 (優先級較高) */
}
注意:CSS 方案中 .gallery:hover li img 會在滑鼠進入容器瞬間將所有圖變半透明,再由 a:hover img 覆蓋當前高亮,視覺效果相同但無 JS 開銷。
<li>
<a href="#">
<img src="...">
<div class="overlay">
<h3>圖片標題</h3>
<p>描述文字</p>
</div>
</a>
</li>
.overlay {
position: absolute; inset: 0; background: rgba(0,0,0,0.5);
color: #fff; display: flex; flex-direction: column; justify-content: center; align-items: center;
opacity: 0; transition: opacity 0.3s; pointer-events: none;
}
.gallery li a:hover .overlay { opacity: 1; }
滑鼠滑過高亮遮罩是經典交互模式。jQuery 版本適合舊專案或需精細控制動畫佇列的場景;現代專案優先使用 純 CSS 方案,效能更好、無依賴、支援 GPU 加速。關鍵在於:容器層級 mouseleave 統一恢復、.stop() 防佊列、快取選擇器。