|
60 | 60 | function playSound(e) { |
61 | 61 | const audio = document.querySelector(`audio[code="${event.code}"]`); |
62 | 62 | const key = document.querySelector(`.key[code="${event.code}"]`); |
63 | | - if (!audio) return; // stop function from running all together |
64 | | - audio.currentTime = 0; // rewind to the start |
65 | | - audio.play(); |
66 | | - key.classList.add('playing'); |
| 63 | + if (!audio) return; // stop function from running all together if there is no corresponding code |
| 64 | + audio.currentTime = 0; // rewind to the start if clicked while still playing |
| 65 | + audio.play(); // when playSound (event) is invoked, play the audio |
| 66 | + key.classList.add('playing'); // add the playing classList when event is triggered (and thus function runs) |
67 | 67 | } |
68 | 68 |
|
69 | 69 | function removeTransition(e) { |
70 | | - if (e.propertyName !== 'transform') return; // skip if it's not transformed |
71 | | - this.classList.remove('playing'); |
| 70 | + if (e.propertyName !== 'transform') return; // stop function if transform is not active (has finished) |
| 71 | + this.classList.remove('playing'); // when removeTransition is called (and transform is no longer active) remove .playing class |
72 | 72 | } |
73 | 73 |
|
74 | | - const keys = document.querySelectorAll('.key'); |
75 | | - keys.forEach((key) => |
76 | | - key.addEventListener('transitionend', removeTransition) |
| 74 | + const keys = document.querySelectorAll('.key'); // select the NodeList of all divs with the class .key |
| 75 | + keys.forEach( |
| 76 | + ( |
| 77 | + key // for every key in the NodeList |
| 78 | + ) => key.addEventListener('transitionend', removeTransition) // when event transitionend is triggered, run removeTransition function declared above (removes .playing class) |
77 | 79 | ); |
78 | 80 |
|
79 | | - window.addEventListener('keydown', playSound); |
| 81 | + window.addEventListener('keydown', playSound); // when key is pressed, invoke playSound function declared above (adds class and plays sound if key matches html code declaration) |
80 | 82 | </script> |
81 | 83 | </body> |
82 | 84 | </html> |
0 commit comments