forked from CodeExplainedRepo/Carousel-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarousel.js
More file actions
124 lines (99 loc) · 3.9 KB
/
Copy pathcarousel.js
File metadata and controls
124 lines (99 loc) · 3.9 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// SELECT CAROUSEL
const carousel = document.querySelector(".carousel");
// SELECT NEXT BUTTON
const nextButton = document.querySelector(".right-btn");
// SELECT LEFT BUTTON
const previousButton = document.querySelector(".left-btn");
// SELECT THE NAV
const nav = document.querySelector(".nav");
// SELECT ALL THE DOTS
const dots = [...nav.children];
// SELECT ALL THE SLIDES INSIDE THE CAROUSEL
const slides = [...carousel.children];
// CALCULATE THE SLIDE WIDTH
let slideWidth = slides[0].getBoundingClientRect().width;
// POSITION THE SLIDES HORIZONTALY
function positionSlides(slides){
for(let index = 0; index < slides.length; index++){
slides[index].style.left = slideWidth * index + "px";
}
}
positionSlides(slides);
// ON RIGHT BUTTON CLICK, WE MOVE(TranslateX) THE CAROUSEL TO THE LEFT
nextButton.addEventListener("click", function(){
const currentSlide = carousel.querySelector(".active");
const nextSlide = currentSlide.nextElementSibling;
moveToSlide(carousel, currentSlide, nextSlide);
hideButton(nextSlide, slides);
moveToDot(nextSlide, slides, nav, dots);
});
// ON LEFT BUTTON CLICK, WE MOVE(TranslateX) THE CAROUSEL TO THE RIGHT
previousButton.addEventListener("click", function(){
const currentSlide = carousel.querySelector(".active");
const previousSlide = currentSlide.previousElementSibling;
moveToSlide(carousel, currentSlide, previousSlide);
hideButton(previousSlide, slides);
moveToDot(previousSlide, slides, nav, dots);
});
// ON DOT CLICK
nav.addEventListener("click", function(e){
// if we didn't click on a dot, we exit
if(e.target === nav) return;
// SELECT THE CLICKED DOT
const targetDot = e.target;
// SELECT THE CURRENT DOT
const currentDot = nav.querySelector(".active");
// SELECT THE CURRENT SLIDE
const currentSlide = carousel.querySelector(".active");
// find the index of the dot, so we can target the right slide
let targetDotIndex = findIndex(targetDot, dots);
// SELECT THE TARGET SLIDE
const targetSlide = slides[targetDotIndex];
moveToSlide(carousel, currentSlide, targetSlide);
toggleActive(currentDot, targetDot);
hideButton(targetSlide, slides);
})
// MOVE TO DOT
function moveToDot(targetSlide, slides, nav, dots){
let slideIndex = findIndex(targetSlide, slides);
const currentDot = nav.querySelector(".active");
const targetDot = dots[slideIndex];
toggleActive(currentDot, targetDot);
}
// MOVE TO SLIDE
function moveToSlide(carousel, currentSlide, targetSlide){
const position = targetSlide.style.left;
carousel.style.transform = `translateX(-${position})`;
toggleActive(currentSlide, targetSlide);
}
// Toggle ACTIVE CLASS
function toggleActive(current, target){
current.classList.remove("active");
target.classList.add("active");
}
// HIDE BUTTON
function hideButton(targetSlide, slides){
// If the target slide is the first slide the previous button must be hidden
// and the next button must be shown
if(targetSlide === slides[0]){
previousButton.classList.add("hide");
nextButton.classList.remove("hide");
}else if(targetSlide === slides[slides.length - 1]){
// If the target slide is the last slide the next button must be hidden
// and the previous button must be shown
nextButton.classList.add("hide");
previousButton.classList.remove("hide");
}else{
// if none of the above is true, we show both the next and prevoius button
previousButton.classList.remove("hide");
nextButton.classList.remove("hide");
}
}
// FIND THE INDEX OF AN ITEM, INSIDE AN ARRAY OF ITEMS
function findIndex(item, items){
for(let index = 0; index < items.length; index++){
if(item === items[index]){
return index;
}
}
}