键盘控制div上下左右移动

<html>
  <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="public/easy_ui/themes/icon.css">
    <script type="text/javascript" src="public/easy_ui/jquery.min.js"></script>
    <script type="text/javascript" src="public/easy_ui/easy_ui.min.js"></script>
    <script type="text/javascript" src="public/easy_ui/easyloader.js"></script>
  </head>
  <body>
    <div id="div" style="background-color: red; width: 400px; height: 300px; left: 100px; top: 100px;">
    </div>
  </body>
<script>
  $(function(){
$(document).keydown(function (event) {
    var keyCode = event.keyCode;
    var space = 20;
    var div = $('#div');
    switch(keyCode){
    case 37: div.offset({left:(div.offset().left - 20)}); break;
    case 38: div.offset({top:(div.offset().top - 20)}); break;
    case 39: div.offset({left:(div.offset().left + 20)}); break;
    case 40: div.offset({top:(div.offset().top + 20)}); break;
    default: break;
    }
});
  });
</script>
</html>
posted @ 2016-01-15 22:54  cglworkBook  阅读(940)  评论(0编辑  收藏  举报