jsDOM编程-小球在盒子里来回撞击

首先写一个小页面;页面需要一个div 这个div就是盒子,然后在div里在包含一个子div 这个子div就包含一张小球的图片

代码:

    <!doctype html>

    <html>

     <head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><title>小球碰撞游戏</title><head>

    <body>

<div id="gamescroll" style="width:600px;height:400px;position:absolute;top:0px;left:0px;border:1px solid black">

   <div id="qiu"><img src="qiu.gif"/></div>

</div>

<script type="text/javascript" language="javascript">

        function $(id){ return document.getElementById(id);};

         function TinyGame(x,y,direx,direy){

                      this.x =x ;//横坐标

                      this.y = y;//纵坐标

                       this.direx = direx; //x方向移动的值

                     this.direy = direy;//y方向移动的值

                    this.moveqiu  = function(){

                          this.x += this.direx;

                          this.y += this.direy;

                       $("qiu").style.left = this.x +'px';

                      $("qiu").style.top = this.y+'px';

                     if(this.x+$('qiu').offsetWidth >= $("gamescroll").offsetWidth || this.x <= 0){

                                    /*如果x方向移动的距离+小球的实际宽度(offsetWidth-在浏览器里的实际宽度,offsetHeight--在浏览器里的实际高度)>=盒子的实际宽度 或者 x方向移动的距离小于等于0

                                        就表示到达了边界 

                                  */

                                     this.direx = -this.direx;//正变负 负变正  表示处于两个不同的边界的情况处理

 

   if(this.y+$('qiu').offsetHeight >= $("gamescroll").offsetHeight || this.y <= 0){

                                 

                                     this.direy= -this.direy;//正变负 负变正  表示处于两个不同的边界的情况处理

 

                }

}

   var TG1 = new TinyGame(0,0,1,1);

setInterval("TG1->moveqiu()",500);

</script>

</body>

</html>

转载于:https://www.cnblogs.com/YangJieCheng/p/5679513.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值