JavaScript Drum Kit 指南(纯 JS 模拟敲鼓效果)
网络编程 2021-07-04 18:32www.168986.cn编程入门
这篇文章主要介绍了JavaScript Drum Kit 指南,也就是纯 JS 模拟敲鼓效果实现代码,需要的朋友可以参考下
核心代码
<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; // S 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 our 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浏览器下查看效果。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指