知識(shí)點(diǎn):
可以通過(guò)keyCode獲取案件的編碼,可以判斷那個(gè)按鍵被按下
altKey、ctrlKey、shiftKey判斷alt ctrl shift是否被按下
使div可以根據(jù)不同的方向移動(dòng)
按左鍵,div向左移
按右鍵,div右移
按下鍵,div下移
按上鍵,div上移
詳細(xì)代碼:
-
-
-
-
-
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
window.onload = function () {
-
-
document.onkeydown = function (event) {
-
event = event || window.event;
-
console.log(event.keyCode);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
box1.style.left = box1.offsetLeft - speed + "px";
-
-
-
box1.style.left = box1.offsetLeft + speed + "px";
-
-
-
box1.style.top = box1.offsetTop - speed + "px";
-
-
-
box1.style.top = box1.offsetTop + speed + "px";
-
-
-
-
-
-
-
-
-
-
-
-
該文章在 2022/8/22 12:20:15 編輯過(guò)