
div 元素,絕對定位。setInterval 週期性更新所有星星座標與尺寸,超出視窗邊界則重置位置。<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>宇宙星空背景效果,JS特效</title>
<style>
.star { position:absolute; background:white; width:1px; height:1px; font-size:1px; top:-50px; visibility:visible; }
.title-text { font-size:60pt; color:#ff66cc; text-align:center; }
</style>
<script>
var starnum = 75;
var isNS = (document.layers);
var _all = isNS ? '' : 'all.';
var _style = isNS ? '' : '.style';
var xoffset, yoffset, w_x, w_y, tmpx, tmpy, scrlx, scrly;
function getstartpos(obj) {
obj.deltay = Math.floor(Math.random() * 12);
obj.deltax = Math.floor(Math.random() * 12);
obj.xdir = (Math.floor(Math.random() * 2) == 1) ? '+' : '-';
obj.ydir = (Math.floor(Math.random() * 2) == 1) ? '+' : '-';
obj.counter = 1;
if (isNS) {
obj.clip.width = obj.clip.height = 1;
obj.moveTo(xoffset + pageXOffset, yoffset + pageYOffset);
} else {
obj.width = obj.height = 1;
obj.pixelTop = yoffset + document.body.scrollTop;
obj.pixelLeft = xoffset + document.body.scrollLeft;
}
}
function movestar(starN) {
tmpx = starN.deltax * starN.counter + starN.counter;
tmpy = starN.deltay * starN.counter + starN.counter;
if (isNS) {
starN.clip.width = starN.clip.height = starN.counter / 3;
scrlx = pageXOffset; scrly = pageYOffset;
if ((starN.left+tmpx >= w_x+scrlx) || (starN.top+tmpy >= w_y+scrly) ||
(starN.left-tmpx <= scrlx) || (starN.top-tmpy <= scrly)) {
getstartpos(starN);
} else {
eval('starN.moveBy('+starN.xdir+tmpx+', '+starN.ydir+tmpy+')');
}
} else {
starN.width = starN.height = starN.counter/3;
scrlx = document.body.scrollLeft; scrly = document.body.scrollTop;
if ((starN.pixelLeft+tmpx >= w_x+scrlx)||(starN.pixelTop+tmpy >= w_y+scrly) ||
(starN.pixelLeft-tmpx <= scrlx)||(starN.pixelTop-tmpy <= scrly)) {
getstartpos(starN);
} else {
eval('starN.pixelTop'+starN.ydir+'=tmpy');
eval('starN.pixelLeft'+starN.xdir+'=tmpx');
}
}
starN.counter++;
}
function animate() {
for(var i=1; i <= starnum; i++) movestar(eval('star'+i));
setTimeout('animate()', 100);
}
function findwindowparams() {
w_x = isNS ? window.innerWidth : document.body.clientWidth;
w_y = isNS ? window.innerHeight : document.body.clientHeight;
xoffset = w_x / 2; yoffset = w_y / 2;
for (var i = 1; i <= starnum; i++) getstartpos(eval('star'+i));
}
function resizeNS() { setTimeout('document.location.reload()', 400); }
isNS ? window.onresize = resizeNS : window.onresize = findwindowparams;
window.onload = new Function("findwindowparams(); animate();");
</script>
</head>
<body bgcolor="#999999">
<script>
for (var i = 1; i <= starnum; i++) {
document.writeln('<div id="star'+i+'" class="star"></div>');
eval('var star'+i+'=document.'+_all+'star'+i+_style);
}
</script>
<p><center><font class="title-text"><br>飛翔在<br>宇宙的星空中<br></font></center></p>
</body>
</html>
isNS 判斷瀏覽器類型,分別操作 layer (Netscape 4) 或 style (IE/現代瀏覽器)。document.writeln 配合 eval 批量生成星星 DOM 並建立變數引用。requestAnimationFrame + Canvas。此特效展示了早期 DHTML 動畫精髓,雖屬經典技法,但邏輯清晰,適合初學者理解 DOM 動畫基礎。現代專案建議改用 Canvas/WebGL 實現粒子系統以獲得更佳效能與視覺效果。