-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmain.js
More file actions
242 lines (209 loc) · 6.64 KB
/
main.js
File metadata and controls
242 lines (209 loc) · 6.64 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
/**
* main.js
* http://www.codrops.com
*
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright 2015, Codrops
* http://www.codrops.com
*/
;(function(window) {
'use strict';
var support = { transitions: Modernizr.csstransitions },
// transition end event name
transEndEventNames = { 'WebkitTransition': 'webkitTransitionEnd', 'MozTransition': 'transitionend', 'OTransition': 'oTransitionEnd', 'msTransition': 'MSTransitionEnd', 'transition': 'transitionend' },
transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
onEndTransition = function( el, callback ) {
var onEndCallbackFn = function( ev ) {
if( support.transitions ) {
if( ev.target != this ) return;
this.removeEventListener( transEndEventName, onEndCallbackFn );
}
if( callback && typeof callback === 'function' ) { callback.call(this); }
};
if( support.transitions ) {
el.addEventListener( transEndEventName, onEndCallbackFn );
}
else {
onEndCallbackFn();
}
},
// the pages wrapper
stack = document.querySelector('.pages-stack'),
// the page elements
pages = [].slice.call(stack.children),
// total number of page elements
pagesTotal = pages.length,
// index of current page
current = 0,
// menu button
menuCtrl = document.querySelector('button.menu-button'),
// the navigation wrapper
nav = document.querySelector('.pages-nav'),
// the menu nav items
navItems = [].slice.call(nav.querySelectorAll('.link--page')),
// check if menu is open
isMenuOpen = false;
function init() {
buildStack();
initEvents();
}
function buildStack() {
var stackPagesIdxs = getStackPagesIdxs();
// set z-index, opacity, initial transforms to pages and add class page--inactive to all except the current one
for(var i = 0; i < pagesTotal; ++i) {
var page = pages[i],
posIdx = stackPagesIdxs.indexOf(i);
if( current !== i ) {
classie.add(page, 'page--inactive');
if( posIdx !== -1 ) {
// visible pages in the stack
page.style.WebkitTransform = 'translate3d(0,100%,0)';
page.style.transform = 'translate3d(0,100%,0)';
}
else {
// invisible pages in the stack
page.style.WebkitTransform = 'translate3d(0,75%,-300px)';
page.style.transform = 'translate3d(0,75%,-300px)';
}
}
else {
classie.remove(page, 'page--inactive');
}
page.style.zIndex = i < current ? parseInt(current - i) : parseInt(pagesTotal + current - i);
if( posIdx !== -1 ) {
page.style.opacity = parseFloat(1 - 0.1 * posIdx);
}
else {
page.style.opacity = 0;
}
}
}
// event binding
function initEvents() {
// menu button click
menuCtrl.addEventListener('click', toggleMenu);
// navigation menu clicks
navItems.forEach(function(item) {
// which page to open?
var pageid = item.getAttribute('href').slice(1);
item.addEventListener('click', function(ev) {
ev.preventDefault();
openPage(pageid);
});
});
// clicking on a page when the menu is open triggers the menu to close again and open the clicked page
pages.forEach(function(page) {
var pageid = page.getAttribute('id');
page.addEventListener('click', function(ev) {
if( isMenuOpen ) {
ev.preventDefault();
openPage(pageid);
}
});
});
// keyboard navigation events
document.addEventListener( 'keydown', function( ev ) {
if( !isMenuOpen ) return;
var keyCode = ev.keyCode || ev.which;
if( keyCode === 27 ) {
closeMenu();
}
} );
}
// toggle menu fn
function toggleMenu() {
if( isMenuOpen ) {
closeMenu();
}
else {
openMenu();
isMenuOpen = true;
}
}
// opens the menu
function openMenu() {
// toggle the menu button
classie.add(menuCtrl, 'menu-button--open')
// stack gets the class "pages-stack--open" to add the transitions
classie.add(stack, 'pages-stack--open');
// reveal the menu
classie.add(nav, 'pages-nav--open');
// now set the page transforms
var stackPagesIdxs = getStackPagesIdxs();
for(var i = 0, len = stackPagesIdxs.length; i < len; ++i) {
var page = pages[stackPagesIdxs[i]];
page.style.WebkitTransform = 'translate3d(0, 75%, ' + parseInt(-1 * 200 - 50*i) + 'px)'; // -200px, -230px, -260px
page.style.transform = 'translate3d(0, 75%, ' + parseInt(-1 * 200 - 50*i) + 'px)';
}
}
// closes the menu
function closeMenu() {
// same as opening the current page again
openPage();
}
// opens a page
function openPage(id) {
var futurePage = id ? document.getElementById(id) : pages[current],
futureCurrent = pages.indexOf(futurePage),
stackPagesIdxs = getStackPagesIdxs(futureCurrent);
// set transforms for the new current page
futurePage.style.WebkitTransform = 'translate3d(0, 0, 0)';
futurePage.style.transform = 'translate3d(0, 0, 0)';
futurePage.style.opacity = 1;
// set transforms for the other items in the stack
for(var i = 0, len = stackPagesIdxs.length; i < len; ++i) {
var page = pages[stackPagesIdxs[i]];
page.style.WebkitTransform = 'translate3d(0,100%,0)';
page.style.transform = 'translate3d(0,100%,0)';
}
// set current
if( id ) {
current = futureCurrent;
}
// close menu..
classie.remove(menuCtrl, 'menu-button--open');
classie.remove(nav, 'pages-nav--open');
onEndTransition(futurePage, function() {
classie.remove(stack, 'pages-stack--open');
// reorganize stack
buildStack();
isMenuOpen = false;
});
}
// gets the current stack pages indexes. If any of them is the excludePage then this one is not part of the returned array
function getStackPagesIdxs(excludePageIdx) {
var nextStackPageIdx = current + 1 < pagesTotal ? current + 1 : 0,
nextStackPageIdx_2 = current + 2 < pagesTotal ? current + 2 : 1,
idxs = [],
excludeIdx = excludePageIdx || -1;
if( excludePageIdx != current ) {
idxs.push(current);
}
if( excludePageIdx != nextStackPageIdx ) {
idxs.push(nextStackPageIdx);
}
if( excludePageIdx != nextStackPageIdx_2 ) {
idxs.push(nextStackPageIdx_2);
}
return idxs;
}
init();
//changes to a page such as id="page-manuals" without opening the menu and preserving menu transition functionality
window.openPageNoTransition = function(id){
var futurePage = id ? document.getElementById(id) : pages[current],
futureCurrent = pages.indexOf(futurePage),
stackPagesIdxs = getStackPagesIdxs(futureCurrent);
// set transforms for the new current page
futurePage.style.WebkitTransform = 'translate3d(0, 0, 0)';
futurePage.style.transform = 'translate3d(0, 0, 0)';
futurePage.style.opacity = 1;
classie.remove(futurePage, 'page--inactive');
// set current
if( id ) {
current = futureCurrent;
}
buildStack();
}
})(window);