-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (69 loc) · 1.81 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
const music = document.querySelector("audio");
const img = document.querySelector("img");
const play = document.getElementById("play");
const artist = document.getElementById("artist");
const title = document.getElementById("title");
const prev = document.getElementById("prev");
const next = document.getElementById("next");
let isplaying = false;
const songs = [
{
name: "music-1",
title: "On My Way",
artist: "Farruko",
},
{
name: "music-2",
title: "Faded",
artist: "Alan Walker, Iselin Solheim",
},
{
name: "music-3",
title: "Heaven",
artist: "Julia Michaels",
},
];
// for play function
const playMusic = () => {
isplaying = true;
music.play();
play.classList.replace("fa-play", "fa-pause");
img.classList.add("anime");
};
// for pause function
const pauseMusic = () => {
isplaying = false;
music.pause();
play.classList.replace("fa-pause", "fa-play");
img.classList.remove("anime");
};
play.addEventListener('click', () => {
// if (isplaying) {
// pauseMusic();
// } else {
// playMusic();
// }
isplaying ? pauseMusic() : playMusic();
});
// changing the music data
const loadSong = (songs) => {
title.textContent = songs.title;
artist.textContent = songs.artist;
music.src = "musics/" + songs.name + ".mp3";
img.src = "images/" + songs.name + ".jpg";
};
songIndex = 0;
// loadSong(songs[0]);
const nextSong = () => {
// songIndex++;
songIndex = (songIndex + 1) % songs.length;
loadSong(songs[songIndex]);
playMusic();
};
const prevSong = () => {
songIndex = (songIndex - 1 + songs.length) % songs.length;
loadSong(songs[songIndex]);
playMusic();
};
next.addEventListener('click', nextSong);
prev.addEventListener('click', prevSong);