Elffuss BraidHub

kikocis/iscroll public

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