|
59 | 59 |
|
60 | 60 | <script> |
61 | 61 |
|
62 | | - |
63 | | - function playSound(e) { |
| 62 | + function playsound(e) { |
| 63 | + // listen for keys, if a keyCode is bound to an audio attribute sore it in the audio variable |
64 | 64 | const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); |
| 65 | + // get the key element |
65 | 66 | const key = document.querySelector(`.key[data-key="${e.keyCode}"]`); |
66 | | - if (!audio) return; // stop the function from running all together |
67 | | - audio.currentTime = 0; // rewind to the start |
| 67 | + // stop the function if a a key was pressed that has no audio attribute |
| 68 | + if(!audio) return; |
| 69 | + // rewind audio clip to the start. |
| 70 | + audio.currentTime = 0; |
68 | 71 | audio.play(); |
| 72 | + // add the playing css to the element that was triggered |
69 | 73 | key.classList.add('playing'); |
70 | 74 | } |
| 75 | + |
71 | 76 | function removeTransition(e) { |
72 | | - if (e.propertyName !== 'transform') return; // skip it if it's not a transform |
73 | | - this.classList.remove('playing'); |
| 77 | + // skip it if it is not a transform |
| 78 | + if (e.propertyName !== 'transform') return; |
| 79 | + // since a key element called this function, we can refer to it as this |
| 80 | + this.classList.remove('playing') |
74 | 81 | } |
75 | 82 |
|
| 83 | + // add an event listening to all key elements for when have tranistioned after playing a drum loop |
76 | 84 | const keys = document.querySelectorAll('.key'); |
77 | 85 | keys.forEach(key => key.addEventListener('transitionend', removeTransition)); |
78 | | - window.addEventListener('keydown', playSound); |
79 | 86 |
|
| 87 | + // list for the keydown event |
| 88 | + window.addEventListener('keydown', playsound); |
80 | 89 | </script> |
81 | 90 |
|
| 91 | + |
82 | 92 | </body> |
83 | 93 | </html> |
0 commit comments