-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (175 loc) · 6.35 KB
/
script.js
File metadata and controls
219 lines (175 loc) · 6.35 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Initial display
document.getElementById('search-form').addEventListener('submit', function(event) {
event.preventDefault();
let searchBox = document.getElementById('search_box');
let search = searchBox.value.trim().toLowerCase(); // Normalize the search input
let redirectTo = '';
if (search === 'javascript') {
redirectTo = "jsplaylist.html";
} else if (search === 'html') {
redirectTo = "playlist.html";
} else if (search === 'css') {
redirectTo = "cssplaylist.html";
} else {
redirectTo = "courses.html";
}
// Clear the search box
searchBox.value = '';
// Redirect to the determined page
window.location.href = redirectTo;
});
let toggleBtn = document.getElementById('toggle-btn');
let body = document.body;
let darkMode = localStorage.getItem('dark-mode');
const enableDarkMode = () =>{
toggleBtn.classList.replace('fa-sun', 'fa-moon');
body.classList.add('dark');
localStorage.setItem('dark-mode', 'enabled');
}
const disableDarkMode = () =>{
toggleBtn.classList.replace('fa-moon', 'fa-sun');
body.classList.remove('dark');
localStorage.setItem('dark-mode', 'disabled');
}
if(darkMode === 'enabled'){
enableDarkMode();
}
toggleBtn.onclick = (e) =>{
darkMode = localStorage.getItem('dark-mode');
if(darkMode === 'disabled'){
enableDarkMode();
}else{
disableDarkMode();
}
}
let profile = document.querySelector('.header .flex .profile');
document.querySelector('#user-btn').onclick = () =>{
profile.classList.toggle('active');
search.classList.remove('active');
}
document.addEventListener('click', (event) => {
const isClickInsideProfile = profile.contains(event.target);
const isClickInsideUserBtn = document.querySelector('#user-btn').contains(event.target);
if (!isClickInsideProfile && !isClickInsideUserBtn) {
profile.classList.remove('active');
}
});
// Prevent clicks inside the profile from closing it
profile.addEventListener('click', (event) => {
event.stopPropagation();
});
let search = document.querySelector('.header .flex .search-form');
document.querySelector('#search-btn').onclick = () =>{
search.classList.toggle('active');
profile.classList.remove('active');
}
document.addEventListener('click', (event) => {
const isClickInsideSearch = search.contains(event.target);
const isClickInsideSearchBtn = document.querySelector('#search-btn').contains(event.target);
if (!isClickInsideSearch && !isClickInsideSearchBtn) {
search.classList.remove('active');
}
});
// Prevent clicks inside the profile from closing it
profile.addEventListener('click', (event) => {
event.stopPropagation();
});
let sideBar = document.querySelector('.side-bar');
document.querySelector('#menu-btn').onclick = () =>{
sideBar.classList.toggle('active');
body.classList.toggle('active');
}
document.addEventListener('click', (event) => {
const isClickInsideSide = sideBar.contains(event.target);
const isClickInsideSideBtn = document.querySelector('#menu-btn').contains(event.target);
if (!isClickInsideSide && !isClickInsideSideBtn) {
sideBar.classList.remove('active');
}
});
// Prevent clicks inside the profile from closing it
profile.addEventListener('click', (event) => {
event.stopPropagation();
});
document.querySelector('#close-btn').onclick = () =>{
sideBar.classList.remove('active');
body.classList.remove('active');
}
document.addEventListener('DOMContentLoaded', function() {
const carouselContainer = document.querySelector('.carousel-container');
const slides = document.querySelectorAll('.carousel-slide');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const slideWidth = slides[0].offsetWidth; // Assuming all slides have the same width
let currentSlide = 0;
function scrollToSlide(index) {
// Calculate the scroll position based on the circular behavior
const newIndex = (index + slides.length) % slides.length;
const scrollLeft = newIndex * slideWidth;
carouselContainer.scrollTo({
left: scrollLeft,
behavior: 'smooth'
});
currentSlide = newIndex;
}
nextBtn.addEventListener('click', function() {
scrollToSlide(currentSlide + 1);
});
prevBtn.addEventListener('click', function() {
scrollToSlide(currentSlide - 1);
});
// Optional: Automatically scroll to the first slide on page load
scrollToSlide(0);
});
const slides = document.querySelectorAll('.carousel-slide');
const totalSlides = slides.length;
let currentIndex = 0;
// Function to show the next slide
function showNextSlide() {
currentIndex++;
if (currentIndex >= totalSlides) {
currentIndex = 0;
}
updateCarousel();
}
// Function to show the previous slide
function showPreviousSlide() {
currentIndex--;
if (currentIndex < 0) {
currentIndex = totalSlides - 1;
}
updateCarousel();
}
// Function to update the carousel
function updateCarousel() {
const carouselContainer = document.querySelector('.carousel-container');
const translateXValue = -currentIndex * 100;
carouselContainer.style.transform = `translateX(${translateXValue}%)`;
}
// Set the interval for the auto slide
let slideInterval = setInterval(showNextSlide, 3000); // Change slide every 2 seconds
// Pause the auto slide when hovering over the carousel
document.querySelector('.course-img').addEventListener('mouseenter', () => {
clearInterval(slideInterval);
});
// Resume the auto slide when leaving the carousel
document.querySelector('.course-img').addEventListener('mouseleave', () => {
slideInterval = setInterval(showNextSlide, 3000);
});
// Add event listeners to navigation buttons
document.getElementById('nextBtn').addEventListener('click', () => {
clearInterval(slideInterval); // Stop auto slide when manually navigating
showNextSlide();
slideInterval = setInterval(showNextSlide, 3000); // Restart auto slide
});
document.getElementById('prevBtn').addEventListener('click', () => {
clearInterval(slideInterval); // Stop auto slide when manually navigating
showPreviousSlide();
slideInterval = setInterval(showNextSlide, 3000); // Restart auto slide
});
updateCarousel();
window.onscroll = () =>{
profile.classList.remove('active');
search.classList.remove('active');
sideBar.classList.remove('active');
body.classList.remove('active');
}