Created
April 21, 2020 09:55
-
-
Save ArtemVeremienko/d4c74822f11ab7e736a2591a2c65085c to your computer and use it in GitHub Desktop.
Управляем клавиатурой
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Кликните по "изображению" мыши и перемещайте её с помощью клавиш со стрелками.</p> | |
<pre id="mouse"> | |
_ _ | |
(q\_/p) | |
/. .\ | |
=\_t_/= __ | |
/ \ ( | |
(( )) ) | |
/\) (/\ / | |
\ Y /-' | |
nn^nn | |
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mouse.tabIndex = 0; | |
mouse.onclick = function () { | |
this.style.left = this.getBoundingClientRect().left + "px"; | |
this.style.top = this.getBoundingClientRect().top + "px"; | |
this.style.position = "fixed"; | |
}; | |
mouse.onkeydown = function (event) { | |
switch (event.code) { | |
case "KeyW": | |
case "ArrowUp": | |
this.style.top = parseInt(this.style.top) - this.offsetHeight + "px"; | |
return false; | |
case "KeyS": | |
case "ArrowDown": | |
this.style.top = parseInt(this.style.top) + this.offsetHeight + "px"; | |
return false; | |
case "KeyA": | |
case "ArrowLeft": | |
this.style.left = parseInt(this.style.left) - this.offsetWidth + "px"; | |
return false; | |
case "KeyD": | |
case "ArrowRight": | |
this.style.left = parseInt(this.style.left) + this.offsetWidth + "px"; | |
return false; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#mouse { | |
display: inline-block; | |
cursor: pointer; | |
margin: 0; | |
} | |
#mouse:focus { | |
outline: 1px dashed black; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment