Elffuss BraidHub

kikocis/iscroll public

ad59fa5f830d · 13.7 KiBRawPermalink
1
2function IScroll (el, options) {
3 this.wrapper = typeof el == 'string' ? document.querySelector(el) : el;
4 this.scroller = this.wrapper.children[0];
5 this.scrollerStyle = this.scroller.style; // cache style for better performance
6
7 this.options = {
8
9// INSERT POINT: OPTIONS
10
11 startX: 0,
12 startY: 0,
13 scrollY: true,
14 directionLockThreshold: 5,
15 momentum: true,
16
17 bounce: true,
18 bounceTime: 600,
19 bounceEasing: '',
20
21 preventDefault: true,
22
23 HWCompositing: true,
24 useTransition: true,
25 useTransform: true
26 };
27
28 for ( var i in options ) {
29 this.options[i] = options[i];
30 }
31
32 // Normalize options
33 this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
34
35 this.options.useTransition = utils.hasTransition && this.options.useTransition;
36 this.options.useTransform = utils.hasTransform && this.options.useTransform;
37
38 this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
39 this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
40
41 // If you want eventPassthrough I have to lock one of the axes
42 this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
43 this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
44
45 // With eventPassthrough we also need lockDirection mechanism
46 this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
47 this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
48
49 this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
50
51 if ( this.options.tap === true ) {
52 this.options.tap = 'tap';
53 }
54
55// INSERT POINT: NORMALIZATION
56
57 // Some defaults
58 this.x = 0;
59 this.y = 0;
60 this._events = {};
61
62// INSERT POINT: DEFAULTS
63
64 this._init();
65 this.refresh();
66
67 this.scrollTo(this.options.startX, this.options.startY);
68 this.enable();
69}
70
71IScroll.prototype = {
72 version: '/* VERSION */',
73
74 _init: function () {
75 this._initEvents();
76
77// INSERT POINT: _init
78
79 },
80
81 destroy: function () {
82 this._initEvents(true);
83
84 this._execEvent('destroy');
85 },
86
87 _transitionEnd: function (e) {
88 if ( e.target != this.scroller ) {
89 return;
90 }
91
92 this._transitionTime(0);
93 if ( !this.resetPosition(this.options.bounceTime) ) {
94 this._execEvent('scrollEnd');
95 }
96 },
97
98 _start: function (e) {
99 if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
100 return;
101 }
102
103 if ( this.options.preventDefault && !utils.isAndroidBrowser ) {
104 e.preventDefault(); // This seems to break default Android browser
105 }
106
107 var point = e.touches ? e.touches[0] : e,
108 pos;
109
110 this.initiated = utils.eventType[e.type];
111 this.moved = false;
112 this.distX = 0;
113 this.distY = 0;
114 this.directionX = 0;
115 this.directionY = 0;
116 this.directionLocked = 0;
117
118 this._transitionTime();
119
120 this.isAnimating = false;
121 this.startTime = utils.getTime();
122
123 if ( this.options.useTransition && this.isInTransition ) {
124 pos = this.getComputedPosition();
125
126 this._translate(Math.round(pos.x), Math.round(pos.y));
127 this.isInTransition = false;
128 }
129
130 this.startX = this.x;
131 this.startY = this.y;
132 this.absStartX = this.x;
133 this.absStartY = this.y;
134 this.pointX = point.pageX;
135 this.pointY = point.pageY;
136
137 this._execEvent('scrollStart');
138 },
139
140 _move: function (e) {
141 if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
142 return;
143 }
144
145 if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
146 e.preventDefault();
147 }
148
149 var point = e.touches ? e.touches[0] : e,
150 deltaX = point.pageX - this.pointX,
151 deltaY = point.pageY - this.pointY,
152 timestamp = utils.getTime(),
153 newX, newY,
154 absDistX, absDistY;
155
156 this.pointX = point.pageX;
157 this.pointY = point.pageY;
158
159 this.distX += deltaX;
160 this.distY += deltaY;
161 absDistX = Math.abs(this.distX);
162 absDistY = Math.abs(this.distY);
163
164 // We need to move at least 10 pixels for the scrolling to initiate
165 if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
166 return;
167 }
168
169 // If you are scrolling in one direction lock the other
170 if ( !this.directionLocked && !this.options.freeScroll ) {
171 if ( absDistX > absDistY + this.options.directionLockThreshold ) {
172 this.directionLocked = 'h'; // lock horizontally
173 } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
174 this.directionLocked = 'v'; // lock vertically
175 } else {
176 this.directionLocked = 'n'; // no lock
177 }
178 }
179
180 if ( this.directionLocked == 'h' ) {
181 if ( this.options.eventPassthrough == 'vertical' ) {
182 e.preventDefault();
183 } else if ( this.options.eventPassthrough == 'horizontal' ) {
184 this.initiated = false;
185 return;
186 }
187
188 deltaY = 0;
189 } else if ( this.directionLocked == 'v' ) {
190 if ( this.options.eventPassthrough == 'horizontal' ) {
191 e.preventDefault();
192 } else if ( this.options.eventPassthrough == 'vertical' ) {
193 this.initiated = false;
194 return;
195 }
196
197 deltaX = 0;
198 }
199
200 newX = this.x + (this.hasHorizontalScroll ? deltaX : 0);
201 newY = this.y + (this.hasVerticalScroll ? deltaY : 0);
202
203 // Slow down if outside of the boundaries
204 if ( newX > 0 || newX < this.maxScrollX ) {
205 newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
206 }
207 if ( newY > 0 || newY < this.maxScrollY ) {
208 newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
209 }
210
211 this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
212 this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
213
214 this.moved = true;
215
216 this._translate(newX, newY);
217
218/* REPLACE START: _move */
219
220 if ( timestamp - this.startTime > 300 ) {
221 this.startTime = timestamp;
222 this.startX = this.x;
223 this.startY = this.y;
224 }
225
226/* REPLACE END: _move */
227
228 },
229
230 _end: function (e) {
231 if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
232 return;
233 }
234
235 if ( this.options.preventDefault ) {
236 e.preventDefault(); // TODO: check if needed
237 }
238
239 var point = e.changedTouches ? e.changedTouches[0] : e,
240 momentumX,
241 momentumY,
242 duration = utils.getTime() - this.startTime,
243 newX = Math.round(this.x),
244 newY = Math.round(this.y),
245 time = 0,
246 easing = '';
247
248 this.scrollTo(newX, newY); // ensures that the last position is rounded
249
250 this.isInTransition = 0;
251 this.initiated = 0;
252 this.endTime = utils.getTime();
253
254 // reset if we are outside of the boundaries
255 if ( this.resetPosition(this.options.bounceTime) ) {
256 return;
257 }
258
259 // we scrolled less than 10 pixels
260 if ( !this.moved ) {
261 if ( this.options.tap ) {
262 utils.tap(e, this.options.tap);
263 }
264
265 if ( this.options.click ) {
266 utils.click(e);
267 }
268
269 return;
270 }
271
272 // start momentum animation if needed
273 if ( this.options.momentum && duration < 300 ) {
274 momentumX = this.hasHorizontalScroll ? utils.momentum(this.x, this.startX, duration, this.maxScrollX, this.options.bounce ? this.wrapperWidth : 0) : { destination: newX, duration: 0 };
275 momentumY = this.hasVerticalScroll ? utils.momentum(this.y, this.startY, duration, this.maxScrollY, this.options.bounce ? this.wrapperHeight : 0) : { destination: newY, duration: 0 };
276 newX = momentumX.destination;
277 newY = momentumY.destination;
278 time = Math.max(momentumX.duration, momentumY.duration);
279 this.isInTransition = 1;
280 }
281
282 if ( this.options.snap ) {
283 var snap = this._nearestSnap(newX, newY);
284 this.currentPage = snap;
285 newX = snap.x;
286 newY = snap.y;
287 time = this.options.snapSpeed || Math.max(
288 Math.max(
289 Math.min(Math.abs(newX - this.x), 1000),
290 Math.min(Math.abs(newY - this.y), 1000)
291 ),
292 300);
293
294 easing = this.options.bounceEasing;
295 }
296
297 if ( newX != this.x || newY != this.y ) {
298 // change easing function when scroller goes out of the boundaries
299 if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
300 easing = utils.ease.quadratic;
301 }
302
303 this.scrollTo(newX, newY, time, easing);
304 return;
305 }
306
307 this._execEvent('scrollEnd');
308 },
309
310 _resize: function () {
311 var that = this;
312
313 clearTimeout(this.resizeTimeout);
314
315 this.resizeTimeout = setTimeout(function () {
316 that.refresh();
317 that.resetPosition();
318 }, 60);
319 },
320
321 resetPosition: function (time) {
322 if ( this.x <= 0 && this.x >= this.maxScrollX && this.y <= 0 && this.y >= this.maxScrollY ) {
323 return false;
324 }
325
326 var x = this.x,
327 y = this.y;
328
329 time = time || 0;
330
331 if ( !this.hasHorizontalScroll || this.x > 0 ) {
332 x = 0;
333 } else if ( this.x < this.maxScrollX ) {
334 x = this.maxScrollX;
335 }
336
337 if ( !this.hasVerticalScroll || this.y > 0 ) {
338 y = 0;
339 } else if ( this.y < this.maxScrollY ) {
340 y = this.maxScrollY;
341 }
342
343 if ( this.options.snap ) {
344 var snap = this._nearestSnap(x, y);
345 this.currentPage = snap;
346 x = snap.x;
347 y = snap.y;
348 }
349
350 this.scrollTo(x, y, time, this.options.bounceEasing);
351
352 return true;
353 },
354
355 disable: function () {
356 this.enabled = false;
357 },
358
359 enable: function () {
360 this.enabled = true;
361 },
362
363 refresh: function () {
364 var rf = this.wrapper.offsetHeight; // Force refresh
365
366 this.wrapperWidth = this.wrapper.clientWidth;
367 this.wrapperHeight = this.wrapper.clientHeight;
368
369/* REPLACE START: refresh */
370
371 this.scrollerWidth = this.scroller.offsetWidth;
372 this.scrollerHeight = this.scroller.offsetHeight;
373
374/* REPLACE END: refresh */
375
376 this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
377 this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
378
379 if ( this.maxScrollX > 0 ) {
380 this.maxScrollX = 0;
381 }
382
383 if ( this.maxScrollY > 0 ) {
384 this.maxScrollY = 0;
385 }
386
387 this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
388 this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
389
390 this.endTime = 0;
391
392 this.wrapperOffset = utils.offset(this.wrapper);
393
394 this._execEvent('refresh');
395 },
396
397 on: function (type, fn) {
398 if ( !this._events[type] ) {
399 this._events[type] = [];
400 }
401
402 this._events[type].push(fn);
403 },
404
405 _execEvent: function (type) {
406 if ( !this._events[type] ) {
407 return;
408 }
409
410 var i = 0,
411 l = this._events[type].length;
412
413 if ( !l ) {
414 return;
415 }
416
417 for ( ; i < l; i++ ) {
418 this._events[type][i].call(this);
419 }
420 },
421
422 scrollBy: function (x, y, time, easing) {
423 x = this.x + x;
424 y = this.y + y;
425 time = time || 0;
426
427 this.scrollTo(x, y, time, easing);
428 },
429
430 scrollTo: function (x, y, time, easing) {
431 easing = easing || utils.ease.circular;
432
433 if ( !time || (this.options.useTransition && easing.style) ) {
434 this._transitionTimingFunction(easing.style);
435 this._transitionTime(time);
436 this._translate(x, y);
437 } else {
438 this._animate(x, y, time, easing.fn);
439 }
440 },
441
442 scrollToElement: function (el, time, offsetX, offsetY, easing) {
443 el = el.nodeType ? el : this.scroller.querySelector(el);
444
445 if ( !el ) {
446 return;
447 }
448
449 var pos = utils.offset(el);
450
451 pos.left -= this.wrapperOffset.left;
452 pos.top -= this.wrapperOffset.top;
453
454 // if offsetX/Y are true we center the element to the screen
455 if ( offsetX === true ) {
456 offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
457 }
458 if ( offsetY === true ) {
459 offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
460 }
461
462 pos.left -= offsetX || 0;
463 pos.top -= offsetY || 0;
464
465 pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
466 pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
467
468 time = time === undefined || null || 'auto' ? Math.max(Math.abs(pos.left)*2, Math.abs(pos.top)*2) : time;
469
470 this.scrollTo(pos.left, pos.top, time, easing);
471 },
472
473 _transitionTime: function (time) {
474 time = time || 0;
475 this.scrollerStyle[utils.style.transitionDuration] = time + 'ms';
476
477// INSERT POINT: _transitionTime
478
479 },
480
481 _transitionTimingFunction: function (easing) {
482 this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
483
484// INSERT POINT: _transitionTimingFunction
485
486 },
487
488 _translate: function (x, y) {
489 if ( this.options.useTransform ) {
490
491/* REPLACE START: _translate */
492
493 this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
494
495/* REPLACE END: _translate */
496
497 } else {
498 x = Math.round(x);
499 y = Math.round(y);
500 this.scrollerStyle.left = x + 'px';
501 this.scrollerStyle.top = y + 'px';
502 }
503
504 this.x = x;
505 this.y = y;
506
507// INSERT POINT: _translate
508
509 },
510
511 _initEvents: function (remove) {
512 var eventType = remove ? utils.removeEvent : utils.addEvent,
513 target = this.options.bindToWrapper ? this.wrapper : window;
514
515 eventType(window, 'orientationchange', this);
516 eventType(window, 'resize', this);
517
518 eventType(this.wrapper, 'mousedown', this);
519 eventType(target, 'mousemove', this);
520 eventType(target, 'mousecancel', this);
521 eventType(target, 'mouseup', this);
522
523 if ( utils.hasPointer ) {
524 eventType(this.wrapper, 'MSPointerDown', this);
525 eventType(target, 'MSPointerMove', this);
526 eventType(target, 'MSPointerCancel', this);
527 eventType(target, 'MSPointerUp', this);
528 }
529
530 if ( utils.hasTouch ) {
531 eventType(this.wrapper, 'touchstart', this);
532 eventType(target, 'touchmove', this);
533 eventType(target, 'touchcancel', this);
534 eventType(target, 'touchend', this);
535 }
536
537 eventType(this.scroller, 'transitionend', this);
538 eventType(this.scroller, 'webkitTransitionEnd', this);
539 eventType(this.scroller, 'oTransitionEnd', this);
540 eventType(this.scroller, 'MSTransitionEnd', this);
541 },
542
543 getComputedPosition: function () {
544 var matrix = window.getComputedStyle(this.scroller, null),
545 x, y;
546
547 if ( this.options.useTransform ) {
548 matrix = matrix[utils.style.transform].split(')')[0].split(', ');
549 x = +(matrix[12] || matrix[4]);
550 y = +(matrix[13] || matrix[5]);
551 } else {
552 x = +matrix.left.replace(/[^-\d]/g, '');
553 y = +matrix.top.replace(/[^-\d]/g, '');
554 }
555
556 return { x: x, y: y };
557 },