這篇文章將為大家詳細講解有關(guān)javascript如何實現(xiàn)點擊星星小游戲,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)建站為客戶提供專業(yè)的網(wǎng)站設(shè)計制作、做網(wǎng)站、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、成都手機網(wǎng)站制作等網(wǎng)站方面業(yè)務(wù)。
具體內(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; //定時器
var count=0; //記錄星星的個數(shù)
var n=0; //記錄游戲時間的變量。
var sj; //時間定時器(解決一直按開始游戲bug)
function StarGame(){ //開始游戲
window.clearInterval(sj); //清除時間定時器
window.clearInterval(dingshiqi);//清楚定時器
dingshiqi=window.setInterval("star()",400);
sj=window.setInterval("shijian()",1000);
}
//創(chuàng)建星星
function star(){
var obj=document.createElement("img");
//給星星添加src屬性
obj.src="image.png"
//隨機星星大小
var w=Math.floor(Math.random()*80+20);
obj.width=w;
obj.height=w;
//隨機位置
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點擊事件(綁定的onclick 不需要加";")
obj.οnclick=removeStar;
//控制大于20個星星游戲結(jié)束
count++;
var sp=document.getElementById("d3");
sp.style.width=count*5+"px";
if(count>20){
alert("大于20個星星游戲結(jié)束");
window.clearInterval(dingshiqi);
location.reload(); //重新加載
}
//放到body中
document.body.appendChild(obj);
}
//點擊刪除星星
function removeStar(){
this.parentNode.removeChild(this);
count--; //點擊星星刪除,都要count-1.
var sp=document.getElementById("d3");
sp.style.width=count*5+"px";
}
//點擊暫停游戲。
function zanting(){
alert("暫停游戲");
}
//記錄游戲時間的函數(shù)
function shijian(){
n++;
var obj=document.getElementById("d1");
obj.innerHTML="游戲進行了"+n+"秒"
}
//-->
</script>
</head>
<body>
<input type="button" value="開始游戲" οnclick="StarGame()">
<input type="button" value="暫停游戲" οnclick="zanting()">
<span id="d1">游戲進行了0秒</span>
<span id="d2"><span id="d3"></span></span>
</body>
</html>
關(guān)于“javascript如何實現(xiàn)點擊星星小游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
分享標題:javascript如何實現(xiàn)點擊星星小游戲
標題網(wǎng)址:http://aaarwkj.com/article36/gopdsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站營銷、全網(wǎng)營銷推廣、網(wǎng)站建設(shè)、Google、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)