source: other-projects/nz-flag-design/trunk/main-form/lib/iscroll.js@ 29530

Last change on this file since 29530 was 29530, checked in by davidb, 9 years ago

First cut at overview web pages to nz-flag-design project

  • Property svn:executable set to *
File size: 32.7 KB
Line 
1/*!
2 * iScroll v4.1.9 ~ Copyright (c) 2011 Matteo Spinelli, http://cubiq.org
3 * Released under MIT license, http://cubiq.org/license
4 */
5(function(){
6var m = Math,
7 mround = function (r) { return r >> 0; },
8 vendor = (/webkit/i).test(navigator.appVersion) ? 'webkit' :
9 (/firefox/i).test(navigator.userAgent) ? 'Moz' :
10 (/trident/i).test(navigator.userAgent) ? 'ms' :
11 'opera' in window ? 'O' : '',
12
13 // Browser capabilities
14 isAndroid = (/android/gi).test(navigator.appVersion),
15 isIDevice = (/iphone|ipad/gi).test(navigator.appVersion),
16 isPlaybook = (/playbook/gi).test(navigator.appVersion),
17 isTouchPad = (/hp-tablet/gi).test(navigator.appVersion),
18
19 has3d = 'WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix(),
20 hasTouch = 'ontouchstart' in window && !isTouchPad,
21 hasTransform = vendor + 'Transform' in document.documentElement.style,
22 hasTransitionEnd = isIDevice || isPlaybook,
23
24 nextFrame = (function() {
25 return window.requestAnimationFrame
26 || window.webkitRequestAnimationFrame
27 || window.mozRequestAnimationFrame
28 || window.oRequestAnimationFrame
29 || window.msRequestAnimationFrame
30 || function(callback) { return setTimeout(callback, 1); };
31 })(),
32 cancelFrame = (function () {
33 return window.cancelRequestAnimationFrame
34 || window.webkitCancelAnimationFrame
35 || window.webkitCancelRequestAnimationFrame
36 || window.mozCancelRequestAnimationFrame
37 || window.oCancelRequestAnimationFrame
38 || window.msCancelRequestAnimationFrame
39 || clearTimeout;
40 })(),
41
42 // Events
43 RESIZE_EV = 'onorientationchange' in window ? 'orientationchange' : 'resize',
44 START_EV = hasTouch ? 'touchstart' : 'mousedown',
45 MOVE_EV = hasTouch ? 'touchmove' : 'mousemove',
46 END_EV = hasTouch ? 'touchend' : 'mouseup',
47 CANCEL_EV = hasTouch ? 'touchcancel' : 'mouseup',
48 WHEEL_EV = vendor == 'Moz' ? 'DOMMouseScroll' : 'mousewheel',
49
50 // Helpers
51 trnOpen = 'translate' + (has3d ? '3d(' : '('),
52 trnClose = has3d ? ',0)' : ')',
53
54 // Constructor
55 iScroll = function (el, options) {
56 var that = this,
57 doc = document,
58 i;
59
60 that.wrapper = typeof el == 'object' ? el : doc.getElementById(el);
61 that.wrapper.style.overflow = 'hidden';
62 that.scroller = that.wrapper.children[0];
63
64 // Default options
65 that.options = {
66 hScroll: true,
67 vScroll: true,
68 x: 0,
69 y: 0,
70 bounce: true,
71 bounceLock: false,
72 momentum: true,
73 lockDirection: true,
74 useTransform: true,
75 useTransition: false,
76 topOffset: 0,
77 checkDOMChanges: false, // Experimental
78
79 // Scrollbar
80 hScrollbar: true,
81 vScrollbar: true,
82 fixedScrollbar: isAndroid,
83 hideScrollbar: isIDevice,
84 fadeScrollbar: isIDevice && has3d,
85 scrollbarClass: '',
86
87 // Zoom
88 zoom: false,
89 zoomMin: 1,
90 zoomMax: 4,
91 doubleTapZoom: 2,
92 wheelAction: 'scroll',
93
94 // Snap
95 snap: false,
96 snapThreshold: 1,
97
98 // Events
99 onRefresh: null,
100 onBeforeScrollStart: function (e) { e.preventDefault(); },
101 onScrollStart: null,
102 onBeforeScrollMove: null,
103 onScrollMove: null,
104 onBeforeScrollEnd: null,
105 onScrollEnd: null,
106 onTouchEnd: null,
107 onDestroy: null,
108 onZoomStart: null,
109 onZoom: null,
110 onZoomEnd: null
111 };
112
113 // User defined options
114 for (i in options) that.options[i] = options[i];
115
116 // Set starting position
117 that.x = that.options.x;
118 that.y = that.options.y;
119
120 // Normalize options
121 that.options.useTransform = hasTransform ? that.options.useTransform : false;
122 that.options.hScrollbar = that.options.hScroll && that.options.hScrollbar;
123 that.options.vScrollbar = that.options.vScroll && that.options.vScrollbar;
124 that.options.zoom = that.options.useTransform && that.options.zoom;
125 that.options.useTransition = hasTransitionEnd && that.options.useTransition;
126
127 // Helpers FIX ANDROID BUG!
128 // translate3d and scale doesn't work together!
129 // Ignoring 3d ONLY WHEN YOU SET that.options.zoom
130 if ( that.options.zoom && isAndroid ){
131 trnOpen = 'translate(';
132 trnClose = ')';
133 }
134
135 // Set some default styles
136 that.scroller.style[vendor + 'TransitionProperty'] = that.options.useTransform ? '-' + vendor.toLowerCase() + '-transform' : 'top left';
137 that.scroller.style[vendor + 'TransitionDuration'] = '0';
138 that.scroller.style[vendor + 'TransformOrigin'] = '0 0';
139 if (that.options.useTransition) that.scroller.style[vendor + 'TransitionTimingFunction'] = 'cubic-bezier(0.33,0.66,0.66,1)';
140
141 if (that.options.useTransform) that.scroller.style[vendor + 'Transform'] = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose;
142 else that.scroller.style.cssText += ';position:absolute;top:' + that.y + 'px;left:' + that.x + 'px';
143
144 if (that.options.useTransition) that.options.fixedScrollbar = true;
145
146 that.refresh();
147
148 that._bind(RESIZE_EV, window);
149 that._bind(START_EV);
150 if (!hasTouch) {
151 that._bind('mouseout', that.wrapper);
152 if (that.options.wheelAction != 'none')
153 that._bind(WHEEL_EV);
154 }
155
156 if (that.options.checkDOMChanges) that.checkDOMTime = setInterval(function () {
157 that._checkDOMChanges();
158 }, 500);
159 };
160
161// Prototype
162iScroll.prototype = {
163 enabled: true,
164 x: 0,
165 y: 0,
166 steps: [],
167 scale: 1,
168 currPageX: 0, currPageY: 0,
169 pagesX: [], pagesY: [],
170 aniTime: null,
171 wheelZoomCount: 0,
172
173 handleEvent: function (e) {
174 var that = this;
175 switch(e.type) {
176 case START_EV:
177 if (!hasTouch && e.button !== 0) return;
178 that._start(e);
179 break;
180 case MOVE_EV: that._move(e); break;
181 case END_EV:
182 case CANCEL_EV: that._end(e); break;
183 case RESIZE_EV: that._resize(); break;
184 case WHEEL_EV: that._wheel(e); break;
185 case 'mouseout': that._mouseout(e); break;
186 case 'webkitTransitionEnd': that._transitionEnd(e); break;
187 }
188 },
189
190 _checkDOMChanges: function () {
191 if (this.moved || this.zoomed || this.animating ||
192 (this.scrollerW == this.scroller.offsetWidth * this.scale && this.scrollerH == this.scroller.offsetHeight * this.scale)) return;
193
194 this.refresh();
195 },
196
197 _scrollbar: function (dir) {
198 var that = this,
199 doc = document,
200 bar;
201
202 if (!that[dir + 'Scrollbar']) {
203 if (that[dir + 'ScrollbarWrapper']) {
204 if (hasTransform) that[dir + 'ScrollbarIndicator'].style[vendor + 'Transform'] = '';
205 that[dir + 'ScrollbarWrapper'].parentNode.removeChild(that[dir + 'ScrollbarWrapper']);
206 that[dir + 'ScrollbarWrapper'] = null;
207 that[dir + 'ScrollbarIndicator'] = null;
208 }
209
210 return;
211 }
212
213 if (!that[dir + 'ScrollbarWrapper']) {
214 // Create the scrollbar wrapper
215 bar = doc.createElement('div');
216
217 if (that.options.scrollbarClass) bar.className = that.options.scrollbarClass + dir.toUpperCase();
218 else bar.style.cssText = 'position:absolute;z-index:100;' + (dir == 'h' ? 'height:7px;bottom:1px;left:2px;right:' + (that.vScrollbar ? '7' : '2') + 'px' : 'width:7px;bottom:' + (that.hScrollbar ? '7' : '2') + 'px;top:2px;right:1px');
219
220 bar.style.cssText += ';pointer-events:none;-' + vendor + '-transition-property:opacity;-' + vendor + '-transition-duration:' + (that.options.fadeScrollbar ? '350ms' : '0') + ';overflow:hidden;opacity:' + (that.options.hideScrollbar ? '0' : '1');
221
222 that.wrapper.appendChild(bar);
223 that[dir + 'ScrollbarWrapper'] = bar;
224
225 // Create the scrollbar indicator
226 bar = doc.createElement('div');
227 if (!that.options.scrollbarClass) {
228 bar.style.cssText = 'position:absolute;z-index:100;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);-' + vendor + '-background-clip:padding-box;-' + vendor + '-box-sizing:border-box;' + (dir == 'h' ? 'height:100%' : 'width:100%') + ';-' + vendor + '-border-radius:3px;border-radius:3px';
229 }
230 bar.style.cssText += ';pointer-events:none;-' + vendor + '-transition-property:-' + vendor + '-transform;-' + vendor + '-transition-timing-function:cubic-bezier(0.33,0.66,0.66,1);-' + vendor + '-transition-duration:0;-' + vendor + '-transform:' + trnOpen + '0,0' + trnClose;
231 if (that.options.useTransition) bar.style.cssText += ';-' + vendor + '-transition-timing-function:cubic-bezier(0.33,0.66,0.66,1)';
232
233 that[dir + 'ScrollbarWrapper'].appendChild(bar);
234 that[dir + 'ScrollbarIndicator'] = bar;
235 }
236
237 if (dir == 'h') {
238 that.hScrollbarSize = that.hScrollbarWrapper.clientWidth;
239 that.hScrollbarIndicatorSize = m.max(mround(that.hScrollbarSize * that.hScrollbarSize / that.scrollerW), 8);
240 that.hScrollbarIndicator.style.width = that.hScrollbarIndicatorSize + 'px';
241 that.hScrollbarMaxScroll = that.hScrollbarSize - that.hScrollbarIndicatorSize;
242 that.hScrollbarProp = that.hScrollbarMaxScroll / that.maxScrollX;
243 } else {
244 that.vScrollbarSize = that.vScrollbarWrapper.clientHeight;
245 that.vScrollbarIndicatorSize = m.max(mround(that.vScrollbarSize * that.vScrollbarSize / that.scrollerH), 8);
246 that.vScrollbarIndicator.style.height = that.vScrollbarIndicatorSize + 'px';
247 that.vScrollbarMaxScroll = that.vScrollbarSize - that.vScrollbarIndicatorSize;
248 that.vScrollbarProp = that.vScrollbarMaxScroll / that.maxScrollY;
249 }
250
251 // Reset position
252 that._scrollbarPos(dir, true);
253 },
254
255 _resize: function () {
256 var that = this;
257 setTimeout(function () { that.refresh(); }, isAndroid ? 200 : 0);
258 },
259
260 _pos: function (x, y) {
261 if (this.zoomed) return;
262
263 x = this.hScroll ? x : 0;
264 y = this.vScroll ? y : 0;
265
266 if (this.options.useTransform) {
267 this.scroller.style[vendor + 'Transform'] = trnOpen + x + 'px,' + y + 'px' + trnClose + ' scale(' + this.scale + ')';
268 } else {
269 x = mround(x);
270 y = mround(y);
271 this.scroller.style.left = x + 'px';
272 this.scroller.style.top = y + 'px';
273 }
274
275 this.x = x;
276 this.y = y;
277
278 this._scrollbarPos('h');
279 this._scrollbarPos('v');
280 },
281
282 _scrollbarPos: function (dir, hidden) {
283 var that = this,
284 pos = dir == 'h' ? that.x : that.y,
285 size;
286
287 if (!that[dir + 'Scrollbar']) return;
288
289 pos = that[dir + 'ScrollbarProp'] * pos;
290
291 if (pos < 0) {
292 if (!that.options.fixedScrollbar) {
293 size = that[dir + 'ScrollbarIndicatorSize'] + mround(pos * 3);
294 if (size < 8) size = 8;
295 that[dir + 'ScrollbarIndicator'].style[dir == 'h' ? 'width' : 'height'] = size + 'px';
296 }
297 pos = 0;
298 } else if (pos > that[dir + 'ScrollbarMaxScroll']) {
299 if (!that.options.fixedScrollbar) {
300 size = that[dir + 'ScrollbarIndicatorSize'] - mround((pos - that[dir + 'ScrollbarMaxScroll']) * 3);
301 if (size < 8) size = 8;
302 that[dir + 'ScrollbarIndicator'].style[dir == 'h' ? 'width' : 'height'] = size + 'px';
303 pos = that[dir + 'ScrollbarMaxScroll'] + (that[dir + 'ScrollbarIndicatorSize'] - size);
304 } else {
305 pos = that[dir + 'ScrollbarMaxScroll'];
306 }
307 }
308
309 that[dir + 'ScrollbarWrapper'].style[vendor + 'TransitionDelay'] = '0';
310 that[dir + 'ScrollbarWrapper'].style.opacity = hidden && that.options.hideScrollbar ? '0' : '1';
311 that[dir + 'ScrollbarIndicator'].style[vendor + 'Transform'] = trnOpen + (dir == 'h' ? pos + 'px,0' : '0,' + pos + 'px') + trnClose;
312 },
313
314 _start: function (e) {
315 var that = this,
316 point = hasTouch ? e.touches[0] : e,
317 matrix, x, y,
318 c1, c2;
319
320 if (!that.enabled) return;
321
322 if (that.options.onBeforeScrollStart) that.options.onBeforeScrollStart.call(that, e);
323
324 if (that.options.useTransition || that.options.zoom) that._transitionTime(0);
325
326 that.moved = false;
327 that.animating = false;
328 that.zoomed = false;
329 that.distX = 0;
330 that.distY = 0;
331 that.absDistX = 0;
332 that.absDistY = 0;
333 that.dirX = 0;
334 that.dirY = 0;
335
336 // Gesture start
337 if (that.options.zoom && hasTouch && e.touches.length > 1) {
338 c1 = m.abs(e.touches[0].pageX-e.touches[1].pageX);
339 c2 = m.abs(e.touches[0].pageY-e.touches[1].pageY);
340 that.touchesDistStart = m.sqrt(c1 * c1 + c2 * c2);
341
342 that.originX = m.abs(e.touches[0].pageX + e.touches[1].pageX - that.wrapperOffsetLeft * 2) / 2 - that.x;
343 that.originY = m.abs(e.touches[0].pageY + e.touches[1].pageY - that.wrapperOffsetTop * 2) / 2 - that.y;
344
345 if (that.options.onZoomStart) that.options.onZoomStart.call(that, e);
346 }
347
348 if (that.options.momentum) {
349 if (that.options.useTransform) {
350 // Very lame general purpose alternative to CSSMatrix
351 matrix = getComputedStyle(that.scroller, null)[vendor + 'Transform'].replace(/[^0-9-.,]/g, '').split(',');
352 x = matrix[4] * 1;
353 y = matrix[5] * 1;
354 } else {
355 x = getComputedStyle(that.scroller, null).left.replace(/[^0-9-]/g, '') * 1;
356 y = getComputedStyle(that.scroller, null).top.replace(/[^0-9-]/g, '') * 1;
357 }
358
359 if (x != that.x || y != that.y) {
360 if (that.options.useTransition) that._unbind('webkitTransitionEnd');
361 else cancelFrame(that.aniTime);
362 that.steps = [];
363 that._pos(x, y);
364 }
365 }
366
367 that.absStartX = that.x; // Needed by snap threshold
368 that.absStartY = that.y;
369
370 that.startX = that.x;
371 that.startY = that.y;
372 that.pointX = point.pageX;
373 that.pointY = point.pageY;
374
375 that.startTime = e.timeStamp || Date.now();
376
377 if (that.options.onScrollStart) that.options.onScrollStart.call(that, e);
378
379 that._bind(MOVE_EV);
380 that._bind(END_EV);
381 that._bind(CANCEL_EV);
382 },
383
384 _move: function (e) {
385 var that = this,
386 point = hasTouch ? e.touches[0] : e,
387 deltaX = point.pageX - that.pointX,
388 deltaY = point.pageY - that.pointY,
389 newX = that.x + deltaX,
390 newY = that.y + deltaY,
391 c1, c2, scale,
392 timestamp = e.timeStamp || Date.now();
393
394 if (that.options.onBeforeScrollMove) that.options.onBeforeScrollMove.call(that, e);
395
396 // Zoom
397 if (that.options.zoom && hasTouch && e.touches.length > 1) {
398 c1 = m.abs(e.touches[0].pageX - e.touches[1].pageX);
399 c2 = m.abs(e.touches[0].pageY - e.touches[1].pageY);
400 that.touchesDist = m.sqrt(c1*c1+c2*c2);
401
402 that.zoomed = true;
403
404 scale = 1 / that.touchesDistStart * that.touchesDist * this.scale;
405
406 if (scale < that.options.zoomMin) scale = 0.5 * that.options.zoomMin * Math.pow(2.0, scale / that.options.zoomMin);
407 else if (scale > that.options.zoomMax) scale = 2.0 * that.options.zoomMax * Math.pow(0.5, that.options.zoomMax / scale);
408
409 that.lastScale = scale / this.scale;
410
411 newX = this.originX - this.originX * that.lastScale + this.x,
412 newY = this.originY - this.originY * that.lastScale + this.y;
413
414 this.scroller.style[vendor + 'Transform'] = trnOpen + newX + 'px,' + newY + 'px' + trnClose + ' scale(' + scale + ')';
415
416 if (that.options.onZoom) that.options.onZoom.call(that, e);
417 return;
418 }
419
420 that.pointX = point.pageX;
421 that.pointY = point.pageY;
422
423 // Slow down if outside of the boundaries
424 if (newX > 0 || newX < that.maxScrollX) {
425 newX = that.options.bounce ? that.x + (deltaX / 2) : newX >= 0 || that.maxScrollX >= 0 ? 0 : that.maxScrollX;
426 }
427 if (newY > that.minScrollY || newY < that.maxScrollY) {
428 newY = that.options.bounce ? that.y + (deltaY / 2) : newY >= that.minScrollY || that.maxScrollY >= 0 ? that.minScrollY : that.maxScrollY;
429 }
430
431 that.distX += deltaX;
432 that.distY += deltaY;
433 that.absDistX = m.abs(that.distX);
434 that.absDistY = m.abs(that.distY);
435
436 if (that.absDistX < 6 && that.absDistY < 6) {
437 return;
438 }
439
440 // Lock direction
441 if (that.options.lockDirection) {
442 if (that.absDistX > that.absDistY + 5) {
443 newY = that.y;
444 deltaY = 0;
445 } else if (that.absDistY > that.absDistX + 5) {
446 newX = that.x;
447 deltaX = 0;
448 }
449 }
450
451 that.moved = true;
452 that._pos(newX, newY);
453 that.dirX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
454 that.dirY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
455
456 if (timestamp - that.startTime > 300) {
457 that.startTime = timestamp;
458 that.startX = that.x;
459 that.startY = that.y;
460 }
461
462 if (that.options.onScrollMove) that.options.onScrollMove.call(that, e);
463 },
464
465 _end: function (e) {
466 if (hasTouch && e.touches.length != 0) return;
467
468 var that = this,
469 point = hasTouch ? e.changedTouches[0] : e,
470 target, ev,
471 momentumX = { dist:0, time:0 },
472 momentumY = { dist:0, time:0 },
473 duration = (e.timeStamp || Date.now()) - that.startTime,
474 newPosX = that.x,
475 newPosY = that.y,
476 distX, distY,
477 newDuration,
478 snap,
479 scale;
480
481 that._unbind(MOVE_EV);
482 that._unbind(END_EV);
483 that._unbind(CANCEL_EV);
484
485 if (that.options.onBeforeScrollEnd) that.options.onBeforeScrollEnd.call(that, e);
486
487 if (that.zoomed) {
488 scale = that.scale * that.lastScale;
489 scale = Math.max(that.options.zoomMin, scale);
490 scale = Math.min(that.options.zoomMax, scale);
491 that.lastScale = scale / that.scale;
492 that.scale = scale;
493
494 that.x = that.originX - that.originX * that.lastScale + that.x;
495 that.y = that.originY - that.originY * that.lastScale + that.y;
496
497 that.scroller.style[vendor + 'TransitionDuration'] = '200ms';
498 that.scroller.style[vendor + 'Transform'] = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + that.scale + ')';
499
500 that.zoomed = false;
501 that.refresh();
502
503 if (that.options.onZoomEnd) that.options.onZoomEnd.call(that, e);
504 return;
505 }
506
507 if (!that.moved) {
508 if (hasTouch) {
509 if (that.doubleTapTimer && that.options.zoom) {
510 // Double tapped
511 clearTimeout(that.doubleTapTimer);
512 that.doubleTapTimer = null;
513 if (that.options.onZoomStart) that.options.onZoomStart.call(that, e);
514 that.zoom(that.pointX, that.pointY, that.scale == 1 ? that.options.doubleTapZoom : 1);
515 if (that.options.onZoomEnd) {
516 setTimeout(function() {
517 that.options.onZoomEnd.call(that, e);
518 }, 200); // 200 is default zoom duration
519 }
520 } else {
521 that.doubleTapTimer = setTimeout(function () {
522 that.doubleTapTimer = null;
523
524 // Find the last touched element
525 target = point.target;
526 while (target.nodeType != 1) target = target.parentNode;
527
528 if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA') {
529 ev = document.createEvent('MouseEvents');
530 ev.initMouseEvent('click', true, true, e.view, 1,
531 point.screenX, point.screenY, point.clientX, point.clientY,
532 e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
533 0, null);
534 ev._fake = true;
535 target.dispatchEvent(ev);
536 }
537 }, that.options.zoom ? 250 : 0);
538 }
539 }
540
541 that._resetPos(200);
542
543 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
544 return;
545 }
546
547 if (duration < 300 && that.options.momentum) {
548 momentumX = newPosX ? that._momentum(newPosX - that.startX, duration, -that.x, that.scrollerW - that.wrapperW + that.x, that.options.bounce ? that.wrapperW : 0) : momentumX;
549 momentumY = newPosY ? that._momentum(newPosY - that.startY, duration, -that.y, (that.maxScrollY < 0 ? that.scrollerH - that.wrapperH + that.y - that.minScrollY : 0), that.options.bounce ? that.wrapperH : 0) : momentumY;
550
551 newPosX = that.x + momentumX.dist;
552 newPosY = that.y + momentumY.dist;
553
554 if ((that.x > 0 && newPosX > 0) || (that.x < that.maxScrollX && newPosX < that.maxScrollX)) momentumX = { dist:0, time:0 };
555 if ((that.y > that.minScrollY && newPosY > that.minScrollY) || (that.y < that.maxScrollY && newPosY < that.maxScrollY)) momentumY = { dist:0, time:0 };
556 }
557
558 if (momentumX.dist || momentumY.dist) {
559 newDuration = m.max(m.max(momentumX.time, momentumY.time), 10);
560
561 // Do we need to snap?
562 if (that.options.snap) {
563 distX = newPosX - that.absStartX;
564 distY = newPosY - that.absStartY;
565 if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) { that.scrollTo(that.absStartX, that.absStartY, 200); }
566 else {
567 snap = that._snap(newPosX, newPosY);
568 newPosX = snap.x;
569 newPosY = snap.y;
570 newDuration = m.max(snap.time, newDuration);
571 }
572 }
573
574 that.scrollTo(mround(newPosX), mround(newPosY), newDuration);
575
576 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
577 return;
578 }
579
580 // Do we need to snap?
581 if (that.options.snap) {
582 distX = newPosX - that.absStartX;
583 distY = newPosY - that.absStartY;
584 if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) that.scrollTo(that.absStartX, that.absStartY, 200);
585 else {
586 snap = that._snap(that.x, that.y);
587 if (snap.x != that.x || snap.y != that.y) that.scrollTo(snap.x, snap.y, snap.time);
588 }
589
590 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
591 return;
592 }
593
594 that._resetPos(200);
595 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
596 },
597
598 _resetPos: function (time) {
599 var that = this,
600 resetX = that.x >= 0 ? 0 : that.x < that.maxScrollX ? that.maxScrollX : that.x,
601 resetY = that.y >= that.minScrollY || that.maxScrollY > 0 ? that.minScrollY : that.y < that.maxScrollY ? that.maxScrollY : that.y;
602
603 if (resetX == that.x && resetY == that.y) {
604 if (that.moved) {
605 that.moved = false;
606 if (that.options.onScrollEnd) that.options.onScrollEnd.call(that); // Execute custom code on scroll end
607 }
608
609 if (that.hScrollbar && that.options.hideScrollbar) {
610 if (vendor == 'webkit') that.hScrollbarWrapper.style[vendor + 'TransitionDelay'] = '300ms';
611 that.hScrollbarWrapper.style.opacity = '0';
612 }
613 if (that.vScrollbar && that.options.hideScrollbar) {
614 if (vendor == 'webkit') that.vScrollbarWrapper.style[vendor + 'TransitionDelay'] = '300ms';
615 that.vScrollbarWrapper.style.opacity = '0';
616 }
617
618 return;
619 }
620
621 that.scrollTo(resetX, resetY, time || 0);
622 },
623
624 _wheel: function (e) {
625 var that = this,
626 wheelDeltaX, wheelDeltaY,
627 deltaX, deltaY,
628 deltaScale;
629
630 if ('wheelDeltaX' in e) {
631 wheelDeltaX = e.wheelDeltaX / 12;
632 wheelDeltaY = e.wheelDeltaY / 12;
633 } else if('wheelDelta' in e) {
634 wheelDeltaX = wheelDeltaY = e.wheelDelta / 12;
635 } else if ('detail' in e) {
636 wheelDeltaX = wheelDeltaY = -e.detail * 3;
637 } else {
638 return;
639 }
640
641 if (that.options.wheelAction == 'zoom') {
642 deltaScale = that.scale * Math.pow(2, 1/3 * (wheelDeltaY ? wheelDeltaY / Math.abs(wheelDeltaY) : 0));
643 if (deltaScale < that.options.zoomMin) deltaScale = that.options.zoomMin;
644 if (deltaScale > that.options.zoomMax) deltaScale = that.options.zoomMax;
645
646 if (deltaScale != that.scale) {
647 if (!that.wheelZoomCount && that.options.onZoomStart) that.options.onZoomStart.call(that, e);
648 that.wheelZoomCount++;
649
650 that.zoom(e.pageX, e.pageY, deltaScale, 400);
651
652 setTimeout(function() {
653 that.wheelZoomCount--;
654 if (!that.wheelZoomCount && that.options.onZoomEnd) that.options.onZoomEnd.call(that, e);
655 }, 400);
656 }
657
658 return;
659 }
660
661 deltaX = that.x + wheelDeltaX;
662 deltaY = that.y + wheelDeltaY;
663
664 if (deltaX > 0) deltaX = 0;
665 else if (deltaX < that.maxScrollX) deltaX = that.maxScrollX;
666
667 if (deltaY > that.minScrollY) deltaY = that.minScrollY;
668 else if (deltaY < that.maxScrollY) deltaY = that.maxScrollY;
669
670 if(that.maxScrollY < 0){
671 that.scrollTo(deltaX, deltaY, 0);
672 }
673 },
674
675 _mouseout: function (e) {
676 var t = e.relatedTarget;
677
678 if (!t) {
679 this._end(e);
680 return;
681 }
682
683 while (t = t.parentNode) if (t == this.wrapper) return;
684
685 this._end(e);
686 },
687
688 _transitionEnd: function (e) {
689 var that = this;
690
691 if (e.target != that.scroller) return;
692
693 that._unbind('webkitTransitionEnd');
694
695 that._startAni();
696 },
697
698
699 /**
700 *
701 * Utilities
702 *
703 */
704 _startAni: function () {
705 var that = this,
706 startX = that.x, startY = that.y,
707 startTime = Date.now(),
708 step, easeOut,
709 animate;
710
711 if (that.animating) return;
712
713 if (!that.steps.length) {
714 that._resetPos(400);
715 return;
716 }
717
718 step = that.steps.shift();
719
720 if (step.x == startX && step.y == startY) step.time = 0;
721
722 that.animating = true;
723 that.moved = true;
724
725 if (that.options.useTransition) {
726 that._transitionTime(step.time);
727 that._pos(step.x, step.y);
728 that.animating = false;
729 if (step.time) that._bind('webkitTransitionEnd');
730 else that._resetPos(0);
731 return;
732 }
733
734 animate = function () {
735 var now = Date.now(),
736 newX, newY;
737
738 if (now >= startTime + step.time) {
739 that._pos(step.x, step.y);
740 that.animating = false;
741 if (that.options.onAnimationEnd) that.options.onAnimationEnd.call(that); // Execute custom code on animation end
742 that._startAni();
743 return;
744 }
745
746 now = (now - startTime) / step.time - 1;
747 easeOut = m.sqrt(1 - now * now);
748 newX = (step.x - startX) * easeOut + startX;
749 newY = (step.y - startY) * easeOut + startY;
750 that._pos(newX, newY);
751 if (that.animating) that.aniTime = nextFrame(animate);
752 };
753
754 animate();
755 },
756
757 _transitionTime: function (time) {
758 time += 'ms';
759 this.scroller.style[vendor + 'TransitionDuration'] = time;
760 if (this.hScrollbar) this.hScrollbarIndicator.style[vendor + 'TransitionDuration'] = time;
761 if (this.vScrollbar) this.vScrollbarIndicator.style[vendor + 'TransitionDuration'] = time;
762 },
763
764 _momentum: function (dist, time, maxDistUpper, maxDistLower, size) {
765 var deceleration = 0.0006,
766 speed = m.abs(dist) / time,
767 newDist = (speed * speed) / (2 * deceleration),
768 newTime = 0, outsideDist = 0;
769
770 // Proportinally reduce speed if we are outside of the boundaries
771 if (dist > 0 && newDist > maxDistUpper) {
772 outsideDist = size / (6 / (newDist / speed * deceleration));
773 maxDistUpper = maxDistUpper + outsideDist;
774 speed = speed * maxDistUpper / newDist;
775 newDist = maxDistUpper;
776 } else if (dist < 0 && newDist > maxDistLower) {
777 outsideDist = size / (6 / (newDist / speed * deceleration));
778 maxDistLower = maxDistLower + outsideDist;
779 speed = speed * maxDistLower / newDist;
780 newDist = maxDistLower;
781 }
782
783 newDist = newDist * (dist < 0 ? -1 : 1);
784 newTime = speed / deceleration;
785
786 return { dist: newDist, time: mround(newTime) };
787 },
788
789 _offset: function (el) {
790 var left = -el.offsetLeft,
791 top = -el.offsetTop;
792
793 while (el = el.offsetParent) {
794 left -= el.offsetLeft;
795 top -= el.offsetTop;
796 }
797
798 if (el != this.wrapper) {
799 left *= this.scale;
800 top *= this.scale;
801 }
802
803 return { left: left, top: top };
804 },
805
806 _snap: function (x, y) {
807 var that = this,
808 i, l,
809 page, time,
810 sizeX, sizeY;
811
812 // Check page X
813 page = that.pagesX.length - 1;
814 for (i=0, l=that.pagesX.length; i<l; i++) {
815 if (x >= that.pagesX[i]) {
816 page = i;
817 break;
818 }
819 }
820 if (page == that.currPageX && page > 0 && that.dirX < 0) page--;
821 x = that.pagesX[page];
822 sizeX = m.abs(x - that.pagesX[that.currPageX]);
823 sizeX = sizeX ? m.abs(that.x - x) / sizeX * 500 : 0;
824 that.currPageX = page;
825
826 // Check page Y
827 page = that.pagesY.length-1;
828 for (i=0; i<page; i++) {
829 if (y >= that.pagesY[i]) {
830 page = i;
831 break;
832 }
833 }
834 if (page == that.currPageY && page > 0 && that.dirY < 0) page--;
835 y = that.pagesY[page];
836 sizeY = m.abs(y - that.pagesY[that.currPageY]);
837 sizeY = sizeY ? m.abs(that.y - y) / sizeY * 500 : 0;
838 that.currPageY = page;
839
840 // Snap with constant speed (proportional duration)
841 time = mround(m.max(sizeX, sizeY)) || 200;
842
843 return { x: x, y: y, time: time };
844 },
845
846 _bind: function (type, el, bubble) {
847 (el || this.scroller).addEventListener(type, this, !!bubble);
848 },
849
850 _unbind: function (type, el, bubble) {
851 (el || this.scroller).removeEventListener(type, this, !!bubble);
852 },
853
854
855 /**
856 *
857 * Public methods
858 *
859 */
860 destroy: function () {
861 var that = this;
862
863 that.scroller.style[vendor + 'Transform'] = '';
864
865 // Remove the scrollbars
866 that.hScrollbar = false;
867 that.vScrollbar = false;
868 that._scrollbar('h');
869 that._scrollbar('v');
870
871 // Remove the event listeners
872 that._unbind(RESIZE_EV, window);
873 that._unbind(START_EV);
874 that._unbind(MOVE_EV);
875 that._unbind(END_EV);
876 that._unbind(CANCEL_EV);
877
878 if (!that.options.hasTouch) {
879 that._unbind('mouseout', that.wrapper);
880 that._unbind(WHEEL_EV);
881 }
882
883 if (that.options.useTransition) that._unbind('webkitTransitionEnd');
884
885 if (that.options.checkDOMChanges) clearInterval(that.checkDOMTime);
886
887 if (that.options.onDestroy) that.options.onDestroy.call(that);
888 },
889
890 refresh: function () {
891 var that = this,
892 offset,
893 i, l,
894 els,
895 pos = 0,
896 page = 0;
897
898 if (that.scale < that.options.zoomMin) that.scale = that.options.zoomMin;
899 that.wrapperW = that.wrapper.clientWidth || 1;
900 that.wrapperH = that.wrapper.clientHeight || 1;
901
902 that.minScrollY = -that.options.topOffset || 0;
903 that.scrollerW = mround(that.scroller.offsetWidth * that.scale);
904 that.scrollerH = mround((that.scroller.offsetHeight + that.minScrollY) * that.scale);
905 that.maxScrollX = that.wrapperW - that.scrollerW;
906 that.maxScrollY = that.wrapperH - that.scrollerH + that.minScrollY;
907 that.dirX = 0;
908 that.dirY = 0;
909
910 if (that.options.onRefresh) that.options.onRefresh.call(that);
911
912 that.hScroll = that.options.hScroll && that.maxScrollX < 0;
913 that.vScroll = that.options.vScroll && (!that.options.bounceLock && !that.hScroll || that.scrollerH > that.wrapperH);
914
915 that.hScrollbar = that.hScroll && that.options.hScrollbar;
916 that.vScrollbar = that.vScroll && that.options.vScrollbar && that.scrollerH > that.wrapperH;
917
918 offset = that._offset(that.wrapper);
919 that.wrapperOffsetLeft = -offset.left;
920 that.wrapperOffsetTop = -offset.top;
921
922 // Prepare snap
923 if (typeof that.options.snap == 'string') {
924 that.pagesX = [];
925 that.pagesY = [];
926 els = that.scroller.querySelectorAll(that.options.snap);
927 for (i=0, l=els.length; i<l; i++) {
928 pos = that._offset(els[i]);
929 pos.left += that.wrapperOffsetLeft;
930 pos.top += that.wrapperOffsetTop;
931 that.pagesX[i] = pos.left < that.maxScrollX ? that.maxScrollX : pos.left * that.scale;
932 that.pagesY[i] = pos.top < that.maxScrollY ? that.maxScrollY : pos.top * that.scale;
933 }
934 } else if (that.options.snap) {
935 that.pagesX = [];
936 while (pos >= that.maxScrollX) {
937 that.pagesX[page] = pos;
938 pos = pos - that.wrapperW;
939 page++;
940 }
941 if (that.maxScrollX%that.wrapperW) that.pagesX[that.pagesX.length] = that.maxScrollX - that.pagesX[that.pagesX.length-1] + that.pagesX[that.pagesX.length-1];
942
943 pos = 0;
944 page = 0;
945 that.pagesY = [];
946 while (pos >= that.maxScrollY) {
947 that.pagesY[page] = pos;
948 pos = pos - that.wrapperH;
949 page++;
950 }
951 if (that.maxScrollY%that.wrapperH) that.pagesY[that.pagesY.length] = that.maxScrollY - that.pagesY[that.pagesY.length-1] + that.pagesY[that.pagesY.length-1];
952 }
953
954 // Prepare the scrollbars
955 that._scrollbar('h');
956 that._scrollbar('v');
957
958 if (!that.zoomed) {
959 that.scroller.style[vendor + 'TransitionDuration'] = '0';
960 that._resetPos(200);
961 }
962 },
963
964 scrollTo: function (x, y, time, relative) {
965 var that = this,
966 step = x,
967 i, l;
968
969 that.stop();
970
971 if (!step.length) step = [{ x: x, y: y, time: time, relative: relative }];
972
973 for (i=0, l=step.length; i<l; i++) {
974 if (step[i].relative) { step[i].x = that.x - step[i].x; step[i].y = that.y - step[i].y; }
975 that.steps.push({ x: step[i].x, y: step[i].y, time: step[i].time || 0 });
976 }
977
978 that._startAni();
979 },
980
981 scrollToElement: function (el, time) {
982 var that = this, pos;
983 el = el.nodeType ? el : that.scroller.querySelector(el);
984 if (!el) return;
985
986 pos = that._offset(el);
987 pos.left += that.wrapperOffsetLeft;
988 pos.top += that.wrapperOffsetTop;
989
990 pos.left = pos.left > 0 ? 0 : pos.left < that.maxScrollX ? that.maxScrollX : pos.left;
991 pos.top = pos.top > that.minScrollY ? that.minScrollY : pos.top < that.maxScrollY ? that.maxScrollY : pos.top;
992 time = time === undefined ? m.max(m.abs(pos.left)*2, m.abs(pos.top)*2) : time;
993
994 that.scrollTo(pos.left, pos.top, time);
995 },
996
997 scrollToPage: function (pageX, pageY, time) {
998 var that = this, x, y;
999
1000 time = time === undefined ? 400 : time;
1001
1002 if (that.options.onScrollStart) that.options.onScrollStart.call(that);
1003
1004 if (that.options.snap) {
1005 pageX = pageX == 'next' ? that.currPageX+1 : pageX == 'prev' ? that.currPageX-1 : pageX;
1006 pageY = pageY == 'next' ? that.currPageY+1 : pageY == 'prev' ? that.currPageY-1 : pageY;
1007
1008 pageX = pageX < 0 ? 0 : pageX > that.pagesX.length-1 ? that.pagesX.length-1 : pageX;
1009 pageY = pageY < 0 ? 0 : pageY > that.pagesY.length-1 ? that.pagesY.length-1 : pageY;
1010
1011 that.currPageX = pageX;
1012 that.currPageY = pageY;
1013 x = that.pagesX[pageX];
1014 y = that.pagesY[pageY];
1015 } else {
1016 x = -that.wrapperW * pageX;
1017 y = -that.wrapperH * pageY;
1018 if (x < that.maxScrollX) x = that.maxScrollX;
1019 if (y < that.maxScrollY) y = that.maxScrollY;
1020 }
1021
1022 that.scrollTo(x, y, time);
1023 },
1024
1025 disable: function () {
1026 this.stop();
1027 this._resetPos(0);
1028 this.enabled = false;
1029
1030 // If disabled after touchstart we make sure that there are no left over events
1031 this._unbind(MOVE_EV);
1032 this._unbind(END_EV);
1033 this._unbind(CANCEL_EV);
1034 },
1035
1036 enable: function () {
1037 this.enabled = true;
1038 },
1039
1040 stop: function () {
1041 if (this.options.useTransition) this._unbind('webkitTransitionEnd');
1042 else cancelFrame(this.aniTime);
1043 this.steps = [];
1044 this.moved = false;
1045 this.animating = false;
1046 },
1047
1048 zoom: function (x, y, scale, time) {
1049 var that = this,
1050 relScale = scale / that.scale;
1051
1052 if (!that.options.useTransform) return;
1053
1054 that.zoomed = true;
1055 time = time === undefined ? 200 : time;
1056 x = x - that.wrapperOffsetLeft - that.x;
1057 y = y - that.wrapperOffsetTop - that.y;
1058 that.x = x - x * relScale + that.x;
1059 that.y = y - y * relScale + that.y;
1060
1061 that.scale = scale;
1062 that.refresh();
1063
1064 that.x = that.x > 0 ? 0 : that.x < that.maxScrollX ? that.maxScrollX : that.x;
1065 that.y = that.y > that.minScrollY ? that.minScrollY : that.y < that.maxScrollY ? that.maxScrollY : that.y;
1066
1067 that.scroller.style[vendor + 'TransitionDuration'] = time + 'ms';
1068 that.scroller.style[vendor + 'Transform'] = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + scale + ')';
1069 that.zoomed = false;
1070 },
1071
1072 isReady: function () {
1073 return !this.moved && !this.zoomed && !this.animating;
1074 }
1075};
1076
1077if (typeof exports !== 'undefined') exports.iScroll = iScroll;
1078else window.iScroll = iScroll;
1079
1080})();
Note: See TracBrowser for help on using the repository browser.