forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
85 lines (75 loc) · 2.19 KB
/
index.html
File metadata and controls
85 lines (75 loc) · 2.19 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sounds of the Squad</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="keys">
<div data-key="80" class="key">
<kbd>P</kbd>
<span class="sound">Prabh</span>
</div>
<div data-key="75" class="key">
<kbd>K</kbd>
<span class="sound">Kyra</span>
</div>
<div data-key="77" class="key">
<kbd>M</kbd>
<span class="sound">Mary</span>
</div>
<div data-key="73" class="key">
<kbd>I</kbd>
<span class="sound">Ian</span>
</div>
<div data-key="72" class="key">
<kbd>H</kbd>
<span class="sound">Helene</span>
</div>
<div data-key="82" class="key">
<kbd>R</kbd>
<span class="sound">Raisa</span>
</div>
<div data-key="66" class="key">
<kbd>B</kbd>
<span class="sound">Ping</span>
</div>
</div>
<audio data-key="75" src="sounds/kyra.mp3"></audio>
<audio data-key="77" src="sounds/mary.mp3"></audio>
<audio data-key="80" src="sounds/prabh.mp3"></audio>
<audio data-key="72" src="sounds/helene.mp3"></audio>
<audio data-key="73" src="sounds/ian.mp3"></audio>
<audio data-key="82" src="sounds/raisa.mp3"></audio>
<audio data-key="66" src="sounds/ping.mp3"></audio>
<script>
var onKeyPress = function(e){
playSoundForKey(e.keyCode);
}
var playSoundForKey = function(keyCode){
var audioElement = document.querySelector(`audio[data-key="${keyCode}"]`);
var keyElement = document.querySelector(`div[data-key="${keyCode}"]`);
if(!audioElement) return;
keyElement.classList.add("playing")
audioElement.currentTime = 0;
audioElement.play();
}
const keys = document.querySelectorAll('div.key');
keys.forEach(key => {
key.addEventListener('transitionend', function(e){
if(e.propertyName !== 'transform')
return;
e.target.classList.remove("playing");
});
});
keys.forEach(key =>{
key.addEventListener('click', function(e){
var curKey = e.currentTarget.getAttribute('data-key');
playSoundForKey(cujhrKey);
});
});
window.addEventListener('keydown', onKeyPress)
</script>
</body>
</html>