-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslider.js
More file actions
210 lines (177 loc) · 7.63 KB
/
slider.js
File metadata and controls
210 lines (177 loc) · 7.63 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Constructs a Slider object that enables automatic slideshow functionality.
* @param {Object} obj - An object containing the CSS selectors for various elements used in the slideshow.
* @param {string} obj.bannerWrapperSelector - The CSS selector for the banner wrapper element.
* @param {string} obj.bannerContentSelector - The CSS selector for the banner content element.
* @param {string} obj.slideIndicatorsSelector - The CSS selector for the slide indicator elements.
* @param {string} obj.slideControlsSelector - The CSS selector for the slide control elements.
* @param {string} obj.slideSelector - The CSS selector for the slide elements.
* @param {string} obj.indicatorSelector - The CSS selector for the slide indicator elements.
* @param {number} obj.intervalTime - The interval between slide transitions in milliseconds.
*/
class Slider {
constructor({
bannerWrapperSelector = '.banner',
bannerContentSelector = '#banner-content',
slideIndicatorsSelector = '#slide-indicators',
slideControlsSelector = '.slider-controls',
slideSelector = '.slide',
activeSlideSelector = '.slide.active',
indicatorSelector = '.slide-indicator',
intervalTime = 4000
} = {}) {
this.bannerContent = document.querySelector(bannerContentSelector);
this.bannerWrapper = document.querySelector(bannerWrapperSelector);
this.slideIndicators = document.querySelector(slideIndicatorsSelector);
this.sliderControls = document.querySelector(slideControlsSelector);
this.slides = document.querySelectorAll(slideSelector);
this.activeSlideSelector = activeSlideSelector;
this.indicators = document.querySelectorAll(indicatorSelector);
this.interval = intervalTime;
this.intervalID = null;
this.touchStartX = null;
this.threshold = 30;
this.slideIndicators.addEventListener('click', (event) => {
if (event.target.type === 'button') this.captionHandler(event);
});
this.sliderControls.addEventListener('click', (event) => {
if (event.target.type === 'button') this.controlsHandler(event);
});
this.bannerWrapper.addEventListener('touchstart', (event) => {
this.touchStartX = event.touches[0].clientX;
});
this.bannerWrapper.addEventListener('touchend', (event) => {
this.swipeHandler(event);
this.dropSlidesStyles();
});
this.bannerWrapper.addEventListener('touchmove', (event) => {
this.swipeAnimation(event);
});
}
/**
* Begins the slideshow from the specified start index.
* @param {number} [startIndex=0] - The index at which the slideshow should start.
*/
startSlideshow(startIndex = 0) {
this.bannerWrapper.style = `--duration: ${this.interval / 1000}s`;
if (this.intervalID) clearInterval(this.intervalID);
let currentIndex = startIndex;
this.intervalID = setInterval(() => {
this.dropActiveSlides(currentIndex);
currentIndex = (currentIndex + 1) % this.slides.length;
this.slides[currentIndex].classList.add("active");
this.indicators[currentIndex].classList.add("active");
this.indicators[currentIndex].setAttribute('aria-selected', true);
}, this.interval);
}
/**
* Handles the event that occurs when a slide indicator button is clicked.
* @param {Event} event - The click event.
*/
captionHandler(event) {
const button = event.target;
this.dropActiveSlides();
this.slides[button.value].classList.add('active')
button.classList.add('active');
button.setAttribute('aria-selected', true);
this.startSlideshow(button.value);
}
/**
* Handles the event that occurs when a slide control button is clicked.
* @param {Event} event - The click event.
*/
controlsHandler(event) {
let newSlideIndex;
const indicators = Array.from(this.slideIndicators.children)
indicators.forEach((indicator, index, array) => {
if (indicator.getAttribute('aria-selected') === 'true') {
const value = event.target.value;
const slidesCount = array.length - 1;
if (value === 'next') newSlideIndex = index === slidesCount ? 0 : index + 1;
else if (value === 'prev') newSlideIndex = index === 0 ? slidesCount : index - 1;
}
});
if (newSlideIndex !== undefined && newSlideIndex !== null && newSlideIndex !== NaN)
indicators[newSlideIndex]?.click();
}
/**
* Handles the event that occurs when the user finishes a swipe gesture.
* @param {Event} event - The touchend event.
*/
swipeHandler(event) {
if (this.touchStartX && Math.abs(this.touchStartX - event.changedTouches[0].clientX) > this.threshold) {
if (this.touchStartX > event.changedTouches[0].clientX) {
this.sliderControls.querySelector('[value="next"]').click();
} else {
this.sliderControls.querySelector('[value="prev"]').click();
}
}
this.touchStartX = null;
}
/**
* Handles the swipe animation of the slide elements. This function will
* determine the direction of the swipe and apply the appropriate
* CSS animation classes to the active and next slides.
*
* @param {TouchEvent} event - The touch event triggered by the 'touchmove' event listener.
*/
swipeAnimation(event) {
const activeSlide = document.querySelector(this.activeSlideSelector);
const currentTouchPositionX = event.touches[0].clientX;
const deltaX = currentTouchPositionX - this.touchStartX;
const activeSlideIndex = Array.from(this.slides).indexOf(activeSlide);
let nextSlideIndex;
// If moving to the right, choose the previous slide, else choose the next slide
if (deltaX > 0) {
nextSlideIndex = activeSlideIndex === 0 ? this.slides.length - 1 : activeSlideIndex - 1;
} else {
nextSlideIndex = (activeSlideIndex + 1) % this.slides.length;
}
const nextSlide = this.slides[nextSlideIndex];
// Determine the swipe direction
const swipeDirection = deltaX > 0 ? 'right' : 'left';
// Remove the existing animation classes
activeSlide.classList.remove('swipeInLeft', 'swipeInRight', 'swipeOutLeft', 'swipeOutRight');
nextSlide.classList.remove('swipeInLeft', 'swipeInRight', 'swipeOutLeft', 'swipeOutRight');
// Add the appropriate animation class based on the swipe direction
if (swipeDirection === 'right') {
activeSlide.classList.add('swipeOutRight');
nextSlide.classList.add('swipeInRight');
} else {
activeSlide.classList.add('swipeOutLeft');
nextSlide.classList.add('swipeInLeft');
}
}
/**
* Removes the "active" class and 'aria-selected' attribute from all slides and indicators,
* or a specific slide and indicator if an index is provided.
* @param {number} [index] - The index of the slide and indicator to deactivate.
*/
dropActiveSlides(index) {
if (index) {
this.slides[index].classList.remove("active");
this.indicators[index].classList.remove("active");
this.indicators[index].setAttribute('aria-selected', false);
} else {
for (let slide of this.slides) slide.classList.remove('active');
for (let indicator of this.indicators) {
indicator.classList.remove('active')
indicator.setAttribute('aria-selected', false);
};
}
}
/**
* Removes swipe animation CSS classes from all slide elements after a delay.
* This method is used to clean up the CSS classes added during the swipe animation,
* allowing for the animations to be applied again when the next swipe occurs.
*/
dropSlidesStyles() {
setTimeout(() => {
for (let slide of this.slides) {
slide.classList.remove('swipeInLeft', 'swipeInRight', 'swipeOutLeft', 'swipeOutRight');
}
}, 500)
}
}
const slider = new Slider();
slider.startSlideshow();