這個特效能讓一段文字以「由暗到亮、再由亮到暗」的方式循環漸變,並在每則訊息之間切換,相當適合用在歡迎語或首頁的動態標語。它主要是利用 IE 的 glow 濾鏡,搭配計時器逐步改變顏色深淺來呈現消隱(淡入淡出)的視覺感受。
<SCRIPT language="JavaScript">
var thissize=20
var textfont="隸書"
var textcolor= new Array()
textcolor[0]="000000"
textcolor[1]="000000"
textcolor[2]="000000"
textcolor[3]="111111"
textcolor[4]="222222"
textcolor[5]="333333"
textcolor[6]="444444"
textcolor[7]="555555"
textcolor[8]="666666"
textcolor[9]="777777"
textcolor[10]="888888"
textcolor[11]="999999"
textcolor[12]="aaaaaa"
textcolor[13]="bbbbbb"
textcolor[14]="cccccc"
textcolor[15]="dddddd"
textcolor[16]="eeeeee"
textcolor[17]="ffffff"
textcolor[18]="ffffff"
var message = new Array()
message[0]="歡迎光臨素材"
message[1]="請多停留一會"
message[2]="你會有更多的收獲"
message[3]="請再次光臨"
i_message=0
var i_strength=0
var timer
function glowtext() {
if(document.all) {
if (i_strength <= 17) {
glowdiv.innerText=message[i_message]
document.all.glowdiv.style.filter="glow(color="+textcolor[i_strength]+", strength=4)"
i_strength++
timer=setTimeout("glowtext()",100)
} else {
clearTimeout(timer)
setTimeout("deglowtext()",1500)
}
}
}
function deglowtext() {
if(document.all) {
if (i_strength >= 0) {
glowdiv.innerText=message[i_message]
document.all.glowdiv.style.filter="glow(color="+textcolor[i_strength]+", strength=4)"
i_strength--
timer=setTimeout("deglowtext()",100)
} else {
clearTimeout(timer)
i_message++
if (i_message>=message.length) {i_message=0}
i_strength=0
intermezzo()
}
}
}
function intermezzo() {
glowdiv.innerText=""
setTimeout("glowtext()",1000)
}
</SCRIPT>
<div id="glowdiv" style="position:absolute; visibility:visible; width:600px; text-align:center; top:185px; left:70px; font-family:隸書; font-size:30pt; color:000000"></div>
把 <body> 改為下列寫法,讓頁面載入時啟動動畫:
<body bgcolor="#fef4d9" onLoad="glowtext()">
程式透過 textcolor 陣列存放從深到淺的灰階色碼,glowtext() 讓索引遞增(漸亮)、deglowtext() 讓索引遞減(漸暗),兩者以 setTimeout 每 100 毫秒更新一次 glow 濾鏡的顏色,達到消隱漸變效果,並在訊息之間以 intermezzo() 做短暫停頓後切換下一則。
需注意的是,glow 濾鏡與 document.all 屬於 IE 專屬 API,在現代瀏覽器中不會生效。若要在跨平台呈現類似淡入淡出效果,建議改用 CSS 的 opacity 搭配 transition 來實作。