本文示範如何使用原生 JavaScript 搭配 CSS 濾鏡,打造經典的九宮格圖片畫廊:預設圖片半透明隱藏,滑鼠移入時逐漸淡入顯示,移出則淡出消失。這類效果常用於作品集、產品展示等場景。
.invisible 類別,利用 IE 專屬 filter: alpha(opacity=0) 將圖片初始設為完全透明。high()、low()、highlightit() 三個函式,透過 setInterval 每 50 毫秒遞增 10% 不透明度,達到漸顯動畫;移出時清除計時器並重置透明度為 0。<table> 排版九宮格,每個 <td> 內放置一張套用 .invisible 類別的圖片,並綁定 onmouseover、onmouseout 事件。<html>
<title>九宮格圖片半透明漸顯效果</title>
<body>
<STYLE type="text/css">
.invisible { FILTER: alpha(opacity=0) }
</STYLE>
<SCRIPT language="JavaScript1.2">
//<![CDATA[
var highlighting;
function high(which2) {
theobject = which2;
highlighting = setInterval("highlightit(theobject)", 50);
}
function low(which2) {
clearInterval(highlighting);
which2.filters.alpha.opacity = 0;
}
function highlightit(cur2) {
if (cur2.filters.alpha.opacity < 100)
cur2.filters.alpha.opacity += 10;
else if (window.highlighting)
clearInterval(highlighting);
}
//]]>
</SCRIPT>
<TABLE borderColor="#999999" cellSpacing="8" cellPadding="0" width="302" border="0">
<TBODY>
<TR>
<TD width="90" background="text1.gif" bgColor="#db4d0e" height="90">
<A href=""><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m01.jpg" border="0" width="180" height="135"></A>
</TD>
<TD width="90" background="text2.gif" bgColor="#ff9f07">
<A href="/"><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m02.jpg" border="0" width="180" height="135"></A>
</TD>
<TD width="90" background="text3.gif" bgColor="#ff9f07">
<A href=""><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m03.jpg" border="0" width="180" height="135"></A>
</TD>
</TR>
<TR>
<TD background="text4.gif" bgColor="#ff9f07">
<A href="/"><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m04.jpg" border="0" width="180" height="135"></A>
</TD>
<TD background="text5.gif" bgColor="#a5d523">
<A href="/"><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m05.jpg" border="0" width="180" height="135"></A>
</TD>
<TD background="text6.gif" bgColor="#c56e19">
<A href="/"><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m06.jpg" border="0" width="180" height="135"></A>
</TD>
</TR>
<TR>
<TD background="text12.gif" bgColor="#ff9f07">
<A href="/"><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m07.jpg" border="0" width="180" height="135"></A>
</TD>
<TD background="text8.gif" bgColor="#c56e19">
<A href="/"><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m08.jpg" border="0" width="180" height="135"></A>
</TD>
<TD background="text7.gif" bgColor="#c56e19">
<A href="/"><IMG class="invisible" onmouseover="high(this)" onmouseout="low(this)" src="/images/m09.jpg" border="0" width="180" height="135"></A>
</TD>
</TR>
</TBODY>
</TABLE>
</body>
</html>
filter: alpha(opacity=...) 僅在舊版 IE 生效,現代瀏覽器請改用標準 CSS opacity 屬性。transition 或 animation 實現,效能更好且程式碼更簡潔:.gallery img { opacity: 0; transition: opacity 0.3s ease; }
.gallery img:hover { opacity: 1; }
若需支援舊版 IE,可保留原寫法並搭配條件註釋或 Modernizr 偵測後再決定載入哪套邏輯。