-
Notifications
You must be signed in to change notification settings - Fork 51
/
app.js
120 lines (98 loc) · 3.88 KB
/
app.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
document.addEventListener("DOMContentLoaded", function () {
const audioPlayer = document.getElementById("audio-player");
const playlistItems = document.querySelectorAll("#playlist li");
const playPauseBtn = document.getElementById("playPauseBtn"); // Play/Pause button
// Playlist functionality: Play the clicked audio
playlistItems.forEach((item) => {
item.addEventListener("click", function () {
const audioSource = this.getAttribute("data-src");
audioPlayer.src = audioSource;
audioPlayer.play();
playPauseBtn.classList.remove("play"); // Update the button to pause when playing
playPauseBtn.classList.add("pause");
});
});
// function to speed audio
const speedButton = document.getElementById("speedButton");
const speedDropdown = document.getElementById("speedDropdown");
const audio = document.getElementById("audio");
// function for dropdown
speedButton.addEventListener("click", () => {
speedDropdown.classList.toggle("hidden");
});
speedDropdown.querySelectorAll("li").forEach((item) => {
item.addEventListener("click", () => {
const selectedSpeed = parseFloat(item.getAttribute("data-speed")); // Convert to float
audio.playbackRate = selectedSpeed;
speedButton.textContent = selectedSpeed + "x";
speedDropdown.classList.add("hidden");
});
});
audioPlayer.addEventListener("canplay", () => {
speedDropdown.querySelectorAll("li").forEach((item) => {
item.addEventListener("click", () => {
const selectedSpeed = parseFloat(item.getAttribute("data-speed"));
audioPlayer.playbackRate = selectedSpeed;
speedButton.textContent = selectedSpeed + "x";
speedDropdown.classList.add("hidden");
});
});
});
window.addEventListener("click", (e) => {
if (!speedButton.contains(e.target) && !speedDropdown.contains(e.target)) {
speedDropdown.classList.add("hidden");
}
});
// Play/Pause button functionality
playPauseBtn.addEventListener("click", () => {
if (audioPlayer.paused) {
audioPlayer.play();
playPauseBtn.classList.remove("play");
playPauseBtn.classList.add("pause");
} else {
audioPlayer.pause();
playPauseBtn.classList.remove("pause");
playPauseBtn.classList.add("play");
}
});
// Comment submission functionality
const commentForm = document.getElementById("comment-form");
const commentsList = document.getElementById("comments-list");
commentForm.addEventListener("submit", function (e) {
e.preventDefault();
const username = document.getElementById("username").value;
const commentText = document.getElementById("comment").value;
if (username && commentText) {
const commentElement = document.createElement("div");
commentElement.classList.add("comment");
commentElement.innerHTML = `<strong>${username}:</strong><p>${commentText}</p>`;
commentsList.appendChild(commentElement);
// Clear the form
commentForm.reset();
}
});
// Page navigation buttons functionality
const prevBtn = document.getElementById("prev-btn");
const nextBtn = document.getElementById("next-btn");
// Define the pages for navigation
const pages = ["index.html", "genres.html", "about.html"];
let currentPageIndex = pages.indexOf(window.location.pathname);
// Function to navigate to the next page
function nextPage() {
if (currentPageIndex < pages.length - 1) {
currentPageIndex++;
window.location.href = pages[currentPageIndex];
}
}
// Function to navigate to the previous page
function prevPage() {
if (currentPageIndex > 0) {
currentPageIndex--;
window.location.href = pages[currentPageIndex];
}
}
// Event listener for "Next" button
if (nextBtn) nextBtn.addEventListener("click", nextPage);
// Event listener for "Previous" button
if (prevBtn) prevBtn.addEventListener("click", prevPage);
});