-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
272 lines (236 loc) · 6.4 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Onepage
*
* A component inspired by Apple iPhone page.
*
* Copyright (c) 2013 - 2014 by Hsiaoming Yang.
*/
var events = require('event');
var emitter = require('emitter');
/**
* Interface of onepage.
*/
function Onepage(element, options) {
if (!(this instanceof Onepage)) {
return new Onepage(element, options);
}
options = options || {};
merge(options, {
duration: 800,
timingFunction: 'ease',
period: 300,
wheelDelta: 100,
pagination: true,
keyboard: true,
loop: 'down'
});
var children = element.childNodes;
var pages = [];
var pagination;
if (options.pagination) {
// create pagination.
pagination = document.createElement('div');
pagination.className = 'onepage-pagination';
}
for (var i = 0; i < children.length; i++) {
(function(node) {
if (node.nodeType === document.ELEMENT_NODE) {
node.className += ' onepage-element';
node.style.top = pages.length * 100 + '%';
if (pagination) {
var page = document.createElement('a');
var pageId = node.id || pages.length;
page.href = '#' + pageId;
page.setAttribute('data-page', pages.length);
page.id = 'onepage-pagination-' + pageId;
// pagination with title
if (node.title) {
var explain = document.createElement('span');
explain.className = 'text-title';
explain.innerHTML = node.title;
page.appendChild(explain);
// clear node title
node.title = '';
}
pagination.appendChild(page);
}
pages.push(node);
}
})(children[i]);
}
stylish(element, 'transitionTimingFunction', options.timingFunction);
var prefix = stylish(
element, 'transitionDuration', options.duration + 'ms'
);
element.className += ' onepage-container';
// current active page
this.page = 0;
// last animation time
this.transitioned = null;
this.options = options;
this.element = element;
this.pages = pages;
this.pagination = pagination;
setup(this);
var hashvalue = location.hash.slice(1);
var el = document.getElementById('onepage-pagination-' + hashvalue);
if (el) {
this.page = parseInt(el.getAttribute('data-page'), 10) || 0;
}
this.move(this.page);
}
emitter(Onepage.prototype);
Onepage.prototype.pageUp = function() {
var me = this;
var loop = me.options.loop;
if (me.page > 0) {
me.move(me.page - 1);
} else if (loop === 'up' || loop === 'both') {
me.move(me.pages.length - 1);
}
};
Onepage.prototype.pageDown = function() {
var me = this;
var loop = me.options.loop;
if (me.page < me.pages.length - 1) {
me.move(me.page + 1);
} else if (loop === 'down' || loop === 'both') {
me.move(0);
}
};
/**
* Change current page to the given page.
*/
Onepage.prototype.move = function(page) {
var me = this;
// reset page value for safety
if (page < 0) {
page = 0;
}
if (page > me.pages.length - 1) {
page = me.pages.length - 1;
}
var pagination = me.pagination;
if (pagination) {
// clear pagination active before
var item = pagination.childNodes[me.page];
item.className = '';
// activate current pagination
item = pagination.childNodes[page];
item.className = 'active';
}
stylish(
me.element, 'transform', 'translate3d(0,-' + page * 100 + '%,0)'
);
// emit events
me.emit('move', page);
setTimeout(function() {
me.emit('end', page);
}, me.options.duration);
// update status
me.page = page;
me.transitioned = new Date().getTime();
me.element.setAttribute('data-index', page);
};
/**
* Setup everything for onepage scrolling.
*/
function setup(me) {
// binding mousewheel
var mousewheel = function(e) {
e.preventDefault();
var delta = new Date().getTime() - (me.transitioned || 0);
var period = me.options.period + me.options.duration;
if (delta > period && Math.abs(e.wheelDelta) > me.options.wheelDelta) {
if (e.wheelDelta > 0) {
me.pageUp();
} else {
me.pageDown();
}
}
};
events.bind(me.element, 'mousewheel', mousewheel);
// firefox has no mousewheel event
events.bind(me.element, 'DOMMouseScroll', mousewheel);
// binding touch event
events.bind(me.element, 'touchstart', function(e) {
var x, y;
var touches = e.touches;
if (touches && touches.length) {
x = touches[0].pageX;
y = touches[0].pageY;
var touchmove = function(e) {
e.preventDefault();
if (e.touches && e.touches.length) {
var deltaX = x - e.touches[0].pageX;
var deltaY = y - e.touches[0].pageY;
if (deltaY >= 50) {
me.pageDown();
} else if (deltaY <= -50) {
me.pageUp();
}
if (Math.abs(deltaX) >= 50 || Math.abs(deltaY) >= 50) {
events.unbind(me.element, 'touchmove', touchmove);
}
}
};
events.bind(me.element, 'touchmove', touchmove);
}
}, false);
// binding up and down key
if (me.options.keyboard) {
events.bind(document, 'keydown', function(e) {
if (e.keyCode === 38) {
me.pageUp();
} else if (e.keyCode === 40) {
me.pageDown();
}
});
}
// setup pagination
var pagination = me.pagination;
if (pagination) {
var pages = pagination.getElementsByTagName('a');
for (var i = 0; i < pages.length; i++) {
(function(i) {
events.bind(pages[i], 'click', function(e) {
e.preventDefault();
me.move(i);
});
})(i);
}
document.body.appendChild(pagination);
// pagination be the vertical middle
pagination.style.marginTop = '-' + (pagination.clientHeight / 2) + 'px';
}
}
/**
* Style an element with respect for browser prefixes.
*/
function stylish(node, key, value) {
var prefixes = ['webkit', 'moz', 'ms', 'o'];
var altKey = key.charAt(0).toUpperCase() + key.slice(1);
var browser = null;
for (var i = 0; i < prefixes.length; i++) {
(function(prefix) {
if (node.style[prefix + altKey] !== undefined) {
node.style[prefix + altKey] = value;
browser = prefix;
}
})(prefixes[i]);
}
if (!browser) {
node.style[key] = value;
}
return browser;
}
function merge(obj, params) {
for (var key in params) {
(function(key) {
if (obj[key] === undefined) {
obj[key] = params[key];
}
})(key);
}
}
module.exports = Onepage;