-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteraction.js
228 lines (156 loc) · 5.33 KB
/
interaction.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
// global variables
var body = document.body,
sections = document.querySelectorAll("section"),
sectionThumbnails = document.querySelectorAll(".section-thumbnail"),
segments = body.querySelectorAll(".segment");
segmentLength = 20;
// convert nodelist to array
sections = toArray(sections);
sectionThumbnails = toArray(sectionThumbnails);
// animation default settings
var animationSettings = {
running: false,
easing: "easeInOutQuad",
duration: 750,
delay: 25,
elasticity: 10
}
sections.forEach(function(section){
// document fragment
var fragment = document.createDocumentFragment();
// get data-img value
var sectionImgURL = section.dataset.img;
// looping 20 times
for(var i = 0; i < segmentLength; i++){
var segment = document.createElement("div");
segment.className = "segment";
// calculate the segment inner background position
var posX = -i * 100/segmentLength;
segment.innerHTML = "<div class='segment-inner' style='background-image: url(" + sectionImgURL + "); left: " + posX + "vw'></div>";
// apply segment width by dividing 100 by 20
segment.style.width = 100/segmentLength + "%";
// append segment to section element
fragment.appendChild(segment);
section.appendChild(fragment);
}
});
// get shown section
var shownSection = body.querySelector(".show");
// if the key is clicked
body.addEventListener("keyup", function(e){
// if arrow down is clicked
if(e.keyCode === 40 && shownSection.nextElementSibling && animationSettings.running === false) {
animationSettings.running = true;
animateSegments(shownSection.nextElementSibling, false);
}
// if arrow right is clicked
if(e.keyCode === 39 && shownSection.nextElementSibling && animationSettings.running === false) {
animationSettings.running = true;
animateSegments(shownSection.nextElementSibling, false);
}
// if arrow up is clicked
if(e.keyCode === 38 && shownSection.previousElementSibling && animationSettings.running === false) {
animationSettings.running = true;
animateSegments(shownSection.previousElementSibling, false);
}
// if arrow left is clicked
if(e.keyCode === 37 && shownSection.previousElementSibling && animationSettings.running === false) {
animationSettings.running = true;
animateSegments(shownSection.previousElementSibling, false);
}
});
// if section thumbnail is clicked
sectionThumbnails.forEach(function(thumbnail){
thumbnail.addEventListener("click", function(){
if(animationSettings.running === false && !this.classList.contains("active")) {
animationSettings.running = true;
animateSegments(sections[sectionThumbnails.indexOf(this)], true);
}
})
});
// animate the section's segments
// first param is the next hidden section
// the second param defines whether the animation is requested by click event or not
function animateSegments(afterShownSection, byClickOrNot) {
var translateYValue, // transition direction
hiddenPosition; // hide at top or bottom
if(afterShownSection.className === "hide-bottom") {
translateYValue = "-100%";
hiddenPosition = "hide-top";
} else {
translateYValue = "100%";
hiddenPosition = "hide-bottom";
}
// get the index of after shown section
var afterShownSectionIndex = sections.indexOf(afterShownSection);
// shown section params
var shownSectionParams = {
// do function before animation starts
begin: function(){
// remove the active class from active thumbnails
sectionThumbnails[sections.indexOf(shownSection)].classList.remove("active");
},
targets: shownSection.querySelectorAll(".segment"),
complete: function(){
this.animatables.forEach(function(animatable){
animatable.target.style.transform = "translateY(0)";
});
// if the animate function is requested by click
if(byClickOrNot) {
sections.forEach(function(section, index){
// get all previous sections from the shown
if(index < afterShownSectionIndex) {
section.className = "hide-top";
}
// get all next sections from the shown
if(index > afterShownSectionIndex) {
section.className = "hide-bottom";
}
});
}
// if the animate function is requested by arrow key
else {
shownSection.className = hiddenPosition;
}
}
}
// after shown section params
var afterShownParams = {
begin: function(){
sectionThumbnails[sections.indexOf(afterShownSection)].classList.add("active");
},
targets: afterShownSection.querySelectorAll(".segment"),
complete: function(){
this.animatables.forEach(function(animatable){
animatable.target.style.transform = "translateY(0)";
});
afterShownSection.className = "show";
shownSection = afterShownSection;
animationSettings.running = false;
}
}
// animate the shown section
requestAnimate(shownSectionParams, translateYValue);
// animate the hidden section
requestAnimate(afterShownParams, translateYValue);
}
// utils
// request animate function
function requestAnimate(animationParams, translateYValue){
anime({
begin: animationParams.begin,
targets: animationParams.targets,
translateY: translateYValue,
duration: animationSettings.duration,
delay: function(el, index) {
return index * animationSettings.delay;
},
elasticity: animationSettings.elasticity,
complete: animationParams.complete
});
}
// convert nodelist to array
function toArray(nodeList){
nodeList = [].slice.call(nodeList);
return nodeList;
}