圖片牆上滑過去的時候浮出一層文字說明,是作品集與商品列表常見的互動模式。這裡示範一個以純 CSS hover 為主軸的實作,不需要額外 JavaScript。

每個項目由「圖片區」與「說明區」兩層組成,說明區預設隱藏,滑鼠移入時才浮現:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>滑鼠移到圖片上顯示文字效果</title>
<style>
.explore {
width: 936px;
margin: 20px auto 0;
overflow: hidden;
position: relative;
font-size: 0px; /* 消除 inline-block 元素間的空白縫隙 */
}
.explore li {
width: 160px;
height: 150px;
overflow: hidden;
display: inline-block;
position: relative;
}
.explore li { *display: inline; } /* IE6/7 hack */
.explore li .wqPic {
width: 160px;
height: 150px;
overflow: hidden;
}
.explore .wqLink {
display: block;
width: 160px;
height: 150px;
color: #FFF;
text-align: center;
font-family: "微軟雅黑";
}
/* 預設狀態:底部顯示標題,並鋪一層半透明黑底 */
.explore .wqItem .wqName {
position: absolute;
bottom: 0;
left: 0;
width: 130px;
height: 40px;
line-height: 40px;
font-size: 16px;
overflow: hidden;
padding: 0 10px;
}
.explore .wqItem .bg {
background: #333;
opacity: 0.8;
filter: alpha(opacity = 80);
position: absolute;
bottom: 0;
left: 0;
width: 150px;
height: 40px;
}
.explore .wqLink:hover { cursor: pointer; text-decoration: none; }
/* hover 時:遮罩撐滿整個區塊,原標題隱藏,詳細說明浮現 */
.explore .wqLink:hover .wqItem .bg { height: 150px; }
.explore .wqLink:hover .wqItem .wqName { visibility: hidden; }
.explore .wqLink:hover .detail { visibility: visible; }
.explore .detail {
background: #000;
position: absolute;
top: 128px;
left: 0;
width: 160px;
line-height: 22px;
height: 22px;
font-size: 12px;
filter: alpha(opacity = 65);
}
.explore .detail .wqName { font-size: 16px; padding: 0 10px; line-height: 22px; }
.explore .detail .info { margin-top: 10px; }
</style>
</head>
<body>
<div id="topWrap">
<div class="wqSquare">
<div class="explore">
<ul>
<li>
<a onmousedown="MI.Bos('wQBtnBigImgQun')" href="#" class="wqLink">
<div class="wqItem">
<img src="https://files.jb51.net/file_images/article/201508/201587150400220.jpg" class="wqPic" />
</div>
<div class="detail">
<div class="wqName">天天好心情</div>
</div>
</a>
</li>
<li>
<a onmousedown="MI.Bos('wQBtnBigImgQun')" href="#" class="wqLink">
<div class="wqItem">
<img src="https://files.jb51.net/file_images/article/201508/201587150400220.jpg" class="wqPic" />
</div>
<div class="detail">
<div class="wqName">天天好心情</div>
</div>
</a>
</li>
</ul>
</div>
</div>
</div>
</body>
</html>
遮罩的透明度處理。這裡用 opacity 搭配 filter: alpha() 兼顧舊版 IE。要注意的是 opacity 會連子元素一起變透明,所以文字會跟著變淡。若想讓文字保持不透明,應該改用 rgba() 背景色,例如 background: rgba(0,0,0,0.8)。
inline-block 的空白縫隙。父層設 font-size: 0 是為了消除 HTML 中標籤之間的換行所產生的空白。若不想動字級,也可以改用 flex 排版,或讓 HTML 標籤緊密相連。
用 visibility 而非 display。切換說明文字時用 visibility: hidden 而非 display: none,是為了避免元素從排版流程中消失造成跳動。
可加裝 transition 讓效果更順。在 .detail 與 .bg 上加 transition: all .3s ease,切換就會有淡入淡出的過場,不必額外寫 JavaScript。
整體而言,這個效果的重點不在程式碼多寡,而在於把「預設狀態」與「hover 狀態」兩組樣式想清楚,再決定要用什麼屬性做切換。想清楚這層,替換成圖片縮放、遮罩上滑或文字淡入都只是改幾行 CSS 的事。