朋友請我幫忙做一個圖片高亮展示的效果。需求不複雜,但做法有點巧思:滑鼠移入某張縮圖時,讓該圖維持正常亮度,其餘部分則被一層半透明黑幕蓋住,同時在下方顯示圖片名稱。
作法是預先放一層 #trans 半透明黑色遮罩鋪在整組圖片上,滑入時把該圖的 z-index 提到遮罩之上,它就自然「浮」出來了。
<!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>name</title>
<style type="text/css">
*{}{margin:0; padding:0;}
a img{}{border:none;}
#imagesBox {
width:165px;
height:110px;
position:relative;
left:100px;
top:100px;
z-index:1;
border:1px solid black;
background-color:white;
}
#trans{
width:165px;
height:110px;
position:absolute;
left:0;
top:0;
z-index:2;
display:none;
opacity:0.5;
filter:alpha(opacity=50);
background-color:black;
}
#imagesBox li {
width:55px;
height:55px;
float:left;
overflow:hidden;
}
#imagesBox li a,#imagesBox li img {
width:55px;
height:55px;
display:block;
cursor:pointer;
}
#imagesBox li img{
position:relative;
}
#imagesBox strong{
position:absolute;
left:0;
top:50%;
margin-top:-7px;
z-index:999;
display:block;
color:white;
width:100%;
height:15px;
text-align:center;
}
</style>
<script type="text/javascript">
function gallery(obj){
var tit=obj.getAttribute("alt");
document.getElementById("show_tit").firstChild.nodeValue=tit;
document.getElementById("trans").style.display='block';
obj.style.zIndex="3";
}
function clearBg(obj){
document.getElementById("show_tit").firstChild.nodeValue="";
document.getElementById("trans").style.display='none';
obj.style.zIndex="1";
}
</script>
</head>
<body>
<div id="imagesBox">
<ul>
<li><a href="#" title="名稱1"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱1"/><span></span></a></li>
<li><a href="#" title="名稱2"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱2"/><span></span></a></li>
<li><a href="#" title="名稱3"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱3"/><span></span></a></li>
<li><a href="#" title="名稱4"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱4"/><span></span></a></li>
<li><a href="#" title="名稱5"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱5"/><span></span></a></li>
<li><a href="#" title="名稱6"><img onmouseover="gallery(this);" onmouseout="clearBg(this)" src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱6"/><span></span></a></li>
</ul>
<div id="trans"> </div>
<strong id="show_tit"> </strong>
</div>
</body>
</html>
一開始因為沒完全理解設計意圖,我先做了另一個版本:把遮罩層放進每個 <li> 裡,滑入時才隱藏它。這個做法的好處是不需要 JavaScript 去控制 z-index,純 CSS 的 :hover 就能搞定;缺點是每張圖都要多一個元素。
<!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>name</title>
<style type="text/css">
*{margin:0; padding:0;}
a img{border:none;}
#imagesBox {
width:165px;
height:100px;
position:relative;
left:100px;
top:100px;
}
#imagesBox li {
width:55px;
height:55px;
float:left;
overflow:hidden;
position:relative;
}
#imagesBox li img {
width:55px;
height:55px;
position:relative;
z-index:2;
}
#imagesBox li span {
line-height:100px;
background:#000;
position:absolute;
top:0;
left:0;
z-index:3;
opacity:0.5;
filter:alpha(opacity=50);
display:block;
width:55px;
height:55px;
}
#imagesBox li a:hover span{display:none;}
#imagesBox strong{
position:absolute;
left:0;
top:50%;
margin-top:-7px;
z-index:999;
display:block;
color:white;
width:100%;
height:15px;
text-align:center;
}
</style>
<script type="text/javascript">
function gallery(obj){
var tit=obj.getAttribute("title");
document.getElementById("show_tit").firstChild.nodeValue=tit;
document.getElementById("imagesBox").onmouseout=function(){
document.getElementById("show_tit").firstChild.nodeValue="這裡顯示 名字"
};
}
</script>
</head>
<body>
<div id="imagesBox">
<ul>
<li><a href="#" onmouseover="gallery(this);" title="名稱1"><img src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱1"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名稱2"><img src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱2"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名稱3"><img src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱3"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名稱4"><img src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱4"/><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名稱5"><img src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱5"><span></span></a></li>
<li><a href="#" onmouseover="gallery(this);" title="名稱6"><img src="/images/artist//12053006018793_3.jpg" height="55" width="55" alt="名稱6"/><span></span></a></li>
</ul>
<strong id="show_tit">這裡顯示名字</strong>
</div>
</body>
</html>
版本一用一個共享遮罩搭配 z-index 切換,DOM 結構乾淨、擴充到幾十張圖也不用改 CSS;版本二靠純 CSS 的 :hover 完成視覺切換,JS 只負責文字,關注點分離得更徹底。實際上我個人會偏好版本二的思路,再把 onmouseover 這類行內事件換成 addEventListener,並把 firstChild.nodeValue 改成 textContent——後者不必擔心空白節點,語意也更清楚。
希望本文所述對大家 JavaScript 程式設計有所幫助。