javascript實現(xiàn)隨機(jī)顯示星星特效
更新時間:2016年01月28日 08:55:33 作者:gyz418
這篇文章主要介紹了javascript實現(xiàn)隨機(jī)顯示星星特效的相關(guān)資料,以一個完整實例形式較為詳細(xì)的分析了js實現(xiàn)隨機(jī)顯示星星特效的實現(xiàn)技巧,需要的朋友可以參考下
本文實例講解了javascript實現(xiàn)隨機(jī)顯示星星特效的詳細(xì)代碼,具體內(nèi)容如下
- (1)網(wǎng)頁背景是黑的
- (2)星星隨機(jī)大?。簃in=15,max=80
- (3)星星的坐標(biāo)是隨機(jī)的:
- x_left=0,x_right=(瀏覽器寬-星星寬)
- y_top=0,y_bottom=?
- (4)單擊某個星星,星星消失
- (5)網(wǎng)頁加載完成,開始顯示星星
- (6)定時器:每隔一個周期,插入一個星星
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>無標(biāo)題文檔</title> <script type="text/javascript"> //定義全局變量 var img_width_min = 15; var img_width_max = 80; var x_left = 0; var x_right = 0; var y_top = 0; var y_bottom = 0; //定義初始化的函數(shù) function init() { document.body.bgColor = "#000"; x_right = window.innerWidth - img_width_max; y_bottom = window.innerHeight - img_width_max; //定時器 setInterval("showStar()",1000); } //顯示星星 function showStar() { //創(chuàng)建一個圖片 var node_img = document.createElement("img"); //隨機(jī)圖片大小和隨機(jī)坐標(biāo) var width = getRandom(img_width_min,img_width_max); var x = getRandom(x_left,x_right); var y = getRandom(y_top,y_bottom); //增加src的屬性 node_img.setAttribute("src","images/xingxing.gif"); //增加style屬性 var style = "position:absolute;left:"+x+"px;top:"+y+"px;"; style += "width:"+width+"px;"; node_img.setAttribute("style",style); //增加一個onclick事件屬性 node_img.setAttribute("onclick","removeImg(this)"); //將圖片追加到<body>元素下 document.body.appendChild(node_img); } function removeImg(obj) { document.body.removeChild(obj); } function getRandom(min,max) { return Math.floor(Math.random()*(max-min)+min); } </script> </head> <body onload="init()"> </body> </html>
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
解決ie11 SCRIPT5011:不能執(zhí)行已釋放Script的代碼問題
這篇文章主要介紹了ie11 SCRIPT5011:不能執(zhí)行已釋放Script的代碼 ,需要的朋友可以參考下2019-05-05表現(xiàn)、結(jié)構(gòu)、行為分離的選項卡效果
2008-01-01JS實現(xiàn)讀取Excel文件內(nèi)容并生成二維碼
這篇文章主要為大家介紹了如何使用JavaScript實現(xiàn)讀取Excel文件內(nèi)容并生成二維碼下載到本地,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04微信小程序?qū)崿F(xiàn)發(fā)動態(tài)功能的示例代碼
最近做了一個校園拍賣小程序,想在里面添加一個類似校園圈功能,現(xiàn)在來一步一步實現(xiàn),對微信小程序?qū)崿F(xiàn)發(fā)動態(tài)功能感興趣的朋友一起看看吧2022-08-08JS addEventListener()和attachEvent()方法實現(xiàn)注冊事件
這篇文章主要介紹了JS addEventListener()和attachEvent()方法實現(xiàn)注冊事件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01