-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
227 lines (208 loc) · 7.52 KB
/
scripts.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
//Right Arrow & Left Arrow
let rightArrow = document.querySelector("#carousel-1 .right-arrow");
let leftArrow = document.querySelector("#carousel-1 .left-arrow");
//List of all of the screens in carousel
let screenStore = document.querySelectorAll("#carousel-1 .carousel-screen");
let numOfScreens = screenStore.length;
//List of all the circle stores
let circleStore = document.querySelectorAll("#carousel-1 .circle-container .circle");
//number to target main screen
let currentScreen = 0;
//currently in animation or not
let inAnim = false;
//Animation Time
let animTime = 500;
//Sort out starting position
sortPositioning(screenStore[currentScreen], screenStore[currentScreen - 1], screenStore[currentScreen + 1]);
//Sort out circle styling
highlightCircle(circleStore[0]);
//User clicks on rightArrow
rightArrow.addEventListener("click", () => {
startAnim("right");
});
//User clicks on the leftArrow
leftArrow.addEventListener("click", () => {
startAnim("left");
});
//Start animation. Either towards left or right
function startAnim(direction) {
if(!inAnim) {
inAnim = true;
if(direction === "right") {
moveRight();
highlightCircle(circleStore[currentScreen + 1], "right");
}else if(direction === "left"){
moveLeft();
highlightCircle(circleStore[currentScreen - 1], "left");
}else{
isAnim = false;
return
}
}
}
//Move to the right
function moveRight() {
//Move towards Next screen as usual
if(currentScreen < numOfScreens - 1){
toLeft(screenStore[currentScreen]);
comeRight(screenStore[currentScreen + 1]);
setTimeout(() => {
inAnim = false;
currentScreen++;
sortPositioning(screenStore[currentScreen], screenStore[currentScreen - 1], screenStore[currentScreen + 1]);
}, animTime)
}else{
//Or the screen coming in is the first screen again
toLeft(screenStore[currentScreen]);
comeRight(screenStore[0]);
setTimeout(() => {
inAnim = false;
currentScreen = 0;
sortPositioning(screenStore[currentScreen], screenStore[currentScreen - 1], screenStore[currentScreen + 1]);
}, animTime)
}
}
//Move to the left
function moveLeft() {
//Move back to screen previously on, as usual
if(currentScreen > 0){
toRight(screenStore[currentScreen]);
comeLeft(screenStore[currentScreen - 1]);
setTimeout(() => {
inAnim = false;
currentScreen--;
sortPositioning(screenStore[currentScreen], screenStore[currentScreen - 1], screenStore[currentScreen + 1]);
}, animTime)
}else{
//Move back to the last screen container
toRight(screenStore[currentScreen]);
comeLeft(screenStore[numOfScreens - 1]);
setTimeout(() => {
inAnim = false;
currentScreen = numOfScreens - 1;
sortPositioning(screenStore[currentScreen], screenStore[currentScreen - 1], screenStore[currentScreen + 1]);
}, animTime)
}
}
//User clicks on one of the circles
circleStore.forEach(circle => {
circle.addEventListener("click", event => {
if(!inAnim){
//Convert NodeList to Array, to use 'indexOf' method.
let circleStoreArray = Array.prototype.slice.call(circleStore);
let circleIndex = circleStoreArray.indexOf(event.target);
//Configure circle styling
highlightCircle(event.target);
//Work out whether we need to move right or left, or nowhere.
if(circleIndex > currentScreen){
changeScreenCircleClick(circleIndex, "right");
}else if (circleIndex < currentScreen){
changeScreenCircleClick(circleIndex, "left");
}
}
})
})
function changeScreenCircleClick(circleIndex, direction) {
inAnim = true;
if(direction === "right"){
sortPositioning(screenStore[currentScreen], screenStore[currentScreen - 1], screenStore[circleIndex]);
toLeft(screenStore[currentScreen]);
comeRight(screenStore[circleIndex]);
}else if (direction === "left"){
sortPositioning(screenStore[currentScreen], screenStore[circleIndex], screenStore[currentScreen + 1]);
toRight(screenStore[currentScreen]);
comeLeft(screenStore[circleIndex]);
}else{
inAnim = false;
return
}
setTimeout(() => {
inAnim = false;
currentScreen = circleIndex;
sortPositioning(screenStore[currentScreen], screenStore[currentScreen - 1], screenStore[currentScreen + 1]);
}, animTime)
}
function highlightCircle(circleSelect, direction) {
if(circleSelect === undefined && direction === "right"){
circleSelect = circleStore[0];
}else if (circleSelect === undefined && direction === "left"){
circleSelect = circleStore[numOfScreens - 1];
}
circleStore.forEach(circle => {
if(circle === circleSelect){
circle.classList.add("circle-fill");
}else{
circle.classList.remove("circle-fill");
}
})
}
//Animation methods
function toLeft(screen) {
screen.style.animation = "toLeft 0.5s ease-in-out forwards";
setTimeout(() => {
screen.style.animation = "";
}, animTime);
}
function toRight(screen) {
screen.style.animation = "toRight 0.5s ease-in-out forwards";
setTimeout(() => {
screen.style.animation = "";
}, animTime);
}
function comeRight(screen) {
screen.style.animation = "comeRight 0.5s ease-in-out forwards";
setTimeout(() => {
screen.style.animation = "";
}, animTime);
}
function comeLeft(screen) {
screen.style.animation = "comeLeft 0.5s ease-in-out forwards";
setTimeout(() => {
screen.style.animation = "";
}, animTime);
}
//Sort positioning. Don't want images to overlap
function sortPositioning(mainScreen, leftScreen, rightScreen) {
//If right screen is undefined. We need to repeat first screen again
if(rightScreen === undefined){
rightScreen = screenStore[0];
}
//If left screen is undefined. We use the last screen
if(leftScreen === undefined){
leftScreen = screenStore[numOfScreens - 1];
}
screenStore.forEach(screen => {
if(screen === mainScreen){
screen.style.display = "block";
screen.style.left = "0px";
}else if (screen === leftScreen){
screen.style.display = "block";
screen.style.left = "-100%";
}else if (screen === rightScreen){
screen.style.display = "block";
screen.style.left = "100%";
}else{
screen.style.display = "none";
}
})
}
//Auto Scroll feature
let carousel = document.getElementById("carousel-1");
let scrollTime = Number(carousel.getAttribute("auto-scroll"));
//Only implement the feature if the user has included the attribute 'auto-scroll'.
if(scrollTime) {
//Auto Scroll will be set up the very first time
let autoWipe = setInterval(() => {
startAnim("right");
}, scrollTime);
//Clear the timer when they hover on carousel
carousel.addEventListener("mouseenter", () => {
clearInterval(autoWipe);
});
//Re-initialise the timer when they hover out of the carousel
carousel.addEventListener("mouseleave", () => {
autoWipe = setInterval(() => {
startAnim("right");
}, scrollTime);
})
}