Elffuss BraidHub

kikocis/iscroll public

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