<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>练习</title>
<script type="text/javascript">
window.onload = function () {
//为document绑定一个键盘按下事件
document.onkeydown = function (event) {
event = event || window.event;
//console.log(event.keyCode);
switch (event.keyCode) {
case 37:
//向左
box1.style.left = box1.offsetLeft - 10 + "px";
break;
case 39:
box1.style.left = box1.offsetLeft + 10 + "px";
break;
case 38:
box1.style.top = box1.offsetTop - 10 + "px";
break;
case 40:
box1.style.top = box1.offsetTop + 10 + "px";
break;
}
};
};
</script>
</head>
<body>
<div id="box1" style="width: 100px;height: 100px;background-color: red; position: relative;"></div>
</body>
</html>
2022/1/15,练习
最新推荐文章于 2024-08-16 19:02:09 发布