-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (127 loc) · 4.56 KB
/
index.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
const app = (function() {
const sliderItems = [
"https://i.imgur.com/ZZX0bVz.jpg",
"https://i.imgur.com/1nT9vem.jpg",
"https://i.imgur.com/L9BiiXr.jpg",
"https://i.imgur.com/3xAR3LR.jpg",
"https://i.imgur.com/P8hVhjC.jpg"
];
let prev = sliderItems.length-1;
let next = 1;
const sliderTransitionDuration = 0.4;
let sliderEl = document.querySelector('.slider');
let prevEl = document.querySelector('.prev');
let currentEl = document.querySelector('.current');
let nextEl = document.querySelector('.next');
const calcTotalSliderWidth = () => Number(window.getComputedStyle(sliderEl).width.match(/\d+/)[0]);
const calcSlideWidth = (totalSliderWidth) => totalSliderWidth / 3;
let totalSliderWidth = calcTotalSliderWidth();
let slideWidth = calcSlideWidth(totalSliderWidth);
// Initial render
updateSliderOffset('current', sliderEl); // Show .current
setTimeout(() => sliderEl.classList.remove('no-transition'), 30);
sliderEl.style.transition = `all ${sliderTransitionDuration}s`;
prevEl.querySelector('img').src = sliderItems[prev];
currentEl.querySelector('img').src = sliderItems[0];
nextEl.querySelector('img').src = sliderItems[next];
/**
* Updates `prev` and `next` in the outer scope with the wrapping indices for `sliderItems`
* @param {boolean=} decrement - whether to decrement or increment the indices
* @return {Array<number>} [prev, next] updated indices
*/
function calcWrappingIndices(decrement = false) {
return [prev, next] = [prev, next].map(n => {
decrement ? n-- : n++;
if (n < 0) {
n = sliderItems.length + n;
} else if (n > sliderItems.length-1) {
n = n - sliderItems.length;
}
return n;
});
}
let onClickPrevNextRunning = false;
/**
* @param {boolean=} goToPrev - whether to go to previous slider item or next
* @return {undefined}
*/
function onClickPrevNext(goToPrev = false) {
if (onClickPrevNextRunning) return;
onClickPrevNextRunning = true;
// Perform transform on slider
updateSliderOffset(goToPrev ? 'prev' : 'next');
setTimeout(() => {
// Update nodes adjacent to current
sliderEl.classList.add('no-transition');
const nodeToMove = goToPrev ? nextEl : prevEl;
if (goToPrev) {
sliderEl.prepend(nodeToMove);
[currentEl, nextEl, prevEl] = [prevEl, currentEl, nextEl];
} else {
sliderEl.append(nodeToMove);
[nextEl, prevEl, currentEl] = [prevEl, currentEl, nextEl];
}
updateSliderOffset('current'); // Show .current
[prev, next] = goToPrev ? calcWrappingIndices(decrement = true) : calcWrappingIndices();
// Update item for recycled node
nodeToMove.querySelector('img').src = sliderItems[goToPrev ? prev : next];
[prevEl, currentEl, nextEl].forEach((e, i) => {
let newClass, oldClass;
switch (i) {
case 0:
newClass = 'prev';
oldClass = goToPrev ? 'next' : 'current';
break;
case 1:
newClass = 'current';
oldClass = goToPrev ? 'prev' : 'next';
break;
case 2:
newClass = 'next';
oldClass = goToPrev ? 'current' : 'prev';
break;
}
e.className = e.className.replace(oldClass, newClass);
e.setAttribute('aria-hidden', newClass === 'current' ? 'false' : 'true');
});
setTimeout(() => {
sliderEl.classList.remove('no-transition');
onClickPrevNextRunning = false;
}, 30);
}, sliderTransitionDuration * 1000); // Convert from seconds to milliseconds
}
/**
* @param {number} offset - the translateX offset to apply
* @param {Element=} el - the element for `.slider`, otherwise it will be selected
* @return {undefined}
*/
function updateSliderOffset(target) {
totalSliderWidth = calcTotalSliderWidth();
slideWidth = calcSlideWidth(totalSliderWidth);
let offset;
switch (target) {
case 'prev':
offset = 0;
break
case 'current':
offset = slideWidth;
break
case 'next':
offset = totalSliderWidth - slideWidth;
break
}
sliderEl.style.transform = `translateX(-${offset}px)`;
}
/**
* Update the slider offset upon resizing the window. Deactivates transitions during this.
* @return {undefined}
*/
window.onresize = () => {
sliderEl.classList.add('no-transition');
updateSliderOffset('current', sliderEl);
setTimeout(() => sliderEl.classList.remove('no-transition'), 30);
};
return {
onClickPrevNext: onClickPrevNext
};
})();