forked from samuel-hamilton/personal-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
44 lines (36 loc) · 1.53 KB
/
scripts.js
File metadata and controls
44 lines (36 loc) · 1.53 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
document.addEventListener("DOMContentLoaded", function() {
let currentSlide = 0;
const carouselItems = document.querySelectorAll(".carousel-item");
function goToSlide(slideIndex) {
carouselItems[currentSlide].classList.remove("active");
currentSlide = (slideIndex + carouselItems.length) % carouselItems.length;
carouselItems[currentSlide].classList.add("active");
const carouselInner = document.querySelector(".carousel-inner");
carouselInner.style.transform = `translateX(-${currentSlide * 100}%)`;
updateDots(currentSlide); // Add this line
}
function prevSlide() {
goToSlide(currentSlide - 1);
}
function nextSlide() {
goToSlide(currentSlide + 1);
}
function createDots() {
const carouselIndicators = document.querySelector('.carousel-indicators');
carouselItems.forEach((item, index) => {
const dot = document.createElement('button');
dot.classList.add('dot');
if (index === 0) dot.classList.add('active');
dot.addEventListener('click', () => goToSlide(index));
carouselIndicators.appendChild(dot);
});
}
function updateDots(slideIndex) {
const dots = document.querySelectorAll('.dot');
dots.forEach(dot => dot.classList.remove('active'));
dots[slideIndex].classList.add('active');
}
createDots(); // Call createDots() within the event listener
window.prevSlide = prevSlide;
window.nextSlide = nextSlide;
});