-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
44 lines (37 loc) · 1.53 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
44
// slide container
const carouselSlide = document.querySelector('.slide');
//all images
const carouselImages = document.querySelectorAll('.slide img');
//buttons
const prevBtn = document.querySelector('#prev-btn');
const nextBtn = document.querySelector('#next-btn');
//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 1s ease-in-out";
counter++;
carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';
});
prevBtn.addEventListener('click', ()=>{
if(counter <= 0) return;
carouselSlide.style.transition = "transform 1s ease-in-out";
counter--;
carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';
});
//event listener that triggers when a transition finishes
carouselSlide.addEventListener('transitionend', ()=> {
if(carouselImages[counter].id === 'last-clone'){
carouselSlide.style.transition = "none";
counter = carouselImages.length - 2;
carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';
}
if(carouselImages[counter].id === 'first-clone'){
carouselSlide.style.transition = "none";
counter = carouselImages.length - counter;
carouselSlide.style.transform = 'translateX(' + (-size * counter) + 'px)';
}
});