-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
126 lines (103 loc) · 4.63 KB
/
carousel.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
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
const galleryContainer = document.querySelector('.gallery-container');
const galleryControlsContainer = document.querySelector('.gallery-controls');
const galleryControls = ['previous', 'next'];
const galleryItems = document.querySelectorAll('.gallery-item');
class Carousel {
constructor(container, items, controls) {
this.carouselContainer = container;
this.carouselControls = controls;
this.carouselArray = [...items];
}
// Assign initial css classes for gallery and nav items
setInitialState() {
this.carouselArray[0].classList.add('gallery-item-first');
this.carouselArray[1].classList.add('gallery-item-previous');
this.carouselArray[2].classList.add('gallery-item-selected');
this.carouselArray[3].classList.add('gallery-item-next');
this.carouselArray[4].classList.add('gallery-item-last');
document.querySelector('.gallery-nav').childNodes[0].className = 'gallery-nav-item gallery-item-first';
document.querySelector('.gallery-nav').childNodes[1].className = 'gallery-nav-item gallery-item-previous';
document.querySelector('.gallery-nav').childNodes[2].className = 'gallery-nav-item gallery-item-selected';
document.querySelector('.gallery-nav').childNodes[3].className = 'gallery-nav-item gallery-item-next';
document.querySelector('.gallery-nav').childNodes[4].className = 'gallery-nav-item gallery-item-last';
}
// Update the order state of the carousel with css classes
setCurrentState(target, selected, previous, next, first, last) {
selected.forEach(el => {
el.classList.remove('gallery-item-selected');
if (target.className == 'gallery-controls-previous') {
el.classList.add('gallery-item-next');
} else {
el.classList.add('gallery-item-previous');
}
});
previous.forEach(el => {
el.classList.remove('gallery-item-previous');
if (target.className == 'gallery-controls-previous') {
el.classList.add('gallery-item-selected');
} else {
el.classList.add('gallery-item-first');
}
});
next.forEach(el => {
el.classList.remove('gallery-item-next');
if (target.className == 'gallery-controls-previous') {
el.classList.add('gallery-item-last');
} else {
el.classList.add('gallery-item-selected');
}
});
first.forEach(el => {
el.classList.remove('gallery-item-first');
if (target.className == 'gallery-controls-previous') {
el.classList.add('gallery-item-previous');
} else {
el.classList.add('gallery-item-last');
}
});
last.forEach(el => {
el.classList.remove('gallery-item-last');
if (target.className == 'gallery-controls-previous') {
el.classList.add('gallery-item-first');
} else {
el.classList.add('gallery-item-next');
}
});
}
// Construct the carousel navigation
setNav() {
galleryContainer.appendChild(document.createElement('ul')).className = 'gallery-nav';
this.carouselArray.forEach(item => {
const nav = galleryContainer.lastElementChild;
nav.appendChild(document.createElement('li'));
});
}
// Construct the carousel controls
setControls() {
this.carouselControls.forEach(control => {
galleryControlsContainer.appendChild(document.createElement('button')).className = `gallery-controls-${control}`;
});
!!galleryControlsContainer.childNodes[0] ? galleryControlsContainer.childNodes[0].innerHTML = this.carouselControls[0] : null;
!!galleryControlsContainer.childNodes[1] ? galleryControlsContainer.childNodes[1].innerHTML = this.carouselControls[1] : null;
}
// Add a click event listener to trigger setCurrentState method to rearrange carousel
useControls() {
const triggers = [...galleryControlsContainer.childNodes];
triggers.forEach(control => {
control.addEventListener('click', () => {
const target = control;
const selectedItem = document.querySelectorAll('.gallery-item-selected');
const previousSelectedItem = document.querySelectorAll('.gallery-item-previous');
const nextSelectedItem = document.querySelectorAll('.gallery-item-next');
const firstCarouselItem = document.querySelectorAll('.gallery-item-first');
const lastCarouselItem = document.querySelectorAll('.gallery-item-last');
this.setCurrentState(target, selectedItem, previousSelectedItem, nextSelectedItem, firstCarouselItem, lastCarouselItem);
});
});
}
}
const exampleCarousel = new Carousel(galleryContainer, galleryItems, galleryControls);
exampleCarousel.setControls();
exampleCarousel.setNav();
exampleCarousel.setInitialState();
exampleCarousel.useControls();