小球撞击游戏

来源:互联网 发布:manbetx软件哪里下 编辑:程序博客网 时间:2024/04/30 21:34

说明:小球会在屏幕上自动移动,当碰到边缘时会反弹回去继续移动。

<html><head><title>小太阳</title><script language="javascript" type="text/javascript">    //全局变量定两个方向    directX=1;//x轴方向    directY=1;//y轴方向    sunX=0;//太阳坐标    sunY=0;function sunMove(){    sunX+=directX;    sunY+=directY;    //修改div的left top    sundiv.style.top=sunY+"px";    sundiv.style.left=sunX+"px";    //判断什么时候改变方向    //x方向(offsetWidth可以返回当前这个对象事件宽度)    if(sunX+sundiv.offsetWidth>=document.body.clientWidth||sunX<=0){        directX=-directX;    }    if(sunY+sundiv.offsetHeight>=document.body.clientHeight||sunY<=0){        directY=-directY;    }}setInterval("sunMove()",10);</script></head><body style="background-image: url('1.jpg')"> <div id="sundiv" style="position:absolute"><img src="sun.png"/></div></body></html>
原创粉丝点击