圖片牆如果一律以實心狀態鋪滿,畫面會顯得擁擠而呆板。給滑鼠移入的格子加一段「從透明漸變到清晰」的過渡,整塊區域立刻有了呼吸感。下面這個例子用 3×3 的九宮格做演示,核心只有三個函數。
透過一個自訂 CSS 類把初始透明度打到 0,圖片在載入完成後是「隱形」的,只有滑鼠光臨才現身:
<STYLE type=text/css>
.invisible {
FILTER: alpha(opacity=0)
}
</STYLE>
這裡用的是 IE 專有的 alpha 過濾器,數值範圍 0 到 100。它並不是標準寫法,在現代專案中等價的表達是 opacity: 0;,兩者可以並列聲明以兼顧新老環境。
<SCRIPT language=JavaScript1.2>
<!--
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>
三者的分工非常清楚:
setInterval 每 50 毫秒呼叫一次 highlightit,形成動畫心跳。要注意 low 中的 clearInterval 必不可少。少了它,移出後計時器仍在背景累加,再移入時又開了一條新的計時器,動畫節奏很快就亂了。
版面用一個三行三列的表格承載,每個格子裡放一個包在連結中的圖片,並掛上移入/移出事件:
<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=180px height=135px></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=180px height=135px></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=180px height=135px></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=180px height=135px></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=180px height=135px></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=180px height=135px></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=180px height=135px></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=180px height=135px></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=180px height=135px></A></TD>
</TR>
</TBODY></TABLE>
每個 TD 都鋪了一張 background 底圖並配了不同的 bgColor。這個設計有點意思:當 IMG 完全透明時,格子並不是空白,而是露出底色,滑鼠移入後圖片才一層層「浮」上來,視覺層次比單純的顯示/隱藏要豐富得多。
如果今天重寫這段效果,可以完全交給 CSS 轉場,不必動用計時器:
.tile img {
opacity: 0;
transition: opacity .5s ease;
}
.tile img:hover {
opacity: 1;
}
瀏覽器會自行合成中間幀,動畫更順滑,程式碼量也少了三分之二。之所以保留 setInterval 的舊寫法,一方面是為了說明「逐幀疊加」這個通用思路——沒有 CSS 轉場可用的環境下,這仍是最直白的實現手段;另一方面,當你需要的不只是透明度,而是多屬性聯動(例如透明度上升的同時位置平移、色彩偏移),手動控制每一幀反而更靈活。