-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
32 lines (28 loc) · 919 Bytes
/
carousel.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
// JavaScript for carousel functionality
const carousels = document.querySelectorAll('.carousel');
const moveSlidesBack = (slides, prev, next) => {
slides.scrollBy({
left: -slides.offsetWidth,
behavior: 'smooth'
})
if(slides.scrollLeft === slides.offsetWidth) {
prev.disabled = true
} else {
next.disabled = false
}
}
const moveSlidesForth = (slides, prev, next) => {
slides.scrollBy({
left: slides.offsetWidth,
behavior: 'smooth'
});
prev.disabled = false
}
carousels.forEach((carousel) => {
let prevButton = carousel.querySelector('.prev');
let nextButton = carousel.querySelector('.next');
let slides = carousel.querySelector('.slides');
prevButton.addEventListener("click", () => moveSlidesBack(slides, prevButton, nextButton))
nextButton.addEventListener("click", () => moveSlidesForth(slides, prevButton, nextButton))
prevButton.disabled = true;
})