js/jq 键盘上下左右回车按键
js判断上下左右回车按键:
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上下左右按键移动:
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; });