キーボードイベントのエクササイズ-divの動きを制御する

divの動きを制御する


トピック:divの動きを制御するためのキーパッドの矢印キー

しばらく考えて




<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#box {
    
    
				width: 100px;
				height: 100px;
				background-color: #8A2BE2;
				position: absolute;
			}
		</style>
	</head>
	<body>
		<div id="box">

		</div>
		<script type="text/javascript">
			
			document.onkeydown = function(event) {
    
    
				event = event || window.event;
				// 37  39
				//var box = document.getElementById("box");
				var speed = 10;
				if(event.ctrlKey){
    
    
					speed = 30;
				}
				switch (event.keyCode) {
    
    
					case 37:
						
						box.style.left = box.offsetLeft - speed + "px";
						break;
					case 39:
						box.style.left = box.offsetLeft + speed + "px";
						break;
					case 38:
						box.style.top = box.offsetTop-speed+"px";
						break;
					case 40:
						box.style.top = box.offsetTop+speed+"px";
						break;
				}
			};
		</script>
	</body>
</html>

おすすめ

転載: blog.csdn.net/weixin_44154094/article/details/113055319