-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
92 lines (79 loc) · 3.05 KB
/
script.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
window.addEventListener('load', () => {
{
// Get elements
const carouselWrapper = document.querySelector(".carousel-wrapper");
const carousel = document.querySelector(".carousel");
const carouselItem = document.querySelector(".carousel__item");
const carouselOffsetWidth = carousel.offsetWidth;
const carouselScrollWidth = carousel.scrollWidth;
const carouselItemOffsetWidth = carouselItem.offsetWidth;
const nextBtn = document.querySelector("#carousel__button--next");
const prevBtn = document.querySelector("#carousel__button--prev");
let carouselScrollLeftWidth = carousel.scrollLeft;
//Handle Next BTN click
nextBtn.addEventListener('click', function () {
carouselScrollLeftWidth += carouselItemOffsetWidth;
if ((carouselScrollLeftWidth + carouselOffsetWidth) >= carouselScrollWidth) {
hideNextBTN();
showPrevBTN();
} else {
showPrevBTN();
}
carousel.scrollLeft = carouselScrollLeftWidth;
});
//Handle Prev BTN click
prevBtn.addEventListener('click', () => {
carouselScrollLeftWidth -= carouselItemOffsetWidth;
if (carouselScrollLeftWidth <= 0) {
showNextBTN();
hidePrevBTN();
carouselScrollLeftWidth = 0;
} else {
showNextBTN();
}
carousel.scrollLeft = carouselScrollLeftWidth;
});
// Show Carousel arrows on carousel hover
carouselWrapper.addEventListener("mouseover", showCarouselBTNs);
function showCarouselBTNs() {
// If I can slide to the right then hide the prev BTN
showNextBTN();
showPrevBTN();
// show the right arrow if we can scroll to the left. if I am at the end of the scroll left
if ((carouselScrollLeftWidth + carouselOffsetWidth) >= carouselScrollWidth) {
// The end of the scroll
// Hide right arrow
hideNextBTN();
showPrevBTN();
} else {
showPrevBTN();
}
// show the left arrow if I am at the right scroll
if (carouselScrollLeftWidth <= 0) {
showNextBTN();
hidePrevBTN();
carouselScrollLeftWidth = 0;
} else {
showPrevBTN();
}
}
// Hide Carousel arrows on carousel hover
carouselWrapper.addEventListener("mouseout", hideCarouselBTNs);
function hideCarouselBTNs() {
hideNextBTN();
hidePrevBTN();
}
function showNextBTN() {
nextBtn.style.display = "block";
}
function showPrevBTN() {
prevBtn.style.display = "block";
}
function hideNextBTN() {
nextBtn.style.display = "none";
}
function hidePrevBTN() {
prevBtn.style.display = "none";
}
}
});