簡介:JavaScript30是Wes Bos推出的一個(gè) 30 天挑戰(zhàn)。項(xiàng)目免費(fèi)提供了 30 個(gè)視頻教程、30 個(gè)挑戰(zhàn)的起始文檔和 30 個(gè)挑戰(zhàn)解決方案源代碼。
簡介
第一天是用JS制作爵士鼓的頁面,通過敲擊鍵盤上對應(yīng)的字母就會(huì)發(fā)出對應(yīng)的聲音。

效果圖
主要解決的難點(diǎn):
1.將鍵盤事件與audio建立聯(lián)系
2.通過鍵盤按下事件點(diǎn)擊實(shí)現(xiàn)特效的展現(xiàn)與消失
ES6語法
const聲明常量
運(yùn)用了ES6中的模板字符串和箭頭函數(shù)
知識點(diǎn)
Array.forEach()函數(shù)和Array.from()的使用
html5中的data-*的使用

removeTransition
這個(gè)函數(shù)主要是實(shí)現(xiàn)當(dāng)按下相應(yīng)的鍵盤按鍵的時(shí)候,在頁面中顯示特效,并移除特效。

playSound
當(dāng)按下按鍵的時(shí)候,就會(huì)發(fā)出聲音

監(jiān)聽
window.addEventListener('keydown', playSound);監(jiān)聽事件,其中的參數(shù)playSound函數(shù)是有參數(shù)的,通過console.log查看,就可以看見使用addEventListener時(shí),給函數(shù)傳遞的參數(shù)是一個(gè)事件對象。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
html{
background-image: url(http://i.imgur.com/b9r5sEL.jpg);
}
html,body{
margin: 0px;
padding: 0px;
font-family: sans-serif;
}
.keys{
display: flex;
flex: 1;
min-height: 100vh;
align-items: center;
justify-content: center;
}
.key{
font-size: 0.2rem;
border: .4rem solid black;
border-radius: 50%;
margin: 0.6rem;
padding: .2rem .2rem;
transition: all .07s ease;
text-align: center;
background: rgba(0, 0, 0, 0.4);
}
.playing{
transform: scale(1.1);
transform: rotate(360deg);
border-color: #ffc600;
box-shadow: 0 0 2rem #ffc600;
}
kbd{
display: block;
color: #fc60fd;
}
.sound {
text-transform: uppercase;/* 大寫字母 */
letter-spacing: .1rem;
color: #ffc600;
}
</style>
</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 data-key="32" class="key">
<kbd>SPACE</kbd>
<span class="sound">bgmusic</span>
</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>
<audio data-key="32" src="sounds/ss.mp3"></audio>
</div>
</body>
<script type="text/javascript">
function removeTransition(e){
if(e.propertyName!=="transform")return;
e.target.classList.remove("playing");
}
function playSound(e){
//console.dir(e);
const key=document.querySelector(`div[data-key="${e.keyCode}"]`);
const audio=document.querySelector(`audio[data-key="${e.keyCode}"]`);
if(!audio)return;
key.classList.add("playing");
audio.currentTime=0;
audio.play();
}
const keys=Array.from(document.querySelectorAll(".key"));
keys.forEach(keys=>keys.addEventListener("transitionend",removeTransition));
//keys.forEach(()=>addEventListener("keydown",playSound));
window.addEventListener("keydown",playSound);
</script>
</html>