js實(shí)現(xiàn)宇宙星空背景效果的方法
本文實(shí)例講述了js實(shí)現(xiàn)宇宙星空背景效果的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<!DOCTYPE html PUBLIC "-//W3C//h2D XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/h2D/xhtml1-transitional.h2d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<TITLE>宇宙星空背景效果,Js特效</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.star {
position:absolute;
layer-background-color:white;
visibility:visible;
top:-50px;
width:50px;
height:50px;
font-size:1px;
background-color:white;
}
.60pt{font-size:60pt;color:#ff66cc;}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
var starnum = 75; // 星星的數(shù)目
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) { //定義星星移動(dòng)的起始位置
obj.deltay = Math.floor(Math.random() * 12); //用隨機(jī)函數(shù)決定
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 = 1;
obj.clip.height = 1;
obj.moveTo(xoffset+pageXOffset, yoffset+pageYOffset);
} else {
obj.width = 1;
obj.height = 1;
obj.pixelTop = yoffset+document.body.scrollTop;
obj.pixelLeft = xoffset+document.body.scrollLeft;
}
}
function movestar(starN) //移動(dòng)星星的位置
{
tmpx = starN.deltax*starN.counter+starN.counter;
tmpy = starN.deltay*starN.counter+starN.counter;
if (isNS) {
starN.clip.width = starN.counter / 3;
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.counter/3;
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() //讓所有的星星動(dòng)起來(lái)
{
for(i=1; i <= starnum; i++) {
movestar(eval('star'+i));
}
setTimeout('animate()', 100);
}
function findwindowparams() { //定義星星移動(dòng)的起始位置
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 (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>
<body bgcolor=#999999>
<SCRIPT LANGUAGE="JavaScript">
<!--
for (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=60pt><br>飛翔在<br>宇宙的星空中<br></font>
</center><p>
</body>
</html>
運(yùn)行效果如下圖所示:

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
帶大家了解一下JavaScript常見的五個(gè)內(nèi)存錯(cuò)誤
這篇文章主要為大家介紹了JavaScript常見的五個(gè)內(nèi)存錯(cuò)誤,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01
JavaScript中clientWidth,offsetWidth,scrollWidth的區(qū)別
這篇文章主要介紹了Element中clientWidth,offsetWidth,scrollWidth的區(qū)別,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下2021-01-01
TypeScript與JavaScript多方面闡述對(duì)比相同點(diǎn)和區(qū)別
TypeScript和JavaScript在開發(fā)交互式網(wǎng)頁(yè)方面有許多相似之處,在提供TypeScript與 JavaScript的直接比較時(shí),我們可以說JavaScript是一種輕量級(jí)的解釋型動(dòng)態(tài)語(yǔ)言,用于增強(qiáng)HTML網(wǎng)頁(yè),TypeScript是JavaScript的增強(qiáng)版,這意味著TypeScript是JavaScript和其他一些特性的組合2024-07-07
Google 靜態(tài)地圖API實(shí)現(xiàn)代碼
Google 靜態(tài)地圖 文檔說的很詳細(xì),這里就不在啰嗦了!2010-11-11
微信小程序?qū)崿F(xiàn)錄音時(shí)的麥克風(fēng)動(dòng)畫效果實(shí)例
這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)錄音時(shí)的麥克風(fēng)動(dòng)畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
BootStrap響應(yīng)式導(dǎo)航條實(shí)例介紹
響應(yīng)式導(dǎo)航條就是可以在不同的設(shè)備下查看不同的效果。這篇文章主要介紹了BootStrap響應(yīng)式導(dǎo)航條實(shí)例介紹的相關(guān)資料,小編認(rèn)為本文介紹的非常的不錯(cuò),特此分享給大家,供大家參考2016-05-05
微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
JS樹形結(jié)構(gòu)根據(jù)id獲取父級(jí)節(jié)點(diǎn)元素的示例代碼
這篇文章主要介紹了JS樹形結(jié)構(gòu)根據(jù)id獲取父級(jí)節(jié)點(diǎn)元素,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
如何使用bootstrap框架 bootstrap入門必看!
如何使用bootstrap?這篇文章就是告訴大家如何使用bootstrap框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
js 模擬實(shí)現(xiàn)類似c#下的hashtable的簡(jiǎn)單功能代碼
越來(lái)越感覺js對(duì)集合的處理沒有c#強(qiáng)大。比如在實(shí)際開發(fā)中,經(jīng)常用到在一維數(shù)組或者二維數(shù)組里取某一個(gè)滿足某些條件的項(xiàng),通常的處理方式就是遍歷數(shù)組,對(duì)比條件,匹配就取出,然后結(jié)束循環(huán)。2010-01-01

