接續上一篇,這裡再整理幾個實用(或有趣)的網頁效果,每則都以「問題 → 解法」的方式呈現。
把以下程式碼加到需要自動滾屏的頁面:
<script language="JavaScript">
var currentpos, timer;
function initialize() {
timer = setInterval("scrollwindow()", 1);
}
function sc() {
clearInterval(timer);
}
function scrollwindow() {
currentpos = document.body.scrollTop;
window.scroll(0, ++currentpos);
if (currentpos != document.body.scrollTop) { sc(); }
}
document.onmousedown = sc;
document.ondblclick = initialize;
</script>
原理很單純:用 setInterval 每毫秒把捲動位置往下推一格,直到抵達底部(scrollTop 不再增加)就自動停止;任何滑鼠按下都會中止。實作時建議把間隔調成 20~30 毫秒,1 毫秒不但沒有比較順,反而會讓 CPU 空轉。
效果是:滑鼠移入連結時,在游標旁出現一個帶有跑馬燈文字的小提示框,移出後消失。
<a href="https://example.com" target="_blank"
onMouseOver="helpor_net_show(this,event,'這裡是說明文字')"
onMouseOut="helpor_net_hide()">把滑鼠放上來試試</a>
<div id="tooltip2"
style="position:absolute; visibility:hidden; clip:rect(0 150 50 0);
width:150px; background-color:seashell">
<layer name="nstip" width="1000px" bgColor="seashell"></layer>
</div>
<script language="JavaScript">
<!--
if (!document.layers && !document.all) event = "test";
function helpor_net_show(current, e, text) {
if (document.all && document.readyState == "complete") {
document.all.tooltip2.innerHTML =
'<marquee style="border:1px solid #3399ff">' + text + '</marquee>';
document.all.tooltip2.style.pixelLeft = event.clientX + document.body.scrollLeft + 10;
document.all.tooltip2.style.pixelTop = event.clientY + document.body.scrollTop + 10;
document.all.tooltip2.style.visibility = "visible";
} else if (document.layers) {
document.tooltip2.document.nstip.document.write('<strong>' + text + '</strong>');
document.tooltip2.document.nstip.document.close();
document.tooltip2.document.nstip.left = 0;
currentscroll = setInterval("scrolltip()", 100);
document.tooltip2.left = e.pageX + 10;
document.tooltip2.top = e.pageY + 10;
document.tooltip2.visibility = "show";
}
}
function helpor_net_hide() {
if (document.all) document.all.tooltip2.style.visibility = "hidden";
else if (document.layers) {
clearInterval(currentscroll);
document.tooltip2.visibility = "hidden";
}
}
function scrolltip() {
if (document.tooltip2.document.nstip.left >=
-document.tooltip2.document.nstip.document.width) {
document.tooltip2.document.nstip.left -= 5;
} else {
document.tooltip2.document.nstip.left = 150;
}
}
//-->
</script>
幾個關鍵:用 position: absolute 讓提示框脫離文件流、用 pixelLeft/pixelTop 加上捲動量做定位(這樣捲動後位置才不會跑掉)、用 clip 控制可視範圍、用 visibility 而非 display 切換(避免版面重排)。現代的等價做法是純 CSS 的 :hover 搭配 ::after,或直接用 title 屬性與 aria-label 兼顧無障礙。
這個效果會像打字機一樣,把一段文字一個字一個字顯示出來,做完一輪後停頓再換下一則,很適合當首頁公告。
第一步,把指令碼放進 <head>:
<script language="JavaScript">
<!--
function textlist() {
max = textlist.arguments.length;
for (i = 0; i < max; i++) this[i] = textlist.arguments[i];
}
tl = new textlist(
"隨著 INTERNET 時代的到來,人們對網路的認識與感知越來越深",
"上網主要是進行 web 頁面瀏覽,所以頁面的精彩程度對網站至關重要",
"許多人透過製作個人主頁來展現自己的才華",
"但是光用軟體就能做出你想要的各種頁面效果嗎?答案是否定的",
"JavaScript 是 Netscape 公司首先推出的一種針對網頁的解釋型語言"
);
var x = 0, pos = 0;
var l = tl[0].length;
function textticker() {
document.tickform.tickfield.value = tl[x].substring(0, pos) + "_";
if (pos++ == l) {
pos = 0;
setTimeout("textticker()", 2000); // 一則顯示完畢後停 2 秒
if (++x == max) x = 0;
l = tl[x].length;
} else {
setTimeout("textticker()", 50); // 每 50 毫秒吐一個字
}
}
// -->
</script>
第二步,在 <body> 中放承載用的表單:
<form name="tickform">
<textarea name="tickfield" rows="3" cols="38"
style="background-color: rgb(0,0,0); color: rgb(255,255,255);
font-family: Arial; font-size: 12px" wrap="virtual">
公告將在頁面載入完成後顯示
</textarea>
</form>
第三步,讓它啟動:
<body onload="textticker()">
三個可調參數:每個字的間隔(50 毫秒)、每則之間的停頓(2000 毫秒)、以及公告清單本身。若嫌 textarea 太笨重,把它換成 <div> 並改寫成 div.textContent = ... 即可。
用 VBScript 的 MsgBox 就能做到(僅 IE 有效):
<html> <head></head> <body></body> <script language="vbscript"> <!-- MsgBox "歡迎光臨我的網站!" --> </script> </html>
跨瀏覽器的版本用 JavaScript:
window.onload = function() { alert("歡迎光臨我的網站!"); };
不過要提醒:載入即跳出的對話框對使用者體驗的傷害很大(打斷閱讀、無法分享連結、在手機上尤其惱人),而且可能被瀏覽器阻擋。若真的需要告知訊息,建議改用頁面內部的橫幅或 toast 提示,並讓使用者可以關閉。
只要一行 meta 標籤,放在 <head> 區即可。
每隔一段時間重新整理:
<meta http-equiv="refresh" content="20">
其中的 20 代表每 20 秒重整一次。
一段時間後自動跳轉:
<meta http-equiv="refresh" content="20; url=https://www.example.com">
20 秒後跳到指定網址。實務上建議加上 0 秒立即跳轉並提供手動連結,同時留意:自動重整會打斷使用者填寫中的表單,對無障礙(螢幕閱讀器)也不友善,除非有明確技術需求,否則應避免。
這個效果會讓游標位置出現一橫一縱兩條線貫穿整個視窗,對某些特殊頁面(例如需要精準對位、測量)蠻有用。
<script language="JavaScript1.2">
<!--
if (document.all && !window.print) {
leftright.style.width = document.body.clientWidth - 2;
topdown.style.height = document.body.clientHeight - 2;
} else if (document.layers) {
document.leftright.clip.width = window.innerWidth;
document.leftright.clip.height = 1;
document.topdown.clip.width = 1;
document.topdown.clip.height = window.innerHeight;
}
function followmouse1() { // IE 4+
leftright.style.pixelTop = document.body.scrollTop + event.clientY + 1;
topdown.style.pixelTop = document.body.scrollTop;
if (event.clientX < document.body.clientWidth - 2)
topdown.style.pixelLeft = document.body.scrollLeft + event.clientX + 1;
else
topdown.style.pixelLeft = document.body.clientWidth - 2;
}
function followmouse2(e) { // NS 4+
document.leftright.top = e.y + 1;
document.topdown.top = pageYOffset;
document.topdown.left = e.x + 1;
}
if (document.all) document.onmousemove = followmouse1;
else if (document.layers) {
window.captureEvents(Event.MOUSEMOVE);
window.onmousemove = followmouse2;
}
function regenerate() { window.location.reload() }
function regenerate2() { setTimeout("window.onresize = regenerate", 400) }
if ((document.all && !window.print) || document.layers) window.onload = regenerate2;
//-->
</script>
<style>
<!--
#leftright, #topdown {
position: absolute; left: 0; top: 0;
width: 1px; height: 1px;
background-color: #00eeff;
z-index: 100; font-size: 1px;
}
-->
</style>
再把兩個線元素貼在 <body> 之後:
<div id="leftright" style="width:expression(document.body.clientWidth-2)"></div> <div id="topdown" style="height:expression(document.body.clientHeight-2)"></div>
想改線的顏色,改 background-color 的色碼即可。這裡的 expression() 是 IE 專屬語法,在標準瀏覽器中無效;現代的等價做法是用 100vw/100vh 單位搭配 position: fixed,或用 pointermove 事件更新 CSS 自訂屬性。
點擊頁面任意處,游標位置會噴散出彩色字符,像小型煙火。核心做法是預先準備十組軌跡座標陣列,每次點擊時依序移動對應的 div,再控制其顯示與隱藏:
<script language="JavaScript1.2">
<!-- Begin
var ver = navigator.appVersion;
var dom = document.getElementById ? 1 : 0;
var ie5 = (ver.indexOf("MSIE 5") > -1 && dom) ? 1 : 0;
var n = (document.layers);
var ie = (document.all);
var sparksAflyin = 0, totalSparks = 0, sparksOn = 1;
function initMouseEvents() {
if (n) document.captureEvents(event.mousedown | event.mousemove);
}
function SHOW(divName) {
if (document.all)
eval('document.all.' + divName + '.style.visibility = "visible";');
else if (document.layers)
eval('document.layers["' + divName + '"].visibility = "visible";');
}
function HIDE(divName) {
if (document.all)
eval('document.all.' + divName + '.style.visibility = "hidden";');
else if (document.layers)
eval('document.layers["' + divName + '"].visibility = "hide";');
}
// 十組不同的拋射軌跡(x 與 y 各自成對)
anim_0_x = new Array(20,20,10,0,0,0,0,0,0,0,0,0);
anim_0_y = new Array(-20,-40,-60,-80,-60,-40,-20,0,20,40,60,80);
anim_1_x = new Array(20,20,17,36,60,78,90,92,93,98,108,120,133,152,181);
anim_1_y = new Array(-20,-20,-33,-38,-38,-27,-2,25,51,84,113,141,162,212,253);
/* anim_2 到 anim_9 依此類推,每組長度可不同 */
// End -->
</script>
<body onload="initMouseEvents()">
<div id="sparks">
<div id="sDiv0" style="position:absolute; visibility:hidden">
<font face="arial black" color="red">a</font></div>
<div id="sDiv1" style="position:absolute; visibility:hidden">
<font face="arial black" color="yellow">b</font></div>
<div id="sDiv2" style="position:absolute; visibility:hidden">
<font face="arial black" color="blue">c</font></div>
<!-- 其餘 sDiv3 ~ sDiv9 結構相同,顏色各異 -->
</div>
設計上的巧思是:十個粒子共用同一組 DOM,每次點擊挑一組尚未使用的軌跡來跑,跑完就隱藏並歸還;累計十個同時飛散後(totalSparks == 10)就重置計數,形成循環。這種「物件池」概念在遊戲開發中非常常見,用來避免頻繁建立與銷毀物件。
若要在今日的專案重現類似效果,用 Canvas 或 CSS animation 會流暢得多,也記得加上 prefers-reduced-motion 的判斷,尊重使用者「減少動態效果」的系統設定。
這批特效寫於 2000 年代初期,當時的瀏覽器支援與效能都相當有限,因此處處可見 document.all、document.layers、eval、expression() 這類如今已被淘汰或不建議的寫法。它們的價值在於示範觀念:絕對定位與座標計算、定時器的啟動與清除、物件池的 reuse、以及事件監聽的掛載與移除。把這些觀念用標準 API 重新實作一次,你會發現程式碼短很多,也可靠很多。