A, S, D, F, G, H, J, K ,L ์ ํค์ ๋ ฅ์ ๋ฐ์ ๋ง๋ ์ ๋๋ฉ์ด์ ํจ๊ณผ์ ๋๋ผ ์๋ฆฌ๋ฅผ ์ถ๋ ฅํ๋ Drum Kit
์ด๊ธฐ html : index-START.html ์ฐธ๊ณ .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Drum Kit</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="keys">
<div data-key="65" class="key">
<kbd>A</kbd>
<span class="sound">clap</span>
</div>
<div data-key="83" class="key">
<kbd>S</kbd>
<span class="sound">hihat</span>
</div>
<div data-key="68" class="key">
<kbd>D</kbd>
<span class="sound">kick</span>
</div>
<div data-key="70" class="key">
<kbd>F</kbd>
<span class="sound">openhat</span>
</div>
<div data-key="71" class="key">
<kbd>G</kbd>
<span class="sound">boom</span>
</div>
<div data-key="72" class="key">
<kbd>H</kbd>
<span class="sound">ride</span>
</div>
<div data-key="74" class="key">
<kbd>J</kbd>
<span class="sound">snare</span>
</div>
<div data-key="75" class="key">
<kbd>K</kbd>
<span class="sound">tom</span>
</div>
<div data-key="76" class="key">
<kbd>L</kbd>
<span class="sound">tink</span>
</div>
</div>
<audio data-key="65" src="sounds/clap.wav"></audio>
<audio data-key="83" src="sounds/hihat.wav"></audio>
<audio data-key="68" src="sounds/kick.wav"></audio>
<audio data-key="70" src="sounds/openhat.wav"></audio>
<audio data-key="71" src="sounds/boom.wav"></audio>
<audio data-key="72" src="sounds/ride.wav"></audio>
<audio data-key="74" src="sounds/snare.wav"></audio>
<audio data-key="75" src="sounds/tom.wav"></audio>
<audio data-key="76" src="sounds/tink.wav"></audio>
<script>
// add Answer
</script>
</body>
</html>
์ด๊ธฐํ๋ฉด
kbd ํ๊ทธ๋ํค๋ณด๋ ์ ๋ ฅ, ์์ฑ ์ ๋ ฅ ๋ฑ ์์์ ์ฅ์น๋ฅผ ์ฌ์ฉํ ์ฌ์ฉ์์ ์ ๋ ฅ์ ๋ํ๋ ๋๋ค.
์ฐธ๊ณ : https://developer.mozilla.org/ko/docs/Web/HTML/Element/kbd
data-๋ก ์์ํ๋ ์์ฑ์ ๋ฌด์์ด๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
data-colums , data-index-number, data-parent ๋ฑ๋ฑ...
์ด๋ฒ์์ ์์ ์ฌ์ฉํ data-key์์ฑ์ ์์ div์ ์๋ audio๋ฅผ ํ๋๋ก ๋ฌถ๋ ์์ฑ์ด๊ณ ,
key๊ฐ์ ์ฐ๋ฆฌ๊ฐ ๋๋ฅผ ํค๋ณด๋ํค ASCII-Code๊ฐ์ผ๋ก ์ฐ๊ฒฐํด๋์๋ค.
์ฐธ๊ณ : https://developer.mozilla.org/ko/docs/Learn/HTML/Howto/Use_data_attributes
// add Answer
window.addEventListener('keydown', function(e){
// console.log(e);
// console.log(e.keyCode);
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
// console.log(audio);
if(!audio) return; // stop the function from running all togethers
audio.currentTime = 0; // rewind to the start, ํค์ ์ฌ์
๋ ฅ์๊ฐ ์ค์ .
audio.play();
});
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
์ผ์ผ์ด keyCode๋ฅผ ์
๋ ฅํ์ง ์๊ณ , ES6๋ฌธ๋ฒ ํ์ฉํด ๋ฐฑํฑ(`)๊ณผ ${e.keycode}๋ก ๊ฐ์ ๋ฐ์.
keyCode๋ console.log(e.keyCode)๋ก ํ์ธ.
audio.currentTime = 0; // rewind to the start, ํค์ ์ฌ์
๋ ฅ์๊ฐ ์ค์ .
function removeTransition(e){
// console.log(e);
if(e.propertyName !== 'transform') return; // skip if is it's not a transform
// console.log(e.prototypeName);
// console.log(this);
this.classList.remove('playing');
}
removeTransition์์ console.log(e) ๊ฒฐ๊ณผ
๋๋ฅธ ํค์ ์์ฑ๋ช
์ค transform์ด ์์ผ๋ฉด ๋ค์ ์ฝ๋๋ฅผ ์คํํ์ง์๊ณ , ์กด์ฌํ๋ฉด playingํด๋์ค๋ฅผ ์ง์ด๋ค.
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);
const keys = document.querySelectorAll('.key');
keyํด๋์ค๋ฅผ ๊ฐ์ง ๋ชจ๋ ์์๋ฅผ keys์ ๋ฐฐ์ด๋ก ๋ด๊ณ ,
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
forEach()๋ฅผ ํตํด ๊ฐ ์์์ ๋ณํ๊ฐ ๋๋๋ฉด, removeTransitionํจ์๋ฅผ ์คํํจ.
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
ES6๋ฌธ๋ฒ์ ํ์ดํํจ์๋ฅผ ์ด์ฉ.
์์์ ๋ง๋ ํจ๊ณผ์์ ์ถ๊ฐํ๋ function์ playSound()๋ก ๋ฐ๋ก ๋ง๋ค๊ณ ,
keydown event๋ฅผ script๊ฐ์ฅ ์๋ ์ถ๊ฐ.