Elffuss BraidHub

kikocis/iscroll public

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