当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript Drum Kit 指南(纯 JS 模拟敲鼓效果)

JavaScript Drum Kit 指南(纯 JS 模拟敲鼓效果)

2017年12月12日  | 移动技术网IT编程  | 我要评论

核心代码:

<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}"]`); // 根据触发按键的键码,获取对应音频
  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 事件监听
 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}"]`); // 根据触发按键的键码,获取对应音频
  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 事件监听
 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>

在线演示地址:

请在chrome浏览器下查看效果。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网