-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
241 lines (180 loc) · 7.21 KB
/
script.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
const controllerButtons = document.querySelector('.carousel__controllers');
const wrapper = document.querySelector('.carousel__wrapper');
let items = wrapper.querySelectorAll('.carousel__item');
const firsItemClone = items[0].cloneNode(true);
const secondItemClone = items[1].cloneNode(true);
const lastItemClone = items[2].cloneNode(true);
wrapper.insertAdjacentElement("beforeend", firsItemClone);
wrapper.insertAdjacentElement("beforeend", secondItemClone);
wrapper.insertAdjacentElement("afterbegin", lastItemClone);
items = wrapper.querySelectorAll('.carousel__item');
const lastItem = items[items.length - 1];
wrapper.insertAdjacentElement('afterbegin', lastItem);
const dotsActive = (itemOnScreen) => {
const dots = document.querySelectorAll('.carousel__button');
dots.forEach((button, index) => {
button.style.transition = 'all 0.5s';
setTimeout(() => {
button.classList.toggle('carousel__button--active', index === Number(itemOnScreen));
}, 100)
});
}
const carouselDotControllers = (dotIndex, marginLeft, itemOnScreen) => {
const startItems = wrapper.querySelectorAll('.carousel__item');
const carouselFirstItem = startItems[startItems.length - 1];
const carouselFirsItem = startItems[0];
const carouselSecondItem = startItems[1];
const carouselLastButOneItem = startItems[startItems.length - 2];
dotsActive(dotIndex);
wrapper.style.transform = `translateX(${marginLeft})`;
wrapper.style.transition = 'all 0.5s';
setTimeout(() => {
wrapper.style.transition = 'none';
switch (dotIndex) {
case 0:
if (itemOnScreen === 1) {
wrapper.insertAdjacentElement('afterbegin', carouselFirstItem);
} else {
wrapper.insertAdjacentElement('afterbegin', carouselFirstItem);
wrapper.insertAdjacentElement('afterbegin', carouselLastButOneItem);
}
wrapper.style.transform = `translateX(-${(100 / startItems.length) * 2}%)`
break;
case 1:
if (itemOnScreen === 0) {
next();
} else {
wrapper.insertAdjacentElement('afterbegin', carouselFirstItem);
wrapper.style.transform = `translateX(-${(100 / startItems.length) * 2}%)`
}
break;
case 2:
if (itemOnScreen === 0) {
wrapper.insertAdjacentElement('beforeend', carouselFirsItem);
wrapper.insertAdjacentElement('beforeend', carouselSecondItem);
} else {
wrapper.insertAdjacentElement('beforeend', carouselFirsItem);
wrapper.style.transform = `translateX(-${(100 / startItems.length) * 2}%)`
}
break;
default:
wrapper.insertAdjacentElement('afterbegin', carouselFirstItem);
break;
}
}, 500);
}
const next = () => {
const nextItems = wrapper.querySelectorAll('.carousel__item');
const nextIndexItemOnScreen = Number(nextItems[nextItems.length - 3].attributes.index.value);
const nextFistItem = nextItems[0];
dotsActive(nextIndexItemOnScreen);
wrapper.style.transform = `translateX(-${(100 / nextItems.length) * 2 + 100 / nextItems.length}%)`;
wrapper.style.transition = 'all 0.5s';
setTimeout(() => {
wrapper.style.transition = 'none';
wrapper.insertAdjacentElement('beforeend', nextFistItem);
wrapper.style.transform = `translateX(-${(100 / nextItems.length) * 2}%)`;
}, 500);
}
const prev = () => {
const prevItems = wrapper.querySelectorAll('.carousel__item');
const prevIndexItemOnScreen = Number(prevItems[prevItems.length - 2].attributes.index.value);
const prevLastItem = prevItems[prevItems.length - 1];
dotsActive(prevIndexItemOnScreen);
wrapper.style.transform = `translateX(-${(100 / prevItems.length)}%)`;
wrapper.style.transition = 'all 0.5s';
setTimeout(() => {
wrapper.style.transition = 'none';
wrapper.insertAdjacentElement('afterbegin', prevLastItem);
wrapper.style.transform = `translateX(-${(100 / prevItems.length) * 2}%)`;
}, 500);
}
// ACTION CLICK ON DOT CONTROLS
document.querySelectorAll('.carousel__button').forEach((button, index) => button.addEventListener('click', () => {
switch (button.id) {
case 'dot1':
const dot1Items = wrapper.querySelectorAll('.carousel__item');
const dot1ItemsOnScreen = Number(dot1Items[dot1Items.length - 4].attributes.index.value);
const indexDot1 = 0;
if (dot1ItemsOnScreen === index) {
dotsActive(dot1ItemsOnScreen);
return;
};
const dot1MarginLeft = dot1ItemsOnScreen === 2 && dot1ItemsOnScreen !== 0 ? 0 : `-${(100 / 6)}%`;
carouselDotControllers(indexDot1, dot1MarginLeft, dot1ItemsOnScreen);
break;
case 'dot2':
const dot2Items = wrapper.querySelectorAll('.carousel__item');
const dot2ItemsOnScreen = Number(dot2Items[dot2Items.length - 4].attributes.index.value);
const indexDot2 = 1;
if (dot2ItemsOnScreen === index) {
dotsActive(dot2ItemsOnScreen);
return;
};
const dot2MarginLeft = dot2ItemsOnScreen === 0 && dot2ItemsOnScreen !== 1 ? `-${(100 / 6) * 3}%` : `-${(100 / 6)}%`;
carouselDotControllers(indexDot2, dot2MarginLeft, dot2ItemsOnScreen);
break;
case 'dot3':
const dot3Items = wrapper.querySelectorAll('.carousel__item');
const dot3ItemsOnScreen = Number(dot3Items[dot3Items.length - 4].attributes.index.value);
const indexDot3 = 2;
if (dot3ItemsOnScreen === index) {
dotsActive(dot3ItemsOnScreen);
return;
};
const dot3MarginLeft = dot3ItemsOnScreen === 0 && dot3ItemsOnScreen !== 2 ? `-${(100 / 6) * 4}%` : `-${(100 / 6) * 3}%`;
carouselDotControllers(indexDot3, dot3MarginLeft, dot3ItemsOnScreen);
default:
break;
}
}));
// DRAGGING SLIDE
let initialPosition = null;
let moving = false;
let transform = 0;
const transformMatrix = () => {
const transformMatrix = window.getComputedStyle(wrapper).getPropertyValue('transform');
if (transformMatrix !== 'none') {
return parseInt(transformMatrix.split(',')[4].trim());
}
}
const gestureStart = (e) => {
initialPosition = e.pageX;
moving = true;
transform = transformMatrix();
}
const gestureMove = (e) => {
if (moving) {
const currentPosition = e.pageX;
const diff = currentPosition - initialPosition;
wrapper.style.transform = `translateX(${transform + diff}px)`;
}
};
const gestureEnd = () => {
moving = false;
const itemsLength = items.length;
const scrollWidth = wrapper.scrollWidth;
const atualPosition = transformMatrix();
const scrollWidthPercentual = (100 * atualPosition) / scrollWidth + (100 / itemsLength) * 2;
if (scrollWidthPercentual < scrollWidthPercentual / 2) {
next();
} else if (scrollWidthPercentual > scrollWidthPercentual / 2) {
prev();
}
}
if (window.PointerEvent) {
wrapper.addEventListener('pointerdown', gestureStart);
wrapper.addEventListener('pointermove', gestureMove);
wrapper.addEventListener('pointerup', gestureEnd);
} else {
wrapper.addEventListener('touchdown', gestureStart);
wrapper.addEventListener('touchmove', gestureMove);
wrapper.addEventListener('touchup', gestureEnd);
wrapper.addEventListener('mousedown', gestureStart);
wrapper.addEventListener('mousemove', gestureMove);
wrapper.addEventListener('mouseup', gestureEnd);
}
// AUTOPLAY
setInterval(() => {
next();
}, 5000);