-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (63 loc) · 2.39 KB
/
index.js
File metadata and controls
70 lines (63 loc) · 2.39 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
class Slideshow {
constructor(sliderId, leftArrowId, rightArrowId, indicatorParentId, totalSlides, slidePercentage) {
this.slider = document.getElementById(sliderId);
this.leftArrow = document.getElementById(leftArrowId);
this.rightArrow = document.getElementById(rightArrowId);
this.indicatorParents = document.getElementById(indicatorParentId);
this.totalSlides = totalSlides;
this.slidePercentage = slidePercentage;
this.essayIndex = 0;
// Check if all elements exist
if (!this.slider) {
console.error(`Slider element with ID ${sliderId} not found.`);
return;
}
if (!this.leftArrow) {
console.error(`Left arrow element with ID ${leftArrowId} not found.`);
return;
}
if (!this.rightArrow) {
console.error(`Right arrow element with ID ${rightArrowId} not found.`);
return;
}
if (!this.indicatorParents) {
console.error(`Indicator parent element with ID ${indicatorParentId} not found.`);
return;
}
this.init();
}
init() {
this.setIndex();
this.leftArrow.addEventListener('click', () => this.prevSlide());
this.rightArrow.addEventListener('click', () => this.nextSlide());
document.querySelectorAll(`#${this.indicatorParents.id} indicator`).forEach((indicator, ind) => {
indicator.addEventListener('click', () => {
this.essayIndex = ind;
this.setIndex();
});
});
}
setIndex() {
const selected = this.indicatorParents.querySelector('.selected');
if (selected) {
selected.classList.remove('selected');
}
this.slider.style.transform = `translate(${this.essayIndex * -this.slidePercentage}%)`;
this.indicatorParents.children[this.essayIndex].classList.add('selected');
}
nextSlide() {
this.essayIndex = (this.essayIndex < this.totalSlides - 1) ? this.essayIndex + 1 : 0;
this.setIndex();
}
prevSlide() {
this.essayIndex = (this.essayIndex > 0) ? this.essayIndex - 1 : this.totalSlides - 1;
this.setIndex();
}
}
// Instantiate the slideshows
document.addEventListener('DOMContentLoaded', () => {
new Slideshow('writing', 'leftSlide', 'rightSlide', 'platter', 3, 33.33333);
new Slideshow('heroes', 'leftSlide2', 'rightSlide2', 'platter2', 4, 25);
new Slideshow('music', 'leftSlide3', 'rightSlide3', 'platter3', 3, 33.33333);
new Slideshow('interests', 'leftSlide4', 'rightSlide4', 'platter4', 6, 16.66666);
});