-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
254 lines (218 loc) · 7.11 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// "use strict";
// "use strict";
// !(function(d){
// let itemClassName = 'carousel-photo';
// let items = qsa('.carousel-photo');
// console.log(items);
// let totalItems = items.length;
// let slide = 0;
// let moving = true;
// // Set classes
// function setInitialClasses() {
// // Targets the previous, current, and next items
// // This assumes there are at least three items.
// items[totalItems - 1].classList.add('prev');
// items[0].classList.add('active');
// items[1].classList.add('next');
// console.log('set initial class');
// }
// // Set event listeners
// function setEventListeners() {
// let next = qsa('.carousel-button-next')[0],
// prev = qsa('.carousel-button-prev')[0];
// next.addEventListener('click', moveNext);
// prev.addEventListener('click', movePrev);
// console.log('set event listener');
// }
// // Next navigation handler
// function moveNext() {
// console.log('moveNext called');
// // Check if moving
// if (!moving) {
// // If it's the last slide, reset to 0, else +1
// if (slide === (totalItems - 1)) {
// slide = 0;
// } else {
// slide++;
// }
// // Move carousel to updated slide
// moveCarouselTo(slide);
// }
// }
// // Previous navigation handler
// function movePrev() {
// console.log('movePrev called');
// // Check if moving
// if (!moving) {
// // If it's the first slide, set as the last slide, else -1
// if (slide === 0) {
// slide = (totalItems - 1);
// } else {
// slide--;
// }
// // Move carousel to updated slide
// moveCarouselTo(slide);
// }
// }
// function disableInteraction() {
// console.log('disabling interaction');
// // Set 'moving' to true for the same duration as our transition.
// // (0.5s = 500ms)
// moving = true;
// // setTimeout runs its function once after the given time
// setTimeout(function(){
// moving = false
// }, 500);
// }
// function moveCarouselTo(slide) {
// console.log('moving carousel')
// // Check if carousel is moving, if not, allow interaction
// if(!moving) {
// // temporarily disable interactivity
// disableInteraction();
// // Update the "old" adjacent slides with "new" ones
// var newPrevious = slide - 1,
// newNext = slide + 1,
// oldPrevious = slide - 2,
// oldNext = slide + 2;
// // Test if carousel has more than three items
// if ((totalItems - 1) > 3) {
// // Checks and updates if the new slides are out of bounds
// if (newPrevious <= 0) {
// oldPrevious = (totalItems - 1);
// } else if (newNext >= (totalItems - 1)){
// oldNext = 0;
// }
// // Checks and updates if slide is at the beginning/end
// if (slide === 0) {
// newPrevious = (totalItems - 1);
// oldPrevious = (totalItems - 2);
// oldNext = (slide + 1);
// } else if (slide === (totalItems -1)) {
// newPrevious = (slide - 1);
// newNext = 0;
// oldNext = 1;
// }
// // Now we've worked out where we are and where we're going,
// // by adding/removing classes we'll trigger the transitions.
// // Reset old next/prev elements to default classes
// items[oldPrevious].className = itemClassName;
// items[oldNext].className = itemClassName;
// // Add new classes
// items[newPrevious].className = itemClassName + " prev";
// items[slide].className = itemClassName + " active";
// items[newNext].className = itemClassName + " next";
// }
// }
// }
// /**
// * Returns all of the selectors
// * @param {selector} all of the selectors selected
// * @returns {selector} all of the selectors
// */
// function qsa(selector) {
// return d.querySelectorAll(selector);
// }
// function initCarousel() {
// setInitialClasses();
// setEventListeners();
// // Set moving to false so that the carousel becomes interactive
// moving = false;
// }
// initCarousel();
// }(document));
"use strict";
(function() {
window.addEventListener('load', init);
let photos = qsa('.carousel-photo');
// Stores the html element that contains the current photo
let currentPhoto = getInitialElement(photos);
// n = how many photos are there
let n = photos.length;
let indicators = qsa('.indicator')
function init() {
let nextBtn = qs('.carousel-button-next');
let prevBtn = qs('.carousel-button-prev');
nextBtn.addEventListener('click', goNext);
prevBtn.addEventListener('click', goPrev);
}
function updateCurrentIndicator() {
('update curr indicator')
// Remove active class from previous indicator
let activeIndicator = getActiveElement(indicators);
if (activeIndicator !== null) {
activeIndicator.classList.remove('active');
}
// Add active class to current indicator
let currIndex = parseInt(currentPhoto.id);
indicators[currIndex - 1].classList.add('active');
removeInitialClass(getInitialElement(indicators));
}
function goNext() {
let nextPhoto = (parseInt(currentPhoto.id) % n) + 1;
currentPhoto.classList.remove('active');
photos[nextPhoto - 1].classList.add('active');
currentPhoto = photos[nextPhoto - 1];
removeInitialClass(getInitialElement(photos));
updateCurrentIndicator();
}
function goPrev() {
let oldPhoto = (parseInt(currentPhoto.id));
let nextPhoto;
if (oldPhoto === 1) {
nextPhoto = n;
} else {
nextPhoto = oldPhoto - 1;
}
currentPhoto.classList.remove('active');
photos[nextPhoto - 1].classList.add('active');
currentPhoto = photos[nextPhoto - 1];
removeInitialClass(getInitialElement(photos));
updateCurrentIndicator();
}
function getActiveElement(nodeList) {
for (let i = 0; i < nodeList.length; i++) {
if (nodeList[i].classList.contains('active')) {
return nodeList[i];
}
}
return null;
}
function getInitialElement(nodeList) {
for (let i = 0; i < nodeList.length; i++) {
if (nodeList[i].classList.contains('initial')) {
return nodeList[i];
}
}
return null;
}
function removeInitialClass(element) {
if (element !== null) {
element.classList.remove('initial');
}
}
/**
* Returns the element that has the ID attribute with the specified value.
* @param {string} id - element ID
* @return {object} DOM object associated with id.
*/
function id(id) {
return document.getElementById(id);
}
/**
* Returns the first element that matches the given CSS selector.
* @param {string} query - CSS query selector.
* @return {object[]} array of DOM objects matching the query.
*/
function qs(query) {
return document.querySelector(query);
}
/**
* Returns all of the selectors
* @param {selector} all of the selectors selected
* @returns {selector} all of the selectors
*/
function qsa(selector) {
return document.querySelectorAll(selector);
}
})();