首页 > 其他 > 详细

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

时间:2020-01-05 11:19:41      阅读:75      评论:0      收藏:0      [点我收藏+]

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;
  }
}

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

原文:https://www.cnblogs.com/hjysunshine/p/12151730.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!