js/jq 鍵盤上下左右回車按鍵

js判斷上下左右回車按鍵:css

document.onkeydown=function(e){
  e=window.event||e;
  switch(e.keyCode){
    case 37: //左鍵
    console.log("left");
    break;
    case 38: //向上鍵
    console.log("top");
    break;
    case 39: //右鍵
    console.log("right");
    break;
    case 40: //向下鍵
    console.log("down");
    break;
    case 13: //回車鍵
    console.log("回車");
    default:
    break;
  }
}

 

jq上下左右按鍵移動:spa

var x = 0;//水平方向位移
var y = 0;//垂直方向位移
$(document).keydown(function (event) {
  case 37: y = y - 10; $("selector").css("left", y + "px"); break;
  case 38: x = x - 10; $("selector").css("top", x + "px"); break;
  case 39: y = y + 10; $("selector").css("left", y + "px"); break;
  case 40: x = x + 10; $("selector").css("top", x + "px"); break;
  default: break;
});
相關文章
相關標籤/搜索