-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (34 loc) · 1.41 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
const carouselSlide = document.querySelector('.carousel-slide');
const carouselImages = document.querySelectorAll('.carousel-slide img');
//Buttons
const prevBtn = document.querySelector('#prevBtn');
const nextBtn = document.querySelector('#nextBtn');
//Counter
let counter = 1;
const size = carouselImages[0].clientWidth;
carouselSlide.style.transform = 'translateX('+ (-size * counter ) + 'px)';
//Button Listeners
nextBtn.addEventListener('click',()=>{
if(counter >= carouselImages.length-1) return;
carouselSlide.style.transition = "transform 0.4s ease-in-out";
counter++;
carouselSlide.style.transform = 'translateX('+ (-size * counter ) + 'px)';
});
prevBtn.addEventListener('click',()=>{
if (counter <= 0) return;
carouselSlide.style.transition = "transform 0.4s ease-in-out";
counter--;
carouselSlide.style.transform = 'translateX('+ (-size * counter ) + 'px)';
});
carouselSlide.addEventListener('transitionend',()=>{
if(carouselImages[counter].id=='lastClone'){
carouselSlide.style.transition = none;
counter=carouselImages.length-2;
carouselSlide.style.transform = 'translateX('+ (-size * counter ) + 'px)';
}
if(carouselImages[counter].id=='firstClone'){
carouselSlide.style.transition = none;
counter=carouselImages.length-counter;
carouselSlide.style.transform = 'translateX('+ (-size * counter ) + 'px)';
}
});