JS30 walkthrough-Day1-Drum Kit

🐒:做爲一個野生coder,爲了夯實基礎,決定跟着油管up主[ Alex 宅幹嘛 ]的教程開擼業界人氣頗高的wes bos JS30系列,記錄思考過程和知識要點。css

Day1-Drum Kit
demodrum.pnghtml

A. 基本實現思路

實現效果:摁下鍵盤時摁鍵樣式變化+播放相應音效
實現思路:監聽鍵盤keydown事件,利用返回的keyCode去處理相應的樣式和聲效git

B.JS要點

  • IIFE-當即調用函數表達式,是一個在定義時就會當即執行的 JavaScript 函數。
(function () {
    statements
})();

這是一個被稱爲[自執行匿名函數]的設計模式,主要包含兩部分。第一部分是包圍在 [圓括號運算符] 裏的一個匿名函數,這個匿名函數擁有獨立的詞法做用域。這不只避免了外界訪問此 IIFE 中的變量,並且又不會污染全局做用域。es6

第二部分再一次使用 () 建立了一個當即執行函數表達式,JavaScript 引擎到此將直接執行函數。github

  • Keydown 事件-事件觸發於鍵盤按鍵按下的時候。與keypress 事件不一樣的是, 全部按鍵均會觸發keydown事件,不管這些按鍵是否會產生字符值。
  • 利用屬性和es6的template string選取元素
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
const dom = document.querySelector(`div[data-key="${e.keyCode}"]`)
  • 聲效連續播放的關鍵-HTMLMediaElement.currentTime:屬性會以秒爲單位返回當前媒體元素的播放時間。設置這個屬性會改變媒體元素當前播放位置。

連續觸發鍵盤事件的時候,若要聲效可以達到連續播放的效果,要把currentTime的值初始化爲0設計模式

if(audio){
      audio.currentTime = 0;//cosecutive playing 
      audio.play()
    }

Final Code

github repo:https://github.com/Torrie-Leung/JS30
htmldom

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="style.css">
  <script src="player.js"></script>
  <title>Document</title>
</head>
<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">hithat</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="sounds/clap.wav"></audio>
  <audio data-key="83" src="sounds/hihat.wav"></audio>
  <audio data-key="68" src="sounds/kick.wav"></audio>
  <audio data-key="70" src="sounds/openhat.wav"></audio>
  <audio data-key="71" src="sounds/boom.wav"></audio>
  <audio data-key="72" src="sounds/ride.wav"></audio>
  <audio data-key="74" src="sounds/snare.wav"></audio>
  <audio data-key="75" src="sounds/tom.wav"></audio>
  <audio data-key="76" src="sounds/tink.wav"></audio>
  
</body>
</html>

csside

html{
  font-size: 10px;
  background: url("bg.jpg");
}
body,html{
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}
.keys{
  display: flex;
  flex: 1;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
}
.key {
  border:4px solid black;
  border-radius:5px;
  margin:1rem;
  font-size: 1.5rem;
  padding:1rem .5rem;
  transition:all .07s;
  width:100px;
  text-align: center;
  color:white;
  background:rgba(0,0,0,0.3);
  text-shadow:0 0 5px black;
}
.playing {
  transform:scale(1.1);
  border-color:#ffc600;
  box-shadow: 0 0 10px #ffc600;
}
kbd{
  display: block;
  font-size: 40px;
}
.sound {
  font-size: 1.2rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color:#ffc600;
}

js函數

window.onload = function(){
  (function(){
  function playHandler(e){
    //play music
    const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
    if(audio){
      //console.log(audio.currentTime)
      audio.currentTime = 0;//cosecutive playing 
      audio.play()
    }
    //dom style
    const dom = document.querySelector(`div[data-key="${e.keyCode}"]`)
    if(dom)dom.classList.add('playing')
    
  }
  
  function transitionendHandler(e){
    console.log(e.propertyName)
      if(e.propertyName === 'transform'|| e.propertyName === 'border-top-color'){
        e.currentTarget.classList.remove('playing')
      }
  }
  
  window.addEventListener('keydown',playHandler)
  document.querySelectorAll('.key').forEach( key => {
    key.addEventListener('transitionend',transitionendHandler)
  })
  
})();
}

References

相關文章
相關標籤/搜索