本文彙整了三十三個實用的 JavaScript 經典效果,涵蓋日常開發與頁面美化常見的需求:
一、實用且必用的小腳本程式碼
二、滑鼠旁邊的提示資訊,類似 163 登錄後的頁面提示效果
三、文字過長時,將過長部分變成省略號顯示
四、滾動的圖片
五、接收鍵盤指令的腳本
六、讓文字連結漸隱漸顯
七、類似 QQ 好友/黑名單的樹型選單(推薦)
八、多種腳本翻頁(推薦)
九、DIV 的透明層實現
十、JSP 頁面自動生成 HTML 頁面(推薦)
十一、超強大的表單驗證(推薦)
十二、漂亮的腳本日曆
十三、進入、退出頁面的各種效果
十四、表格被選中變色
十五、彈出提示的效果
十六、圖片之間的切換
十七、DIV 圓邊圓角的實現
十八、跳動的選單
十九、透過頁面抓取照片
二十、客戶端靜態頁面玩分頁
二十一、類似 Google 個性頁面的好東西
二十二、漂亮的表格
二十三、經典的帶陰影可拖動浮動層(推薦)
二十四、運行程式碼的程式碼
二十五、凹陷文字
二十六、漂亮的仿 Flash 選單
二十七、自訂容器和字體大小
二十八、超 REAL 影片播放器
二十九、論壇快捷鍵提交表單
三十、accesskey 提交
三十一、新聞廣告圖片切換效果+描述
三十二、選單特效
三十三、採用 CSS 和 JS 的下拉選單
以下挑選幾個最具代表性的做重點說明。
腳本 1:進入主頁後自動播放聲音
<embed src="pnm://10.13.31.90/~kayvin/mihunji.rm" hidden=true autostart=true loop=true>
腳本 2:進入主頁後自動最大化
<script> self.moveTo(0, 0) self.resizeTo(screen.availWidth, screen.availHeight) </script>
腳本 3:顯示現在時間
<script language="vbscript">document.write now</script>
腳本 4:顯示最後修改時間
<script>document.write(document.lastModified)</script>
腳本 6:狀態欄動態顯示現在時間
<script>
function see() {
window.setTimeout("see()", 1000);
today = new Date();
self.status = today.toString();
}
</script>
<body onload="see()">
腳本 7:關閉視窗
<a href="javascript:close()">[關閉視窗]</a>
腳本 9:後退、刷新、前進
<input type="button" value="後退" onclick="history.go(-1)"> <input type="button" value="刷新" onclick="history.go(-0)"> <input type="button" value="前進" onclick="history.go(+1)">
進入頁面 <meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)">;退出頁面 <meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)">。duration 表示特效持續時間(秒),transition 表示使用哪種特效,取值 1~23,例如 0 矩形縮小、1 矩形擴大、2 圓形縮小、8 豎百葉窗、9 橫百葉窗、12 點擴散、23 隨機選擇等。
<div id="Layer1" style="position:absolute; width:260px; height:115px; z-index:1; left:50px; top:77px; filter:Alpha(opacity=30)"> ... </div>
透過 CSS 的 filter:Alpha(opacity=30) 就能輕鬆做出半透明浮層。
利用 li:hover ul 顯示子選單,再搭配一段 JS 為 IE 補上 hover 類別:
startList = function () {
if (document.all && document.getElementById) {
navRoot = document.getElementById("nav");
for (i = 0; i < navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName == "LI") {
node.onmouseover = function () { this.className += " over"; }
node.onmouseout = function () { this.className = this.className.replace(" over", ""); }
}
}
}
}
window.onload = startList;
這種「CSS 負責呈現、JS 補足舊 IE 相容」的下拉選單,是無 Flash、純前端的經典做法,至今仍具參考價值。
以上三十三個效果從實用小腳本、選單、日曆、表單驗證到浮動層、圖片切換應有盡有,是前端學習與速查的寶貴素材庫。