-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (49 loc) · 2.21 KB
/
index.js
File metadata and controls
53 lines (49 loc) · 2.21 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
// setting nav margin
var margTop = window.getComputedStyle(document.body).marginTop;
var margLeft = window.getComputedStyle(document.body).marginLeft;
var margRight = window.getComputedStyle(document.body).marginRight;
var mainCont = document.querySelector('.nav');
mainCont.style.marginTop = '-' + margTop;
mainCont.style.marginLeft = '-' + margLeft;
mainCont.style.marginRight = '-' + margRight;
// assigning img sources and sound sources
var imgElems = document.querySelectorAll('.main-container img');
var overlays = document.querySelectorAll('.overlay')
var images = ['tom1', 'tom2', 'tom3', 'tom4', 'crash', 'kick', 'snare'];
var keys = ['w', 'a', 's', 'd', 'i', 'j', 'l'];
for (var i = 0; i < imgElems.length; i++) {
var temp = 'images/' + images[i] + '.png';
var temp2 = 'sounds/' + images[i] + '.mp3';
imgElems[i].setAttribute('src', temp);
addOverlayClickHandler(overlays[i], temp2);
addOverlayKeyHandler(overlays[i], temp2, keys[i]);
}
function addOverlayClickHandler(overlay, soundSrc) {
overlay.addEventListener('mousedown', function () {
if (overlay.getAttribute('class').includes('pressed') != true) {
overlay.setAttribute('class', overlay.getAttribute('class') + ' pressed');
}
var sound = new Audio(soundSrc);
sound.play();
});
overlay.addEventListener('mouseup', function(){
overlay.setAttribute('class', overlay.getAttribute('class').replace(' pressed', ''));
})
}
function addOverlayKeyHandler(overlay, soundSrc, keyPress) {
document.addEventListener('keydown', function (event) {
if (event.key === keyPress || event.key === keyPress.toUpperCase()) {
if (overlay.getAttribute('class').includes('pressed') != true) {
overlay.setAttribute('class', overlay.getAttribute('class') + ' pressed');
}
var sound = new Audio(soundSrc);
sound.play();
}
});
document.addEventListener('keyup', function (event) {
if (event.key === keyPress || event.key === keyPress.toUpperCase()) {
overlay.setAttribute('class', overlay.getAttribute('class').replace(' pressed', ''));
}
})
}
// ---