小游戏:点不到的小窗口

html:

<span style="font-size:18px;"><!DOCTYPE html>
<html>
 <head>
  <title>小游戏:点不到的小窗口</title>
  <meta charset="utf-8"/>
 </head>
 <body>
  <script src="js/2.js"></script>
 </body>
</html>
</span>

js:

<span style="font-size:18px;">var game={
  OWIDTH:100+16,//弹出窗口的外部宽,横向修正误差16px
  OHEIGHT:100+66,//弹出窗口的外部高,纵向修正66px
  MAXLEFT:0,//保存弹出窗口最大可用left值: 屏幕宽-窗口宽
  MAXTOP:0,//保存弹出窗口最大可用top值: 屏幕高-窗口高
  init:function(){//计算MAXLEFT和MAXTOP
    this.MAXLEFT=screen.availWidth-this.OWIDTH;
    this.MAXTOP=screen.availHeight-this.OHEIGHT;
  },
  start:function(){
    this.init();
    //在0~MAXLEFT之间获得随机整数,保存在变量left中
    var left=parseInt(Math.random()*this.MAXLEFT);
    //在0~MAXTOP之间获得随机整数,保存在变量top中
    var top=parseInt(Math.random()*this.MAXTOP);
    //定义配置字符串:
      //config:top=top,left=left,width=100,heigth=100
    var config="top="+top
              +",left="+left
              +",width=100,height=100";
    console.log(config);
    //打开新窗口:url:空页面 , name:"pop" , config
    var pop=open("about:blank","pop",config);
    //pop->新窗口的window,用法和window完全一致: 
    pop.document.write(
  '<img src="d:/xiaoxin.gif" style="width:80"onclick="alert(\'约么?\')"/>'
    );
    var me=this;//留住this
    pop.onmouseover=function(e){
      e=window.event||e;
      //this->pop, me->game
      for(;;){
        //在0~MAXLEFT之间获得随机整数,保存在变量left中
        var left=parseInt(Math.random()*me.MAXLEFT);
        //在0~MAXTOP之间获得随机整数,保存在变量top中
        var top=parseInt(Math.random()*me.MAXTOP);
        //只有新位置不包含鼠标的位置时
        if(!(left<e.screenX&&e.screenX<left+me.OWIDTH
            &&top<e.screenY&&e.screenY<left+me.OHEIGHT)){
          //将当前窗口移动到top和left位置上
          this.moveTo(left,top);
          break;//退出循环
        }
      }
    }
  }
}
window.onload=function(){game.start();}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值