javascript實(shí)現(xiàn)點(diǎn)擊星星小游戲
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)點(diǎn)擊星星的具體代碼,供大家參考,具體內(nèi)容如下
<head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #d2{ width:100px; height:20px; border:1px solid red; display:inline-block; } #d3{ display:inline-block; background:yellow; height:20px; } </style> <script type="text/javascript"> <!-- var dingshiqi; //定時(shí)器 var count=0; //記錄星星的個(gè)數(shù) var n=0; //記錄游戲時(shí)間的變量。 var sj; //時(shí)間定時(shí)器(解決一直按開始游戲bug) function StarGame(){ //開始游戲 window.clearInterval(sj); //清除時(shí)間定時(shí)器 window.clearInterval(dingshiqi);//清楚定時(shí)器 dingshiqi=window.setInterval("star()",400); sj=window.setInterval("shijian()",1000); } //創(chuàng)建星星 function star(){ var obj=document.createElement("img"); //給星星添加src屬性 obj.src="image.png" //隨機(jī)星星大小 var w=Math.floor(Math.random()*80+20); obj.width=w; obj.height=w; //隨機(jī)位置 var x=Math.floor(Math.random()*1166+100); var y=Math.floor(Math.random()*500+100); obj.style.position="absolute"; obj.style.top=y+"px"; obj.style.left=x+"px"; //放到body中 document.body.appendChild(obj); //添加onclick點(diǎn)擊事件(綁定的onclick 不需要加";") obj.οnclick=removeStar; //控制大于20個(gè)星星游戲結(jié)束 count++; var sp=document.getElementById("d3"); sp.style.width=count*5+"px"; if(count>20){ alert("大于20個(gè)星星游戲結(jié)束"); window.clearInterval(dingshiqi); location.reload(); //重新加載 } //放到body中 document.body.appendChild(obj); } //點(diǎn)擊刪除星星 function removeStar(){ this.parentNode.removeChild(this); count--; //點(diǎn)擊星星刪除,都要count-1. var sp=document.getElementById("d3"); sp.style.width=count*5+"px"; } //點(diǎn)擊暫停游戲。 function zanting(){ alert("暫停游戲"); } //記錄游戲時(shí)間的函數(shù) function shijian(){ n++; var obj=document.getElementById("d1"); obj.innerHTML="游戲進(jìn)行了"+n+"秒" } //--> </script> </head> <body> <input type="button" value="開始游戲" οnclick="StarGame()"> <input type="button" value="暫停游戲" οnclick="zanting()"> <span id="d1">游戲進(jìn)行了0秒</span> <span id="d2"><span id="d3"></span></span> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
GitHub上77.9K的Axios項(xiàng)目有哪些值得借鑒的地方詳析
提到axios,相信大家應(yīng)該都不會(huì)陌生,這篇文章主要給大家介紹了關(guān)于GitHub上77.9K的Axios項(xiàng)目有哪些值得借鑒的地方,需要的朋友可以參考下2021-06-06原生javascript實(shí)現(xiàn)DIV拖拽并計(jì)算重復(fù)面積
這篇文章主要介紹了使用原生javascript實(shí)現(xiàn)DIV拖拽并計(jì)算重復(fù)面積的方法及示例代碼分享,效果十分漂亮,需要的朋友可以參考下2015-01-01Javascript removeChild()刪除節(jié)點(diǎn)及刪除子節(jié)點(diǎn)的方法
這篇文章主要介紹了Javascript removeChild()刪除節(jié)點(diǎn)及刪除子節(jié)點(diǎn)的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12微信開發(fā)之調(diào)起攝像頭、本地展示圖片、上傳下載圖片實(shí)例
這篇文章主要介紹了微信開發(fā)之調(diào)起攝像頭、本地展示圖片、上傳下載圖片實(shí)例,具有一定的參考價(jià)值有興趣的可以了解一下。2016-12-12關(guān)于vite?+?ts?找不到模塊@/xxxx?或其相應(yīng)的類型聲明問題
這篇文章主要介紹了vite?+?ts?找不到模塊@/xxxx?或其相應(yīng)的類型聲明,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06學(xué)習(xí)drag and drop js實(shí)現(xiàn)代碼經(jīng)典之作
今天讀John Resig的Pro Javascript Techniques時(shí)候看到他書上給的一個(gè)關(guān)于drag and drop的例子, 合上書本自己寫一個(gè)簡化版本的。大約20分鐘完成, 沒有考慮兼容firefox。整個(gè)代碼封裝成一個(gè)對象 也是借鑒書中的風(fēng)格。我覺得很好。2009-04-04js 通過cookie實(shí)現(xiàn)刷新不變化樹形菜單
通過設(shè)置cookie來保存樹形菜單的狀態(tài),在頁面加載時(shí)重新讀取cookie來設(shè)置菜單2014-10-10