-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (46 loc) · 1.51 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function playSound(e) {
/**
* 此的 this 等同 e.target
* this.getAttribute('data-key') 等同 this.dataset.key;
*/
const keyCode = e.keyCode || this.dataset.key;
const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
const key = document.querySelector(`div[data-key="${keyCode}"]`);
if (!audio | !key) return;
/**
* HTMLMediaElement
* https://developer.mozilla.org/zh-TW/docs/Web/API/HTMLMediaElement
*
* element.play(): 進行播放
* element.currentTime: 指定播放秒數
*/
audio.currentTime = 0;
audio.play();
// key.classList.add('playing');
key.classList.toggle("playing", true);
}
function removeTransition(e) {
// 只取一個屬性觸發
if (e.propertyName !== "transform") return;
// this.classList.remove('playing');
this.classList.toggle("playing", false);
}
/**
* querySelectorAll 為 NodeList 而非 Array (但 NodeList 也有 forEach 方法)
*
* 可以用 Array.from 包裹解決
* const keys = Array.from(document.querySelectorAll(`.key`));
*/
const keys = document.querySelectorAll(`.key`);
keys.forEach(key => {
/**
* transitionend
* https://developer.mozilla.org/zh-CN/docs/Web/Events/transitionend
*
* CSS transition 結束後觸發。
* 當 transition 完成前被移除(譬如移除 transition-property),或 display 設定 none,事件將不會被觸發。
*/
key.addEventListener("transitionend", removeTransition);
key.addEventListener("click", playSound);
});
window.addEventListener("keydown", playSound);