|
59 | 59 |
|
60 | 60 | <script> |
61 | 61 |
|
| 62 | +function playSound(e) { |
| 63 | + console.log(e.keyCode); |
62 | 64 |
|
63 | | - function playSound(e) { |
64 | | - const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); |
65 | | - 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 |
68 | | - audio.play(); |
69 | | - key.classList.add('playing'); |
70 | | - } |
71 | | - function removeTransition(e) { |
72 | | - if (e.propertyName !== 'transform') return; // skip it if it's not a transform |
73 | | - this.classList.remove('playing'); |
| 65 | + // Simple string substitution |
| 66 | + var name = "Brendan"; |
| 67 | + console.log(`Yo, ${name}!`); |
| 68 | + |
| 69 | + // => "Yo, Brendan!" |
| 70 | + |
| 71 | + var a = 65; |
| 72 | + var b = 83; |
| 73 | + console.log(`Hello ${a} world ${b}`); |
| 74 | + |
| 75 | + var user = {name: 'Mickey Mouse'}; |
| 76 | + console.log(user.name.toUpperCase()); |
| 77 | + console.log(`Thanks, ${user.name.toUpperCase()}.`); |
| 78 | + // Look for a match for this keyCode. |
| 79 | + // Use an attribute selector: [] |
| 80 | + // Uses ES6 template strings: ${} |
| 81 | + const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); |
| 82 | + console.log(audio); |
| 83 | + |
| 84 | + // Get the key element out of the DOM. |
| 85 | + const key = document.querySelector(`.key[data-key="${e.keyCode}"]`); |
| 86 | + console.log(key); |
| 87 | + |
| 88 | + if (!audio) { |
| 89 | + console.log("no audio found for keyCode"); |
| 90 | + return; // stop function from running. |
74 | 91 | } |
| 92 | + |
| 93 | + // reset audio to start |
| 94 | + audio.currentTime = 0; |
| 95 | + audio.play(); |
| 96 | + |
| 97 | + // Add the 'playing' class to the key element. |
| 98 | + key.classList.add('playing'); |
| 99 | +} |
| 100 | + |
| 101 | +function removeTransition(e) { |
| 102 | + if (e.propertyName != 'transform') return; // skip if it is not a transform. |
| 103 | + console.log(e.propertyName); |
| 104 | + console.log(this); |
| 105 | + // Remove the 'playing' class from the key (which is what this is equal to) |
| 106 | + this.classList.remove('playing'); |
| 107 | +} |
| 108 | + |
| 109 | +// Use a transition end event to detect when the animation stops. |
| 110 | +const keys = document.querySelectorAll('.key'); |
| 111 | +// Arrow function... => |
| 112 | +keys.forEach(key => key.addEventListener('transitionend', removeTransition)); |
| 113 | + |
| 114 | +window.addEventListener('keydown', playSound); |
75 | 115 |
|
76 | | - const keys = document.querySelectorAll('.key'); |
77 | | - keys.forEach(key => key.addEventListener('transitionend', removeTransition)); |
78 | | - window.addEventListener('keydown', playSound); |
79 | 116 |
|
80 | 117 | </script> |
81 | 118 |
|
| 119 | + |
82 | 120 | </body> |
83 | 121 | </html> |
0 commit comments