forked from backendforth/hero-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
308 lines (260 loc) · 8.72 KB
/
Copy pathscript.js
File metadata and controls
308 lines (260 loc) · 8.72 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* Siemens Hero JS
*
*
*/
var animation = {
animationTime: 2000,
animationDelay: 5000
};
var sliderElements = [
{
animation: {
delay: 5000
}
},
{
animation: {
delay: 5000
}
},
{
animation: {
delay: 5000
}
},
{
animation: {
delay: 5000
}
},
];
document.addEventListener("DOMContentLoaded", function (event) {
// get the hero element
var hero = document.getElementById("samm-hero-slider-interview");
// get the hero images
var slides = hero.getElementsByClassName("slide");
let _activeIndex = 0;
const activeIndex = {
get index() {
return _activeIndex;
},
set index(value) {
_activeIndex = value;
updatePillContainer();
}
};
const pills = hero.getElementsByClassName("pill");
function updatePillContainer() {
for (let i = 0; i < pills.length; i++) {
const pill = pills[i];
if (i === activeIndex.index) {
pill.classList.add("active");
} else {
pill.classList.remove("active");
}
}
}
Array.from(pills).forEach((pill, index) => {
pill.addEventListener("click", () => {
setAnimationToIndex(activeIndex.index, index);
activeIndex.index = index;
});
});
var numberOfSlides = slides.length; // the number of slides
function triggerAnimation() {
blockSwipe = true
setTimeout(() => {
blockSwipe = false
}, 1000)
var isMobile = window.innerWidth < 781;
if (isMobile) {
activeIndex.index = (activeIndex.index + 1) % numberOfSlides}
var prevPrevSlide = slides[(activeIndex.index - 2 + numberOfSlides) % numberOfSlides];
var prevSlide =
slides[(activeIndex.index - 1 + numberOfSlides) % numberOfSlides];
var currentSlide = slides[activeIndex.index];
var nextSlideOuter = slides[(activeIndex.index + 1) % numberOfSlides];
var nextSlideInner = nextSlideOuter.getElementsByTagName("div")[0];
var slideAfterNextSlideInner = slides[
(activeIndex.index + 2) % numberOfSlides
].getElementsByTagName("div")[0];
var slideAfterNextSlideOuter =
slides[(activeIndex.index + 2) % numberOfSlides];
prevPrevSlide.classList.remove("other-lower");
// set the z-index of the previous slide to 0
prevSlide.classList.remove("prev");
prevSlide.classList.add("other");
prevSlide.classList.add("other-lower");
// move the sliderAfterNextSlide to the start position
slideAfterNextSlideInner.classList.remove("transform-inner-end");
slideAfterNextSlideInner.classList.add("transform-inner-start");
slideAfterNextSlideOuter.classList.remove("transform-outer-end");
slideAfterNextSlideOuter.classList.add("transform-outer-start");
// set the zIndex of the current slide to 1
currentSlide.classList.remove("next");
currentSlide.classList.add("prev");
// set the zIndex of the next slide to 2
nextSlideOuter.classList.remove("other");
nextSlideOuter.classList.add("next");
// start the animation
nextSlideInner.classList.remove("transform-inner-start");
nextSlideInner.classList.add("transform-inner-end");
nextSlideOuter.classList.remove("transform-outer-start");
nextSlideOuter.classList.add("transform-outer-end");
// update the activeIndex
if (!isMobile) {
activeIndex.index = (activeIndex.index + 1) % numberOfSlides
}
}
function stopAnimations() {
for (let i = 0; i < slides.length; i++) {
const slide = slides[i];
const slideInner = slide.getElementsByTagName("div")[0];
slide.classList.remove("anim");
slideInner.classList.remove("anim");
}
}
function startAnimations() {
for (let i = 0; i < slides.length; i++) {
const slide = slides[i];
const slideInner = slide.getElementsByTagName("div")[0];
slide.classList.add("anim");
slideInner.classList.add("anim");
}
}
function setAnimationToIndex(oldIndex, newIndex, mobileTransition = false) {
stopInterval();
stopAnimations();
if (oldIndex === newIndex) {
startAnimations();
startInterval();
return;
}
// position newIndex slide
const newSlide = slides[(newIndex + numberOfSlides) % numberOfSlides];
const newSlideInner = newSlide.getElementsByTagName("div")[0];
if (!mobileTransition) {
Array.from(slides).forEach(slide => {
slide.querySelector('.big-slide').classList.add('no-transition');
slide.querySelector('.small-slide').classList.add('no-transition');
})
}
const beforeNewSlide = slides[(newIndex - 1 + numberOfSlides) % numberOfSlides];
beforeNewSlide.classList.add("other-lower");
// z-index
newSlide.classList.remove("other");
newSlide.classList.remove("prev");
newSlide.classList.add("next");
const oldNextSlide = slides[(oldIndex + 1) % numberOfSlides];
oldNextSlide.classList.remove("next");
oldNextSlide.classList.remove("prev");
oldNextSlide.classList.add("other");
newSlide.classList.remove("transform-outer-start");
newSlide.classList.add("transform-outer-end");
newSlideInner.classList.remove("transform-inner-start");
newSlideInner.classList.add("transform-inner-end");
const oldPrevSlide = slides[(oldIndex - 1 + numberOfSlides) % numberOfSlides];
oldPrevSlide.classList.remove("next");
oldPrevSlide.classList.remove("prev");
oldPrevSlide.classList.add("other");
// position oldIndex slide
const oldSlide = slides[oldIndex];
const oldSlideInner = oldSlide.getElementsByTagName("div")[0];
const beforeOldSlide = slides[(oldIndex - 1 + numberOfSlides) % numberOfSlides];
beforeOldSlide.classList.remove("other-lower");
oldSlide.classList.remove("prev");
oldSlide.classList.remove('next')
oldSlide.classList.add("other");
// move oldSlide to end position
oldSlide.classList.remove("transform-outer-start");
oldSlide.classList.add("transform-outer-end");
oldSlideInner.classList.remove("transform-inner-start");
oldSlideInner.classList.add("transform-inner-end");
const nextSlideOuter = slides[(newIndex + 1) % numberOfSlides];
const nextSlideInner = nextSlideOuter.getElementsByTagName("div")[0];
// move nextSlide to start position
nextSlideInner.classList.remove("transform-inner-end");
nextSlideInner.classList.add("transform-inner-start");
nextSlideOuter.classList.remove("transform-outer-end");
nextSlideOuter.classList.add("transform-outer-start");
// set newSlide to prev
newSlide.classList.add("prev");
newSlide.classList.remove("other");
newSlide.classList.remove("next");
// set nextSlide to next
nextSlideOuter.classList.add("next");
nextSlideOuter.classList.remove("other");
nextSlideOuter.classList.remove("prev");
// restart Animations
setTimeout(() => {
// turn on mobile transition
if (!mobileTransition) {
Array.from(slides).forEach(slide => {
slide.querySelector('.big-slide').classList.remove('no-transition');
slide.querySelector('.small-slide').classList.remove('no-transition');
})
}
startAnimations();
}, 500);
startInterval();
}
let isRunning = false;
let animationIntervalId = null;
function startInterval(delay) {
if (!isRunning) {
animationIntervalId = setInterval(
triggerAnimation,
delay || animation.animationDelay
);
isRunning = true;
}
}
function stopInterval() {
if (isRunning) {
clearInterval(animationIntervalId);
isRunning = false;
}
}
startInterval()
let blockSwipe = false
let startX, startY
hero.addEventListener("touchstart", (e) => {
stopInterval();
startX = e.touches[0].clientX
startY = e.touches[0].clientY
})
hero.addEventListener("touchend", (e) => {
const threshold = 50
const distX = e.changedTouches[0].clientX - startX
const distY = e.changedTouches[0].clientY - startY
if (Math.abs(distX) > Math.abs(distY) && Math.abs(distX) > threshold)
if (distX > 0) {
if (blockSwipe) return
blockSwipe = true
setAnimationToIndex(activeIndex.index, activeIndex.index - 1, true)
activeIndex.index = (activeIndex.index - 1 + numberOfSlides) % numberOfSlides
setTimeout(() => {
blockSwipe = false
}, 1000)
} else {
if (blockSwipe) return
blockSwipe = true
triggerAnimation()
setTimeout(() => {
blockSwipe = false
}, 1000)
}
startInterval()
})
const buttonStartStop = document.querySelector(".button-start-stop");
buttonStartStop.addEventListener("click", function () {
if (isRunning) {
stopInterval();
buttonStartStop.innerHTML = "Start";
} else {
startInterval();
buttonStartStop.innerHTML = "Stop";
}
})
});