Skip to content

Commit

Permalink
Click Event Added
Browse files Browse the repository at this point in the history
  • Loading branch information
markdavepanado committed Mar 12, 2020
1 parent 82c2f08 commit 571a818
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,37 @@ window.addEventListener("keyup", stop);
function stop(e) {
delete keysdown[e.key];
}

addClickEventListener();

function addClickEventListener() {
const keys = document.querySelectorAll(`.key`);

try {
for (var i = 0; i <= keys.length; i++) {
keys[i].addEventListener("click", clickKey);
}
} catch (e) {
// do nothing
}
}

function clickKey() {
const dataKey = this.getAttribute("data-key");

const audio = document.querySelector(`audio[data-key="${dataKey}"]`);

audio.currentTime = 0;
audio.play();

this.classList.add("playing");

const keys = document.querySelectorAll(".key");
keys.forEach(key => key.addEventListener("transitionend", removeClass));
}

function mouseUpKey() {
console.log(this);

this.classList.remove("playing");
}

0 comments on commit 571a818

Please sign in to comment.