欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript Drum Kit 指南(純 JS 模擬敲鼓效果)

 更新時間:2017年07月23日 19:44:37   投稿:mdxy-dxy  
這篇文章主要介紹了JavaScript Drum Kit 指南,也就是純 JS 模擬敲鼓效果實現(xiàn)代碼,需要的朋友可以參考下

核心代碼:

<script>
 function removeTransition(event) {
  if (event.propertyName !== 'transform') return; // 過濾其中一種事件
  event.target.classList.remove('playing'); // 移除高亮的樣式
 }

 function playSound(event) {
  const audio = document.querySelector(`audio[data-key="${event.keyCode}"]`); // 根據(jù)觸發(fā)按鍵的鍵碼,獲取對應音頻
  const key = document.querySelector(`div[data-key="${event.keyCode}"]`); // 獲取頁面對應按鈕 DIV 元素
  if (!audio) return; // 處理無效的按鍵事件

  key.classList.add('playing'); // 改變樣式
  audio.currentTime = 0; // 每次播放之后都使音頻播放進度歸零
  audio.play(); // 播放相應音效
 }

 const keys = Array.from(document.querySelectorAll('.key')); // 獲取頁面所有按鈕元素
 keys.forEach(key => key.addEventListener('transitionend', removeTransition)); // 添加 transition 事件監(jiān)聽
 window.addEventListener('keydown', playSound);
</script>

中文版本完整代碼:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>JS Drum Kit</title>
 <link rel="stylesheet" href="style.css" rel="external nofollow" rel="external nofollow" >
</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">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="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>

<script>
 function removeTransition(event) {
  if (event.propertyName !== 'transform') return; // 過濾其中一種事件
  event.target.classList.remove('playing'); // 移除高亮的樣式
 }

 function playSound(event) {
  const audio = document.querySelector(`audio[data-key="${event.keyCode}"]`); // 根據(jù)觸發(fā)按鍵的鍵碼,獲取對應音頻
  const key = document.querySelector(`div[data-key="${event.keyCode}"]`); // 獲取頁面對應按鈕 DIV 元素
  if (!audio) return; // 處理無效的按鍵事件

  key.classList.add('playing'); // 改變樣式
  audio.currentTime = 0; // 每次播放之后都使音頻播放進度歸零
  audio.play(); // 播放相應音效
 }

 const keys = Array.from(document.querySelectorAll('.key')); // 獲取頁面所有按鈕元素
 keys.forEach(key => key.addEventListener('transitionend', removeTransition)); // 添加 transition 事件監(jiān)聽
 window.addEventListener('keydown', playSound);
</script>


</body>
</html>

英文版本完整代碼:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>JS Drum Kit</title>
 <link rel="stylesheet" href="style.css" rel="external nofollow" rel="external nofollow" >
</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">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="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>

 <script>
  /** GOAL: When a user opens this page and presses a key that corresponds with
   * one of our div elements, we should play the audio clip associated with
   * the keypress, add a class to the specific element that matches with the keypress,
   * and then remove that class in order to reset the element to it's original state.
   */
  (()=> {
   const playSound = (e) => {
    const soundclip = document.querySelector(`audio[data-key="${e.keyCode}"]`);
    const keyelement = document.querySelector(`.key[data-key="${e.keyCode}"]`);
    if (!soundclip) return undefined; // Stop function from running if key pressed doesn't match up with our elements
    keyelement.classList.add('playing');
    // Ensures that the sound clip always plays from the beginning. Otherwise,
    // if the 'a' key is pressed twice rapidly, the soundclip will only play through
    // once.
    soundclip.currentTime = 0;
    soundclip.play(); // Play sound
   }
   const removeTransition = (e) => {
    // skip if it's not a transform event
    if (e.propertyName !== 'transform') return undefined;
    e.target.classList.remove('playing');
   }
   // Find all elements in the document with a class 'key'
   const keys = document.querySelectorAll('.key');
   // Listen for any `keydown` events that occur on this browser window instance (this page)
   // When a `keydown` event is observered, trigger the `playSound` function, passing in the
   // `keydown` event as the argument (e)
   window.addEventListener('keydown', playSound);
   keys.forEach(key =>
    key.addEventListener(
     'transitionend',
     (e) => removeTransition.call(key, e)
    ));
  })();
 </script>

</body>

</html>

在線演示地址:http://demo.jb51.net/js/2017/JavaScript30/JavaScriptDrumKit/index-FINISHED.html

請在chrome瀏覽器下查看效果。

相關文章

  • Bootstrap每天必學之導航組件

    Bootstrap每天必學之導航組件

    Bootstrap每天必學之導航組件,制作面包屑式導航、導航加下拉菜單效果,感興趣的小伙伴們可以參考一下
    2016-04-04
  • Promise 鏈式調(diào)用原理精簡示例

    Promise 鏈式調(diào)用原理精簡示例

    這篇文章主要為大家介紹了Promise 鏈式調(diào)用原理精簡示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • javascript實現(xiàn)十秒鐘后注冊按鈕可點擊的方法

    javascript實現(xiàn)十秒鐘后注冊按鈕可點擊的方法

    這篇文章主要介紹了javascript實現(xiàn)十秒鐘后注冊按鈕可點擊的方法,涉及javascript時間及樣式操作的相關技巧,需要的朋友可以參考下
    2015-05-05
  • javascript實現(xiàn)前端分頁效果

    javascript實現(xiàn)前端分頁效果

    這篇文章主要為大家詳細介紹了javascript實現(xiàn)前端分頁效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Bootstrap源碼學習筆記之bootstrap進度條

    Bootstrap源碼學習筆記之bootstrap進度條

    本文通過源碼給大家解析bootstrap進度條樣式,分為條紋進度條,動態(tài)條紋進度條,層疊進度條和帶Label的進度條,下面通過代碼給大家簡單介紹下,感興趣的朋友一起看看吧
    2016-12-12
  • 十個開發(fā)人員面臨的最常見的JavaScript問題總結

    十個開發(fā)人員面臨的最常見的JavaScript問題總結

    今天,JavaScript?是幾乎所有現(xiàn)代?Web?應用的核心。這就是為什么JavaScript問題,以及找到導致這些問題的錯誤,是?Web?發(fā)者的首要任務。本文總結了十個常見的問題及解決方法,需要的可以參考一下
    2022-11-11
  • 淺談JavaScript正則表達式分組匹配

    淺談JavaScript正則表達式分組匹配

    一個正則表達式要如何書寫才能同時匹配這兩個數(shù)字呢?簡單的字符表達式當然無法完成了,這個時候我們就可以定義一個字符集合(字符類)來進行匹配。這就是分組匹配了
    2015-04-04
  • JS實現(xiàn)兼容性好,帶緩沖的動感網(wǎng)頁右鍵菜單效果

    JS實現(xiàn)兼容性好,帶緩沖的動感網(wǎng)頁右鍵菜單效果

    這篇文章主要介紹了JS實現(xiàn)兼容性好,帶緩沖的動感網(wǎng)頁右鍵菜單效果,可實現(xiàn)帶有彈性效果并且能夠自定義鼠標事件的右鍵菜單功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • Vue elementUI實現(xiàn)免密登陸與號碼綁定功能

    Vue elementUI實現(xiàn)免密登陸與號碼綁定功能

    這篇文章主要介紹了Vue elementUI實現(xiàn)免密登陸與號碼綁定功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-11-11
  • javascript 獲取元素位置的快速方法 getBoundingClientRect()

    javascript 獲取元素位置的快速方法 getBoundingClientRect()

    有一種快速獲得網(wǎng)頁元素的位置。那就是使用getBoundingClientRect()方法。
    2009-11-11

最新評論