Elffuss BraidHub

kikocis/iscroll public

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