-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
82 lines (68 loc) · 2.43 KB
/
script.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
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
const musicContainer = document.getElementById("music-container");
const playButton = document.getElementById("play");
const prevButton = document.getElementById("prev");
const nextButton = document.getElementById("next");
const audio = document.getElementById("audio");
const progress = document.getElementById("progress");
const progressContainer = document.getElementById("progress-container");
const title = document.getElementById("title");
const cover = document.getElementById("cover");
const coverer = document.getElementById("coverer");
const songs = [ "alexis", "summer", "ni lesa wabomba", "victoria", "theo"];
let songIndex = 1;
function getSongTitle(song) {
return song.charAt(0).toUpperCase() + song.slice(1);
}
function loadSong(song) {
title.innerText = getSongTitle(song);
audio.src = `assets/music/${song}.mp3?raw=true`;
cover.src = `assets/img/musiccover/${song}.jpg?raw=true`;
coverer.src = `assets/img/musiccover/${song}.jpg?raw=true`;
}
function playSong() {
musicContainer.classList.add("play");
playButton.querySelector("i.bi").classList.remove("bi-play");
playButton.querySelector("i.bi").classList.add("bi-pause");
audio.play();
}
function pauseSong() {
musicContainer.classList.remove("play");
playButton.querySelector("i.bi").classList.remove("bi-pause");
playButton.querySelector("i.bi").classList.add("bi-play");
audio.pause();
}
function prevSong() {
songIndex--;
if (songIndex < 0) songIndex = songs.length - 1;
loadSong(songs[songIndex]);
playSong();
}
function nextSong() {
songIndex++;
if (songIndex > songs.length - 1) songIndex = 0;
loadSong(songs[songIndex]);
playSong();
}
function updateProgress(e) {
const { duration, currentTime } = e.srcElement;
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
}
function setProgress(e) {
const width = this.clientWidth;
const clickX = e.offsetX;
const duration = audio.duration;
audio.currentTime = (clickX / width) * duration;
}
// Event Listeners
playButton.addEventListener("click", () => {
const isPlaying = musicContainer.classList.contains("play");
isPlaying ? pauseSong() : playSong();
});
prevButton.addEventListener("click", prevSong);
nextButton.addEventListener("click", nextSong);
audio.addEventListener("timeupdate", updateProgress);
progressContainer.addEventListener("click", setProgress);
audio.addEventListener("ended", nextSong);
// Init
loadSong(songs[songIndex]);