消除小星星案例

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script type="text/javascript">
       // window.οnlοad=init;
       var dingshiqi;
       var sj;//时间定时器名
       var count=0;
       var n=0;//记录游戏时间的变量
       //点击开始游戏的函数
        function startGame()
        {
          window.clearInterval(dingshiqi);
          window.clearInterval(sj);
        sj=window.setInterval("shijian()",1000);
        dingsiqi=window.setInterval("star()",400);
        }
        function star()
        {
        //创建星星
        var obj=document.createElement("img");
        //给星星添加src属性
        obj.src="xx.jpeg";
        //随机星星大小
          var w=Math.floor(Math.random()*80+20);
          obj.width=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";
          //添加点击事件
          obj.οnclick=removestar;
               count++;
               var span=document.getElementById("d3");
               span.style.width=count*5+"px";
               if(count>20)
               {
                alert("停止游戏");
                window.clearInterval(dingshiqi);
                location.reload();
               }
        //放到body中
        document.body.appendChild(obj);
        }
        //点击删除星星的函数
        function removestar()
        {
           this.parentNode.removeChild(this);
           count--;
           var sp=document.getElementById("d3");
           sp.style.width=count*5+"px";


        }
        //点击暂停游戏的函数
        function zanting()
        {
            alert("暂停游戏");
        }
        //记录游戏时间的函数
        function shijian()
        {
        n++;
        var obj=document.getElementById("d1");
        obj.innerHTML="游戏进行了"+n+"秒";
        }
</script>
<style type="text/css">
   #d2
   {
    border:1px solid red;
    width:100px;
    height:20px;
    display:inline-block;
   }
   #d3
   {
    display: inline-block;
    background:yellow;
    height:20px;
   }
</style>
</head>
<body>
<input type="button" value="开始游戏" οnclick="startGame()" />
<input type="button" value="暂停游戏" οnclick="zanting()" />
<span id="d1">游戏进行的0秒</span>
<span id="d2"><span id="d3"></span></span>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值