-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (37 loc) · 1.29 KB
/
Copy pathscript.js
File metadata and controls
49 lines (37 loc) · 1.29 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
const leftArrow = document.querySelector('.left-arrow');
const rightArrow = document.querySelector('.right-arrow');
const carousel = document.querySelector('.carousel');
const carouselItems = document.querySelectorAll('.carousel-item');
let itemWidth = carouselItems[0].offsetWidth;
let itemsToShow = 3;
let currentIndex = 0;
function updateItemsToShow() {
const carouselWidth = carousel.offsetWidth;
if (window.innerWidth < 768) {
itemsToShow = 1;
} else {
itemsToShow = Math.floor(carouselWidth / itemWidth);
}
itemWidth = carouselItems[0].offsetWidth;
}
function updateCarouselPosition() {
const totalItems = carouselItems.length;
if (currentIndex < 0) currentIndex = totalItems - itemsToShow;
if (currentIndex >= totalItems) currentIndex = 0;
const offset = currentIndex * itemWidth;
carousel.style.transform = `translateX(-${offset}px)`;
}
leftArrow.addEventListener('click', () => {
currentIndex--;
updateCarouselPosition();
});
rightArrow.addEventListener('click', () => {
currentIndex++;
updateCarouselPosition();
});
window.addEventListener('resize', () => {
updateItemsToShow();
updateCarouselPosition();
});
updateItemsToShow();
updateCarouselPosition();