-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
245 lines (213 loc) · 7.96 KB
/
app.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
242
243
244
245
// set up barba
document.addEventListener("DOMContentLoaded", function() {
// assign some variables
var lastElementClicked;
var isAnimating = false;
var $body = document.querySelector('body');
var $html = document.querySelector('html');
// options
Barba.Pjax.Dom.wrapperId = 'content-wrapper';
Barba.Pjax.Dom.containerClass = 'content-container';
// ---------------- //
// VIEWS
// ---------------- //
var homeView = Barba.BaseView.extend({
namespace: 'home',
onLeave: function() {
$body.style.overflow = 'hidden';
},
onLeaveCompleted: function() {
$body.style.overflow = 'initial';
}
});
var detailView = Barba.BaseView.extend({
namespace: 'detail',
onEnterCompleted: function() {
// The Transition has just finished
// can now use functions/events specifically for this page
// -- vars
var scrollBtn = document.querySelector('.scrolldown');
// -- events
scrollBtn.addEventListener('click', scrollToContent);
// -- functions
function scrollToContent(e) {
e.preventDefault();
TweenLite.to(window, 0.5, {
scrollTo: {
ease: Power3.easeOut,
y: '.maincontent'
}
});
};
// call the start function below when we load this page
this.start();
},
onLeave: function() {
// A new Transition toward a new page has just started.
$body.classList.remove('loaded');
$html.style.overflow = 'hidden';
},
onLeaveCompleted: function() {
$html.style.overflow = 'initial';
if(typeof(Storage) !== 'undefined') {
// See if there is a scroll pos and go there.
var lastYPos = +localStorage.getItem('scrollYPos');
if (lastYPos) {
window.scrollTo(0, lastYPos);
}
}
},
start: function() {
var tl = new TimelineMax({
onComplete: function() {
$body.classList.add('loaded');
isAnimating = false;
}
});
}
});
// Don't forget to init the view!
homeView.init();
detailView.init();
Barba.Pjax.init();
Barba.Prefetch.init();
// listen to the event on click
// can now reference lastElementClicked to scroll to where it's been clicked
Barba.Dispatcher.on('linkClicked', function(el) {
lastElementClicked = el;
});
// -------------------- //
// TRANSITION FUNCTIONS
// -------------------- //
var revealProject = Barba.BaseTransition.extend({
start: function() {
isAnimating = true;
// set up functions asynchronously
Promise
.all([this.newContainerLoading, this.scrollToProject()])
.then(this.showNewPage.bind(this));
},
// first transition function
scrollToProject: function() {
var project = $(lastElementClicked).parents('.project');
var deferred = Barba.Utils.deferred();
console.log(project);
TweenLite.to(window, 0.5, {
scrollTo: {
y: project
},
onComplete: function() {
deferred.resolve();
if(typeof(Storage) !== 'undefined') {
// See if there is a scroll pos and go there.
var lastYPos = +localStorage.getItem('scrollYPos');
localStorage.setItem('scrollYPos', window.scrollY);
}
}
});
return deferred.promise;
},
// transition to new page / object
showNewPage: function() {
// assign objects that are transitioning
var _this = this;
var newImage = _this.newContainer.querySelector('img');
var scrollArrow = _this.newContainer.querySelector('.scrolldown');
var newText = _this.newContainer.querySelector('summary');
var newTextLink = _this.newContainer.querySelector('.link');
// reset and create a new timeline
var tl = new TimelineMax({
onComplete: function() {
_this.newContainer.style.position = 'static';
_this.done();
// once timeline is finished, reset window to top
// to avoid jumping
window.scroll(0, 0);
isAnimating = false;
}
});
// preset transitional objects
TweenLite.set(_this.newContainer, {
position: 'fixed',
visibility: 'visible',
top: 0,
right: 0,
bottom: 0,
left: 0,
opacity: 0,
zIndex: 10
});
// start transitions
tl.to(_this.newContainer, 0.3, {opacity: 1 });
tl.to(newImage, 0.3, { opacity: 0 });
tl.to(newTextLink, 0.3, { opacity: 0 }, '-=0.3');
tl.to(newText, 0.7, { ease: Expo.easeOut, y: 30 });
tl.to(scrollArrow, 0.5, {
ease: Back.easeOut,
opacity: 1,
y: 0
}, '-=0.7');
}
});
var closeProject = Barba.BaseTransition.extend({
start: function() {
isAnimating = true;
Promise
// Promise Async, do this, then this before load --- our animation
// functions in the .all will run first
.all([this.newContainerLoading, this.scrollTop()])
.then(this.hideNewPage.bind(this));
},
scrollTop: function() {
var deferred = Barba.Utils.deferred();
var obj = { y: window.pageYOffset };
TweenLite.to(obj, 0.4, {
y: 0,
onUpdate: function() {
if (obj.y === 0) {
deferred.resolve();
}
window.scroll(0, obj.y);
},
onComplete: function() {
deferred.resolve();
}
});
return deferred.promise;
},
// transition out new page / object
hideNewPage: function() {
// assign objects that are transitioning
var _this = this;
var oldText = _this.oldContainer.querySelector('summary');
var oldscrollArrow = _this.oldContainer.querySelector('.scrolldown');
var oldImage = _this.oldContainer.querySelector('img');
var oldTextLink = _this.oldContainer.querySelector('.link');
// reset and create a new timeline
var tl = new TimelineMax({
onComplete: function() {
_this.newContainer.style.position = 'static';
_this.done();
isAnimating = false;
}
});
// start transitions
tl.to(oldImage, 0.3, { opacity: 1 });
tl.to(oldscrollArrow, 0.3, { opacity: 0 });
tl.to(oldTextLink, 0.3, { opacity: 1 }, '-=0.3');
tl.to(oldText, 0.7, { ease: Expo.easeOut, y: -113 });
tl.to(_this.newContainer, 0.3, { opacity: 1 });
}
});
// -------------------- //
// SET TRANSITIONS
// -------------------- //
Barba.Pjax.getTransition = function() {
var transitionPage = revealProject;
// if a page has a namespace of 'detail' use the following transition
if (Barba.HistoryManager.prevStatus().namespace === 'detail') {
transitionPage = closeProject;
}
return transitionPage;
};
});