<div class='box'>
<p>단축키</p>
<p>Shift + W,S,A,D</p>
</div>
.box {
width: 130px;
height: 130px;
background: #febf00;
position: absolute;
top: 36%;
left: 36%;
border-radius: 5px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-weight: bold;
}
.box > p:first-child {
font-size: .93rem;
font-weight: normal;
}
/* ---------- */
@import url("https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
outline: none;
word-break: break-all;
font-family: Pretendard;
}
body {
width: 100%;
height: 100vh;
background: #f7f8fc;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
// 단축키 지정
document.addEventListener('keydown', (event) => {
event.preventDefault();
if (event.key === 'W' && event.shiftKey) {
moveToTop();
return false;
}
if (event.key === 'S' && event.shiftKey) {
moveToBottom();
return false;
}
if (event.key === 'A' && event.shiftKey) {
moveToLeft();
return false;
}
if (event.key === 'D' && event.shiftKey) {
moveToRight();
return false;
}
});
const $box = document.querySelector('.box');
const moveToTop = () => {
$box.style.top = Math.max($box.offsetTop - 10, 0) + 'px';
}
const moveToBottom = () => {
$box.style.top = Math.min($box.offsetTop + 10, document.body.clientHeight - 130) + 'px';
}
const moveToLeft = () => {
$box.style.left = Math.max($box.offsetLeft - 10, 0) + 'px';
}
const moveToRight = () => {
$box.style.left = Math.min($box.offsetLeft + 10, document.body.clientWidth - 130) + 'px';
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.