Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>


  <div class="keys">
    <div data-key="65" class="key">
      <kbd>A</kbd>
      <span class="sound">clap</span>
    </div>
    <div data-key="83" class="key">
      <kbd>S</kbd>
      <span class="sound">hihat</span>
    </div>
    <div data-key="68" class="key">
      <kbd>D</kbd>
      <span class="sound">kick</span>
    </div>
    <div data-key="70" class="key">
      <kbd>F</kbd>
      <span class="sound">openhat</span>
    </div>
    <div data-key="71" class="key">
      <kbd>G</kbd>
      <span class="sound">boom</span>
    </div>
    <div data-key="72" class="key">
      <kbd>H</kbd>
      <span class="sound">ride</span>
    </div>
    <div data-key="74" class="key">
      <kbd>J</kbd>
      <span class="sound">snare</span>
    </div>
    <div data-key="75" class="key">
      <kbd>K</kbd>
      <span class="sound">tom</span>
    </div>
    <div data-key="76" class="key">
      <kbd>L</kbd>
      <span class="sound">tink</span>
    </div>
  </div>

  <audio data-key="65" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/clap.wav"></audio>
  <audio data-key="83" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/hihat.wav"></audio>
  <audio data-key="68" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/kick.wav"></audio>
  <audio data-key="70" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/openhat.wav"></audio>
  <audio data-key="71" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/boom.wav"></audio>
  <audio data-key="72" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/ride.wav"></audio>
  <audio data-key="74" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/snare.wav"></audio>
  <audio data-key="75" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/tom.wav"></audio>
  <audio data-key="76" src="https://raw.githubusercontent.com/JMaunders/drum-kit/master/sounds/tink.wav"></audio>



</body>

              
            
!

CSS

              
                html, body { 
	padding: 0;
	margin: 0;
  background-color: #eeeff1;  
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  font-family: Roboto;
  color: #ffffff;
  font-weight: 700;
}

.keys {
	display: flex;
	flex: 1;
	min-height: 100vh;
	align-items: center;
	justify-content: center;
}

.key {
	border: 2px solid #84b71c;
	width: 100px;
	height: 75px;
	padding: 15px 5px;
	margin: auto 10px;
	text-align: center;
	background: rgba(0,0,0,0.4);
	text-shadow: 1px 2px #000000;
	transition: all 0.17s ease;
}

.playing {
  transform: scale(1.5);
  border: 3px #a3d3d5 dotted;
  box-shadow: 0 0 2rem #87509c;
}

.sound {
	text-transform: uppercase;
	text-shadow: 1px 2px #000000;
	font-size: 125%;
  color: #ff3b41;
}

kbd {
	display: block;
	font-size: 4rem;
	text-align: center;
  color: #84b71c;

}
              
            
!

JS

              
                /*e to zmienna reprezentująca event (zdarzenie) - w tym przypadku wciśnięcia klawisza na klawiaturze). 
Właściwość keyCode zawiera wartość odpowiadającą wciśniętemu klawiszowi.
click event jest wtedy, gdy ktoś kliknie na coś i coś odpali event. wtedy click event wie - o ktoś mnie kliknął ;)*/

/*transitionend jest wtedy gdy chcemy, aby transition, które się wydarzyło, zakończyło się bez dodatkowego działania*/

function playSound(e) {
	const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); //Do stałej audio przypisz element audio posiadający atrybut "data-key" o wartości e.keycode
	const key = document.querySelector(`.key[data-key="${e.keyCode}"]`); //Do stałej keys przypisz klasę key posiadający atrybut "data-key" o wartości e.keycode.
   	if (!audio) return; //jeśli nie ma audio zawróc
   	
   	audio.currentTime = 0; //funkcja sprawiająca, że nie czekamy na zakończenie dźwięku, tylko ładuje się szybko od nowa
    audio.play();	 //odtwarzaj audio
    key.classList.add('playing'); //funkcja dodająca zmiany z CSS z klasy playing, m.in obramowanie
}

//funkcja usuwająca transition z css
function removeTransition(e) {
  if(e.propertyName !== 'transform') return;
  this.classList.remove('playing');
}

const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));

window.addEventListener('keydown', playSound);
              
            
!
999px

Console