-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
98 lines (78 loc) · 3.52 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
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
class Carousel{
constructor(parentContainer){
this.carousel = parentContainer;
}
// moveBetweenSlides(){
// let parent = this.carousel;
// setTimeout(function(){
// let activeSlide = parent.querySelector(".slide.translate-x-0.active");
// activeSlide.classList.remove("translate-x-0", "active");
// activeSlide.classList.add("-translate-x-full");
// let nextSlide = activeSlide.nextElementSibling;
// nextSlide.classList.remove("translate-x-full");
// nextSlide.classList.add("translate-x-0", "active");
// }, 3000);
// }
//I need to rework the function above to allow it to continuously switch between the two carousel slides by using setInterval and the previousSlide and nextSlide methods
nextSlide(){
let parent = this.carousel;
let activeSlide = parent.querySelector(".slide.translate-x-0.active");
let nextSlide = activeSlide.nextElementSibling;
if(nextSlide !== null && nextSlide.classList.contains("slide")){
activeSlide.classList.remove("translate-x-0", "active");
activeSlide.classList.add("-translate-x-full");
nextSlide.classList.remove("translate-x-full");
nextSlide.classList.add("translate-x-0", "active");
}
//switch button color to represent you are now on a new slide
this.switchActiveBtn();
}
previousSlide(){
let parent = this.carousel;
let activeSlide = parent.querySelector(".slide.translate-x-0.active");
let previousSlide = activeSlide.previousElementSibling;
if(previousSlide !== null && previousSlide.classList.contains("slide")){
activeSlide.classList.remove("translate-x-0", "active");
activeSlide.classList.add("translate-x-full");
previousSlide.classList.remove("-translate-x-full");
previousSlide.classList.add("translate-x-0", "active");
}
//switch button color to represent you are now on a new slide
this.switchActiveBtn();
}
setActiveBtnColor(){
let parent = this.carousel;
let activeBtn = parent.querySelector("button.active");
activeBtn.classList.add("bg-yellow-600", "border-yellow-600");
}
switchActiveBtn(){
let parent = this.carousel;
let activeBtn = parent.querySelector("button.active");
let inactiveBtn = parent.querySelector("button:not(.active)");
if(activeBtn){
activeBtn.classList.remove("active", "bg-yellow-600", "border-yellow-600");
inactiveBtn.classList.add("active", "bg-yellow-600", "border-yellow-600");
}
}
setEventListerners(){
let parent = this.carousel;
let prevBtn = this.carousel.querySelector(".prev");
prevBtn.addEventListener("click",() => {
this.previousSlide()
});
let nextBtn = this.carousel.querySelector(".next");
nextBtn.addEventListener("click",() => {
this.nextSlide()
});
if(prevBtn.classList.contains("active") || nextBtn.classList.contains("active")){
this.setActiveBtnColor();
}
}
}
const header = document.querySelector(".carousel-parent");
const latestProd = document.querySelector(".latest-carousel")
const headerCarousel = new Carousel(header);
const latestCarousel = new Carousel(latestProd);
headerCarousel.setEventListerners();
latestCarousel.setEventListerners();
// carousel.moveBetweenSlides();