javascript下用键盘控制层的移动的代码

2019-06-02 21:55:48于丽

    div.style.left = "0px";
    break ;
  }
  break;
  }
  case up:{
  press = "<font> ↑ </font>";
  evet = "<font>向 <b>上</b> 移动</font>" + " " + int + " px 。";
  div.style.top = (parseInt(div.currentStyle.top) - int) + "px";
  if(parseInt(div.style.top) <= 0)
  {
    evet = "已经到了 <font>最上边</font> ,无法再 <font>向上</font> 移动。";
    div.style.top = "0px";
    break ;
  }
  break;
  }
  case right:{
  press = "<font> → </font>";
  evet = "<font>向 <b>右</b> 移动</font>" + " " + int + " px 。";
  div.style.left = (parseInt(div.currentStyle.left) + int) + "px";
  if(parseInt(div.style.left) >= (parseInt(document.body.clientWidth) - parseInt(div.offsetWidth)))
  {
    div.style.left = parseInt(document.body.clientWidth) - parseInt(div.offsetWidth);
    evet = "已经到了 <font>最右边</font> ,无法再 <font>向右</font> 移动。";
    break ;
  }
  break;
  }
  case down:{
  press = "<font> ↓ </font>";
  evet = "<font>向 <b>下</b> 移动</font>" + " " + int + " px 。";
  div.style.top = (parseInt(div.currentStyle.top) + int) + "px";
  if(parseInt(div.style.top) >= (parseInt(document.body.clientHeight) - parseInt(div.offsetHeight)))
  {
    div.style.top = parseInt(document.body.clientHeight) - parseInt(div.offsetHeight);
    evet = "已经到了 <font>最下边</font> ,无法再 <font>向下</font> 移动。";
    break ;
  }
  break;
  }
  default:
  {
  return;
  }
  }
  str = "<p>您按下了" + press + "键,</p>";