File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 5959 < audio data-key ="76 " src ="sounds/tink.wav "> </ audio >
6060
6161< script >
62+
63+
64+ function playSound ( e ) {
65+ const audio = document . querySelector ( `audio[data-key="${ e . keyCode } "]` ) ;
66+ const key = document . querySelector ( `.key[data-key="${ e . keyCode } "]` ) ;
67+ if ( ! audio ) return ;
68+ audio . currentTime = 0 ; // rewind to start.
69+ audio . play ( ) ;
70+ if ( ! key ) return ;
71+ key . classList . add ( 'playing' ) ;
72+ }
73+
74+ function removeTransition ( e ) {
75+ // Skip is not a transform.
76+ if ( e . propertyName !== 'transform' ) return ;
77+ this . classList . remove ( 'playing' ) ;
78+ }
79+
80+ const keys = document . querySelectorAll ( '.key' ) ;
81+ // This is based on the CSS transitionend property. This is to make sure that the highlight will be removed after transition to yellow border.
82+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
83+
84+ window . addEventListener ( 'keydown' , playSound ) ;
85+
86+ // window.addEventListener('keyup', function(e) {
87+ // const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
88+ // if (!key) return;
89+ // key.classList.remove('playing');
90+ // })
91+
6292
6393</ script >
6494
You can’t perform that action at this time.
0 commit comments