把選單做成一個可以拖著走的浮動視窗,是早期個人網站很愛用的手法。這個範例把好幾個效果疊在一起:邊框閃爍、滑入時的透明度漸變、用 revealTrans 做的進出轉場,以及按住標題列就能拖曳整個面板的互動。
flashit() 每隔半秒把表格邊框顏色在黑色與灰色之間切換,製造呼吸燈般的閃爍;high() 與 low() 一組,用 setInterval 逐步把 filters.alpha.opacity 加上去,做出由淡到亮的漸入,移開時則清除計時器並把透明度降回 50;Show() 與 Hide() 則呼叫 revealTrans 播放轉場;最後 move() 搭配 onmousedown/onmouseup 完成拖曳。
<!-- 第一步:把如下代碼加入到 <head> 區域中 -->
<style>
body,td,a {font-size:9pt;color:black;text-decoration:none}
.normal {padding:2px}
.up {padding:1px;border:#999999 1px solid;background:#cccccc}
.down {padding:1px;border:dbdbdb 1px solid;background:dbdbdb}
.menuv {filter: revealTrans(transition=23,duration=0.5);padding:2px}
</style>
<!-- 第二步:把如下代碼加入到 <body> 區域中 -->
<script>
// 閃爍的表格邊框
function flashit(){
if(!document.all) return;
if (td123.style.borderColor == "black") td123.style.borderColor = "#999999";
else td123.style.borderColor = "black"
}
setInterval("flashit()",500)
// 漸入的效果,改變 alpha 值
function high(image){
theobject = image
highlighting = setInterval("highlightit(theobject)",100)
}
function low(image){
clearInterval(highlighting)
image.filters.alpha.opacity = 50
}
function highlightit(cur2){
if (cur2.filters.alpha.opacity < 100) cur2.filters.alpha.opacity += 20;
else if(window.highlighting) clearInterval(highlighting)
}
// 拖動層的 JS
var over = false, down = false, divleft, divtop;
function move(){
if(down){
plane.style.left = event.clientX - divleft;
plane.style.top = event.clientY - divtop;
}
}
// 滑動層
function bodyload() {
if(plane.style.pixelTop != 0) {
plane.style.pixelTop -= 10
setTimeout('bodyload()',1)
}
}
// 漸變顯示層
function Show(divid) {
divid.filters.revealTrans.apply();
divid.style.visibility = "visible";
divid.filters.revealTrans.play();
}
function Hide(divid) {
divid.filters.revealTrans.apply();
divid.style.visibility = "hidden";
divid.filters.revealTrans.play();
}
</script>
<div style="position:absolute;z-index:1;width:1;height:1;left:0;top:500"
ID=plane
onmousedown="down=true;divleft=event.clientX-parseInt(plane.style.left);divtop=event.clientY-parseInt(plane.style.top)"
onmouseup="down=false">
<table id="td123" cellspacing=2 width=100 border=0 cellpadding="0"
bgcolor="f4f4f4" style="BORDER:#999999 1px solid;cursor:hand">
<tr>
<td bgcolor="#cccccc" height="20"
style="BORDER:#999999 1px solid;cursor:move">
<div align="center">多級滑動菜單</div>
</td>
</tr>
<tr>
<td class=normal
onMouseDown="this.className='down'"
onMouseOver="this.className='up',high(this),Show(menu1)"
onMouseOut="this.className='normal',low(this),menu1.style.visibility='hidden'"
onMouseUp="this.className='up'"
style="filter:alpha(opacity=50)">
<div align="right">有下級菜單>></div>
</td>
</tr>
<!-- 其餘選項依此格式複製即可 -->
</table>
</div>這些效果放到今天幾乎都有現成的標準替代品:透明度漸變用 CSS opacity 加 transition,轉場用 @keyframes,邊框閃爍用 animation,拖曳則可以用 Pointer Events 搭配 transform 實作,而且不必再依賴 filters 這種 IE 私有屬性。保留這段程式碼的價值,在於它示範了「多個小效果如何組合成一個完整互動元件」——這個思路到現在都還管用。