Elffuss BraidHub

kikocis/iscroll public

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