|
| 1 | +# Exercise 1: JavaScript Drum Kit |
| 2 | +Nitish Dayal, Software & Applications Developer - [Contact](http://nitishdayal.me) |
| 3 | +Last Commit Date: Dec 9, 2016 |
| 4 | + |
| 5 | +An HTML page displays a collection of `div` elements, each containing a letter |
| 6 | + that corresponds with a key on the keyboard, and the name of the soundclip to be |
| 7 | + played when that button is clicked. When a user presses a key that matches |
| 8 | + one of the `div` elements, the page should play the corresponding soundclip |
| 9 | + and place a temporary 'highlight' (or border) around the `div`. Write the |
| 10 | + JavaScript code necessary to add this functionality. |
| 11 | + |
| 12 | +## Guide |
| 13 | + |
| 14 | +We are provided with the HTML, CSS, and sound clips necessary to create this |
| 15 | + page/application. Let's go over the provided files and look at the pieces |
| 16 | + that we can utilize to fulfill the requirements: |
| 17 | + |
| 18 | +- HTML `data-*` attributes: Introduced in HTML5, `data-*` attributes (where * can |
| 19 | + be anything you want) allow us to store _custom data_ on any HTML element. Each |
| 20 | + `div.key` and `audio` element in the provided HTML file has a `data-key` attribute |
| 21 | + which corresponds with a keyboard button. |
| 22 | + |
| 23 | +- CSS `active` class & pre-defined style: The provided CSS file already has an `playing` |
| 24 | + class defined with some styling in it. We will apply this class to the correct |
| 25 | + element, depending on the key pressed by the user, and remove it once animation |
| 26 | + is finished. |
| 27 | + |
| 28 | +And that's...pretty much all we should need from the HTML & CSS files. We can use |
| 29 | + the `data-key` attributes to match up the correct audio clip with the div element, |
| 30 | + and we can use the `playing` class to add that temporary highlight/border. |
| 31 | + |
| 32 | +**Steps:** |
| 33 | + |
| 34 | +1. Add an _event listener_ to the entire window object that is listening for a |
| 35 | + `keydown` event; the function that we will provide as the _callback_ will be |
| 36 | + defined next. For now, decide on what the function name will be and provide |
| 37 | + that name as the second argument for our event listener. |
| 38 | + |
| 39 | +2. Create a function with the name that you decided on in step 1. |
| 40 | + - The function should accept one parameter, the _event_ which the function is going to handle. |
| 41 | + - The body of the function will declare and define two variables that will |
| 42 | + reference the _div and audio elements_ that correspond with the keypress which |
| 43 | + triggered the function (if such elements exist). |
| 44 | + - If the key pressed doesn't match one of the keys defined on our page we don't |
| 45 | + need to proceed any further, but if it does then we can use the variable |
| 46 | + referencing the `div` element to add the `playing` class to it, and we can use |
| 47 | + the variable referencing the `audio` element to play the audio file. |
| 48 | + |
| 49 | +3. Declare & define a variable which will reference all the HTML elements on our |
| 50 | + page with a class `key`. |
| 51 | + |
| 52 | +4. Iterate through the HTML elements and add an _event listener_ to each one that will |
| 53 | + fire on the `transitionend` event. Provide another function (name of your choice) |
| 54 | + as the second argument which will be responsible for **removing** the `playing` |
| 55 | + class. |
| 56 | + |
| 57 | +5. Create a function with the name that was used in step 4. |
| 58 | + - This function (event handler) will fire when the `playing` class is added |
| 59 | + to an element and the transition animation completes. The event provided |
| 60 | + to the function as an argument will contain a reference to the `TransitionEvent` |
| 61 | + (or events) with the CSS property's name which was transitioned into. |
| 62 | + - The function should only be concerned with the `transform` property, as this |
| 63 | + property defines the time the animation will take, and we don't want to remove |
| 64 | + the class until the transformation is complete. |
| 65 | + - If there is an event with a property name `transform`, target the _function |
| 66 | + context_ (the HTML element to which the event listener was attached) and remove |
| 67 | + the `playing` class. |
| 68 | + |
| 69 | +All done! Yaaaaaay! |
0 commit comments