-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
197 lines (171 loc) · 6.77 KB
/
main.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
// get carousel DOM element
const carousel = document.querySelector(".carousel__items");
// and switch-btns container
const indicatorParent = document.querySelector(".carousel__switch-btns");
// prepare an array of DOM elements which of .carousel__items class
const carouselItemCollection = [];
// and array of indicator elements
const indicatorCollection = [];
// prepare a list of names for each wallpaper
const wallpaperSources = [
"reyna.jpg",
"killjoy.jpg",
"brimstone.png",
"jett.jpg",
"chamber.webp",
"neon.jpeg",
"omen.webp",
"phoenix.jpg",
"raze.jpg",
"sage.jpg",
"skye.jpg",
"viper.jpg",
"yoru.jpg",
];
// prepare events for active slides
const scrollToLeftest = function (e) {
e.stopPropagation();
switchSlides(-2);
};
const scrollToLeft = function (e) {
e.stopPropagation();
switchSlides(-1);
};
const scrollToRight = function (e) {
e.stopPropagation();
switchSlides(1);
};
const scrollToRightest = function (e) {
e.stopPropagation();
switchSlides(2);
};
// positioning of active slide elements
function appendVisualClasses(activeItemCollection) {
activeItemCollection[0].classList.add("carousel__item--leftest");
activeItemCollection[1].classList.add("carousel__item--left");
activeItemCollection[2].classList.add("carousel__item--main");
activeItemCollection[3].classList.add("carousel__item--right");
activeItemCollection[4].classList.add("carousel__item--rightest");
for (let i = 0; i < activeItemCollection.length; i++) {
activeItemCollection[i].classList.remove("carousel__item--hidden");
}
}
// active indicator mark
function resetIndicatorActiveCSS() {
for (let i = 0; i < indicatorCollection.length; i++) {
indicatorCollection[i].classList.remove("carousel__switch-btn--active");
}
}
// add/remove DOM listeners for active slides
function appendDOMListeners(activeItemCollection) {
activeItemCollection[0].addEventListener("pointerdown", scrollToLeftest);
activeItemCollection[1].addEventListener("pointerdown", scrollToLeft);
activeItemCollection[3].addEventListener("pointerdown", scrollToRight);
activeItemCollection[4].addEventListener("pointerdown", scrollToRightest);
}
function clearDOMListeners(activeItemCollection) {
activeItemCollection[0].removeEventListener("pointerdown", scrollToLeftest);
activeItemCollection[1].removeEventListener("pointerdown", scrollToLeft);
activeItemCollection[3].removeEventListener("pointerdown", scrollToRight);
activeItemCollection[4].removeEventListener(
"pointerdown",
scrollToRightest
);
}
function fillCarousel(sourceCollection) {
for (let i = 0; i < sourceCollection.length; i++) {
// create carousel item DOM element
const carouselItem = document.createElement("div");
carouselItem.classList.add("carousel__item");
// create image and append "src" attribute to it
const carouselImage = new Image();
carouselImage.src = "/img/" + sourceCollection[i];
carouselImage.alt = sourceCollection[i].split(".")[0] + "wallpaper";
carouselImage.classList.add("carousel__image");
carouselItem.appendChild(carouselImage);
// add that DOM element to items array
carouselItemCollection.push(carouselItem);
// add a carousel indicator to each slide
const indicator = document.createElement("button");
indicator.classList.add("carousel__switch-btn");
indicatorParent.appendChild(indicator);
indicatorCollection.push(indicator);
}
}
function InitializeCarousel() {
//create wallpaper images and put them into carousel
fillCarousel(wallpaperSources);
// add CSS display classes to first active items
appendVisualClasses(carouselItemCollection.slice(0, 5));
appendDOMListeners(carouselItemCollection.slice(0, 5));
// Hide all the other ones
for (let i = 5; i < carouselItemCollection.length; i++) {
carouselItemCollection[i].classList.add("carousel__item--hidden");
}
// push ready DOM elements into the carousel
for (let i = 0; i < carouselItemCollection.length; i++) {
carousel.appendChild(carouselItemCollection[i]);
}
}
InitializeCarousel();
// track which image is currently displayed first (The most left image on page)
let firstImageIndex = 0;
// get left/right arrows of the carousel
const scrollLeftBtn = document.querySelector(".carousel__move-btn--left");
const scrollRightBtn = document.querySelector(".carousel__move-btn--right");
// Switching slides. Direction is determening whether a left or right button was pressed, while selected is there in case indicators were pressed
function switchSlides(direction, selected) {
// reset each carousel item CSS to hide them
for (let i = 0; i < carouselItemCollection.length; i++) {
carouselItemCollection[i].className = "";
carouselItemCollection[i].classList.add("carousel__item");
carouselItemCollection[i].classList.add("carousel__item--hidden");
}
// clear "pointerdown" event listeners for side items
let slides = carouselItemCollection.slice(
firstImageIndex,
firstImageIndex + 5
);
slides = slides.concat(carouselItemCollection.slice(0, 5 - slides.length));
clearDOMListeners(slides);
if (selected !== undefined) {
firstImageIndex = selected;
} else {
// add or distract one from first image index and normalize it to fit the array
firstImageIndex += direction;
firstImageIndex =
firstImageIndex < 0
? carouselItemCollection.length + firstImageIndex
: firstImageIndex % carouselItemCollection.length;
}
// re-add the CSS classes to currently active five items and show them again
// plus add pointerdown event listeners
slides = carouselItemCollection.slice(firstImageIndex, firstImageIndex + 5);
if (slides.length < 5) {
slides = slides.concat(
carouselItemCollection.slice(0, 5 - slides.length)
);
}
appendVisualClasses(slides);
appendDOMListeners(slides);
//make correct indicator button active
resetIndicatorActiveCSS();
indicatorCollection[
(firstImageIndex + 2) % indicatorCollection.length
].classList.add("carousel__switch-btn--active");
}
//add DOM events to left/right buttons
scrollLeftBtn.addEventListener("pointerdown", () => {
switchSlides(-1);
});
scrollRightBtn.addEventListener("pointerdown", () => {
switchSlides(1);
});
//add DOM events to indicators
for (let i = 0; i < indicatorCollection.length; i++) {
indicatorCollection[i].addEventListener("pointerdown", () => {
resetIndicatorActiveCSS();
switchSlides(i - 2 - firstImageIndex);
indicatorCollection[i].classList.add("carousel__switch-btn--active");
});
}