🐒:做为一个野生coder,为了夯实基础,决定跟着油管up主[ Alex 宅幹嘛 ]的教程开撸业界人气颇高的wes bos JS30系列,记录思考过程和知识要点。css
Day1-Drum Kithtml
实现效果:摁下键盘时摁键样式变化+播放相应音效
实现思路:监听键盘keydown事件,利用返回的keyCode去处理相应的样式和声效git
(function () { statements })();
这是一个被称为[自执行匿名函数]的设计模式,主要包含两部分。第一部分是包围在 [圆括号运算符
] 里的一个匿名函数,这个匿名函数拥有独立的词法做用域。这不只避免了外界访问此 IIFE 中的变量,并且又不会污染全局做用域。es6
第二部分再一次使用 ()
建立了一个当即执行函数表达式,JavaScript 引擎到此将直接执行函数。github
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() }
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) }) })(); }