這是一段很有年代感的程式碼,效果是把游標位置加上一團跳動的燭火光暈。它示範了兩件事:如何讓圖層緊跟滑鼠,以及如何用滤镜製造光感。不過要先說清楚——它只能在 IE8 及以下版本跑得起來。
<body id="mybody" style="filter:light(enabled=1);"> <div id="Layer1" style="position:absolute; top:41; left:66; width:52; height:32;"> <img src="images/light.gif" width="29" height="30"> </div>
<script language="javascript">
function move_layer() {
Layer1.style.left = event.clientX + document.body.scrollLeft - 50;
Layer1.style.top = event.clientY + document.body.scrollTop - 20;
}
document.onmousemove = move_layer;
</script>
這段是整份程式最值得學的部分:先用 clientX/clientY 取得游標在視窗中的位置,再加上 scrollLeft/scrollTop 換算成文件座標,最後減去 50 與 20 讓「光點」對準在游標上方而非重疊。這個「視窗座標 + 捲動量 = 文件座標」的換算,直到今天做拖曳、浮動元素仍然通用。
<script language="vbscript"> dim red, gre, blu, x, y, z red = 255 : gre = 255 : blu = 0 : z = 100 sub window_onload() call mybody.filters.light.addPoint(0, 0, z, Red, Gre, Blu, 100) mybody.style.cursor = "hand" end sub sub document_onmousemove() x = window.event.x y = window.event.y call mybody.filters.Light.MoveLight(0, x, y, 100, 1) end sub </script>
filters.light 是 IE 獨有的視覺滤镜,用 RGB(255,255,0,也就是黃光)在指定座標打出一個光源,並隨滑鼠移動。搭配 GIF 圖片,視覺上就成為一團會晃動的火燄。
這份程式同時依賴 VBScript、IE 專屬滤镜、全域 event 物件與 document.all,全都已不屬於標準 Web 技術。若你想做類似效果,現代理應改用:
cursor: url('candle.png'), auto;box-shadow、filter: drop-shadow() 或 mix-blend-modeaddEventListener('mousemove', e => e.clientX)把這份老程式當成「座標換算」與「光效思維」的教材來讀,價值比直接複製要高得多。