其他分享
首页 > 其他分享> > 【DOM操作】——操纵方向键

【DOM操作】——操纵方向键

作者:互联网

var div = document.getElementById('div1');
div.style.width = '100px';
div.style.height = '100px';
div.style.backgroundColor = 'red';
div.style.position = 'absolute';
div.style.top = '0';
div.style.left = '0';
//控制方向键
document.onkeydown = function(e) {
  switch (e.which){
    case 38:
      div.style.top = parseInt(div.style.top) - 5 + "px";
      break;
    case 40:
      div.style.top = parseInt(div.style.top) + 5 + "px";
      break;
    case 37:
      div.style.left = parseInt(div.style.left) - 5 + "px";
      break;
    case 39:
        div.style.left = parseInt(div.style.left) + 5 + "px";
      break;
    default:
      break;
  }
}

标签:break,style,方向键,DOM,top,操纵,parseInt,div,left
来源: https://www.cnblogs.com/hjysunshine/p/12151730.html