亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript實(shí)現(xiàn)點(diǎn)擊星星小游戲

 更新時(shí)間:2019年12月24日 09:03:16   作者:我不抽煙zzm  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)點(diǎn)擊星星小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評(píng)論