打地鼠小游戏,比较简陋

在这里插入代码片
        <style type="text/css">
            body{
                height:500px;
                background-image:url(images/hfbj1.jpg);
            }
        </style>
    </head>
    <body>
        <script src="./public.js"></script>
        <script>
            window.onload=function showImg(){
                setInterval(createImg,1000);
                function createImg(){
                    var oImg=document.createElement("img");
                    document.body.appendChild(oImg);
                    oImg.src="dishu.png";
                    var w=rand(50,200)
                    oImg.width=oImg.height=w;
                    oImg.style.position="absolute";
                    oImg.style.top=rand(0,window.innerHeight-w)+"px";
                    oImg.style.left=rand(0,window.innerWidth-w)+"px";
                    oImg.alt="地鼠";
                    setTimeout(hide,5000)
                    function hide(){
                    document.body.removeChild(oImg)
                    }
                    oImg.onclick = function(){
                        //打掉当前地鼠
                        this.remove();
                        
                    }
                }
            }
            
        </script>
        <input type="button" value="换肤" />
        <script>
            var c=document.getElementsByTagName("input")[0];
            var index=1;
            c.onclick=function(){
                var body=document.querySelector("body");
                index++;
                if(index==5){
                    index=1;
                }               
                body.setAttribute("style","background-image:url(images/hfbj"+index+'.jpg)')//方法一
                /* document.body.style.background="url(images/hfbj"+index+'.jpg)'//方法二 */
            }
        </script>
    </body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值