2 * iScroll v4.1.9 ~ Copyright (c) 2011 Matteo Spinelli, http://cubiq.org
3 * Released under MIT license, http://cubiq.org/license
6 // should we even bother...???
7 Roo.namespace('Roo.ux');
11 vendor = (/webkit/i).test(navigator.appVersion) ? 'webkit' :
12 (/firefox/i).test(navigator.userAgent) ? 'Moz' :
13 'opera' in window ? 'O' : '',
15 // Browser capabilities
16 has3d = 'WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix(),
17 hasTouch = 'ontouchstart' in window,
18 hasTransform = vendor + 'Transform' in document.documentElement.style,
19 isAndroid = (/android/gi).test(navigator.appVersion),
20 isIDevice = (/iphone|ipad/gi).test(navigator.appVersion),
21 isPlaybook = (/playbook/gi).test(navigator.appVersion),
22 hasTransitionEnd = isIDevice || isPlaybook,
23 nextFrame = (function() {
24 return window.requestAnimationFrame
25 || window.webkitRequestAnimationFrame
26 || window.mozRequestAnimationFrame
27 || window.oRequestAnimationFrame
28 || window.msRequestAnimationFrame
29 || function(callback) { return setTimeout(callback, 1); }
31 cancelFrame = (function () {
32 return window.cancelRequestAnimationFrame
33 || window.webkitCancelRequestAnimationFrame
34 || window.mozCancelRequestAnimationFrame
35 || window.oCancelRequestAnimationFrame
36 || window.msCancelRequestAnimationFrame
41 RESIZE_EV = 'onorientationchange' in window ? 'orientationchange' : 'resize',
42 START_EV = hasTouch ? 'touchstart' : 'mousedown',
43 MOVE_EV = hasTouch ? 'touchmove' : 'mousemove',
44 END_EV = hasTouch ? 'touchend' : 'mouseup',
45 CANCEL_EV = hasTouch ? 'touchcancel' : 'mouseup',
46 WHEEL_EV = vendor == 'Moz' ? 'DOMMouseScroll' : 'mousewheel',
49 trnOpen = 'translate' + (has3d ? '3d(' : '('),
50 trnClose = has3d ? ',0)' : ')';
53 Roo.ux.iScroll = function (el, options) {
59 that.wrapper = typeof el == 'object' ? el : doc.getElementById(el);
61 that.wrapper.style.overflow = 'hidden';
62 that.scroller = that.wrapper.children[0];
78 checkDOMChanges: false, // Experimental
83 fixedScrollbar: isAndroid,
84 hideScrollbar: isIDevice,
85 fadeScrollbar: isIDevice && has3d,
93 wheelAction: 'scroll',
101 onBeforeScrollStart: function (e) { e.preventDefault(); },
103 onBeforeScrollMove: null,
105 onBeforeScrollEnd: null,
114 // User defined options
115 for (i in options) that.options[i] = options[i];
118 that.options.useTransform = hasTransform ? that.options.useTransform : false;
119 that.options.hScrollbar = that.options.hScroll && that.options.hScrollbar;
120 that.options.vScrollbar = that.options.vScroll && that.options.vScrollbar;
121 that.options.zoom = that.options.useTransform && that.options.zoom;
122 that.options.useTransition = hasTransitionEnd && that.options.useTransition;
124 // Set some default styles
125 that.scroller.style[vendor + 'TransitionProperty'] = that.options.useTransform ? '-' + vendor.toLowerCase() + '-transform' : 'top left';
126 that.scroller.style[vendor + 'TransitionDuration'] = '0';
127 that.scroller.style[vendor + 'TransformOrigin'] = '0 0';
128 if (that.options.useTransition) that.scroller.style[vendor + 'TransitionTimingFunction'] = 'cubic-bezier(0.33,0.66,0.66,1)';
130 if (that.options.useTransform) that.scroller.style[vendor + 'Transform'] = trnOpen + '0,0' + trnClose;
131 else that.scroller.style.cssText += ';position:absolute;top:0;left:0';
133 if (that.options.useTransition) that.options.fixedScrollbar = true;
137 that._bind(RESIZE_EV, window);
138 that._bind(START_EV);
140 that._bind('mouseout', that.wrapper);
141 that._bind(WHEEL_EV);
144 if (that.options.checkDOMChanges) that.checkDOMTime = setInterval(function () {
145 that._checkDOMChanges();
150 Roo.ux.iScroll.prototype = {
156 currPageX: 0, currPageY: 0,
157 pagesX: [], pagesY: [],
161 handleEvent: function (e) {
165 if (!hasTouch && e.button !== 0) return;
168 case MOVE_EV: that._move(e); break;
170 case CANCEL_EV: that._end(e); break;
171 case RESIZE_EV: that._resize(); break;
172 case WHEEL_EV: that._wheel(e); break;
173 case 'mouseout': that._mouseout(e); break;
174 case 'webkitTransitionEnd': that._transitionEnd(e); break;
178 _checkDOMChanges: function () {
179 if (this.moved || this.zoomed || this.animating ||
180 (this.scrollerW == this.scroller.offsetWidth * this.scale && this.scrollerH == this.scroller.offsetHeight * this.scale)) return;
185 _scrollbar: function (dir) {
190 if (!that[dir + 'Scrollbar']) {
191 if (that[dir + 'ScrollbarWrapper']) {
192 if (hasTransform) that[dir + 'ScrollbarIndicator'].style[vendor + 'Transform'] = '';
193 that[dir + 'ScrollbarWrapper'].parentNode.removeChild(that[dir + 'ScrollbarWrapper']);
194 that[dir + 'ScrollbarWrapper'] = null;
195 that[dir + 'ScrollbarIndicator'] = null;
201 if (!that[dir + 'ScrollbarWrapper']) {
202 // Create the scrollbar wrapper
203 bar = doc.createElement('div');
205 if (that.options.scrollbarClass) bar.className = that.options.scrollbarClass + dir.toUpperCase();
206 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');
208 bar.style.cssText += ';pointer-events:none;-' +
209 vendor + '-transition-property:opacity;-' +
210 vendor + '-transition-duration:' + (that.options.fadeScrollbar ? '350ms' : '0') +
211 ';overflow:hidden;opacity:' + (that.options.hideScrollbar ? '0' : '1');
213 that.wrapper.appendChild(bar);
214 that[dir + 'ScrollbarWrapper'] = bar;
216 // Create the scrollbar indicator
217 bar = doc.createElement('div');
218 if (!that.options.scrollbarClass) {
219 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';
221 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;
222 if (that.options.useTransition) bar.style.cssText += ';-' + vendor + '-transition-timing-function:cubic-bezier(0.33,0.66,0.66,1)';
224 that[dir + 'ScrollbarWrapper'].appendChild(bar);
225 that[dir + 'ScrollbarIndicator'] = bar;
229 that.hScrollbarSize = that.hScrollbarWrapper.clientWidth;
230 that.hScrollbarIndicatorSize = m.max(m.round(that.hScrollbarSize * that.hScrollbarSize / that.scrollerW), 8);
231 that.hScrollbarIndicator.style.width = that.hScrollbarIndicatorSize + 'px';
232 that.hScrollbarMaxScroll = that.hScrollbarSize - that.hScrollbarIndicatorSize;
233 that.hScrollbarProp = that.hScrollbarMaxScroll / that.maxScrollX;
235 that.vScrollbarSize = that.vScrollbarWrapper.clientHeight;
236 that.vScrollbarIndicatorSize = m.max(m.round(that.vScrollbarSize * that.vScrollbarSize / that.scrollerH), 8);
237 that.vScrollbarIndicator.style.height = that.vScrollbarIndicatorSize + 'px';
238 that.vScrollbarMaxScroll = that.vScrollbarSize - that.vScrollbarIndicatorSize;
239 that.vScrollbarProp = that.vScrollbarMaxScroll / that.maxScrollY;
243 that._scrollbarPos(dir, true);
246 _resize: function () {
248 setTimeout(function () { that.refresh(); }, isAndroid ? 200 : 0);
251 _pos: function (x, y) {
252 x = this.hScroll ? x : 0;
253 y = this.vScroll ? y : 0;
255 if (this.options.useTransform) {
256 this.scroller.style[vendor + 'Transform'] = trnOpen + x + 'px,' + y + 'px' + trnClose + ' scale(' + this.scale + ')';
260 this.scroller.style.left = x + 'px';
261 this.scroller.style.top = y + 'px';
267 this._scrollbarPos('h');
268 this._scrollbarPos('v');
271 _scrollbarPos: function (dir, hidden) {
273 pos = dir == 'h' ? that.x : that.y,
276 if (!that[dir + 'Scrollbar']) return;
278 pos = that[dir + 'ScrollbarProp'] * pos;
281 if (!that.options.fixedScrollbar) {
282 size = that[dir + 'ScrollbarIndicatorSize'] + m.round(pos * 3);
283 if (size < 8) size = 8;
284 that[dir + 'ScrollbarIndicator'].style[dir == 'h' ? 'width' : 'height'] = size + 'px';
287 } else if (pos > that[dir + 'ScrollbarMaxScroll']) {
288 if (!that.options.fixedScrollbar) {
289 size = that[dir + 'ScrollbarIndicatorSize'] - m.round((pos - that[dir + 'ScrollbarMaxScroll']) * 3);
290 if (size < 8) size = 8;
291 that[dir + 'ScrollbarIndicator'].style[dir == 'h' ? 'width' : 'height'] = size + 'px';
292 pos = that[dir + 'ScrollbarMaxScroll'] + (that[dir + 'ScrollbarIndicatorSize'] - size);
294 pos = that[dir + 'ScrollbarMaxScroll'];
298 that[dir + 'ScrollbarWrapper'].style[vendor + 'TransitionDelay'] = '0';
299 that[dir + 'ScrollbarWrapper'].style.opacity = hidden && that.options.hideScrollbar ? '0' : '1';
300 that[dir + 'ScrollbarIndicator'].style[vendor + 'Transform'] = trnOpen + (dir == 'h' ? pos + 'px,0' : '0,' + pos + 'px') + trnClose;
303 _start: function (e) {
305 point = hasTouch ? e.touches[0] : e,
309 if (!that.enabled) return;
311 if (that.options.onBeforeScrollStart) that.options.onBeforeScrollStart.call(that, e);
313 if (that.options.useTransition || that.options.zoom) that._transitionTime(0);
316 that.animating = false;
326 if (that.options.zoom && hasTouch && e.touches.length > 1) {
327 c1 = m.abs(e.touches[0].pageX-e.touches[1].pageX);
328 c2 = m.abs(e.touches[0].pageY-e.touches[1].pageY);
329 that.touchesDistStart = m.sqrt(c1 * c1 + c2 * c2);
331 that.originX = m.abs(e.touches[0].pageX + e.touches[1].pageX - that.wrapperOffsetLeft * 2) / 2 - that.x;
332 that.originY = m.abs(e.touches[0].pageY + e.touches[1].pageY - that.wrapperOffsetTop * 2) / 2 - that.y;
334 if (that.options.onZoomStart) that.options.onZoomStart.call(that, e);
337 if (that.options.momentum) {
338 if (that.options.useTransform) {
339 // Very lame general purpose alternative to CSSMatrix
340 matrix = getComputedStyle(that.scroller, null)[vendor + 'Transform'].replace(/[^0-9-.,]/g, '').split(',');
344 x = getComputedStyle(that.scroller, null).left.replace(/[^0-9-]/g, '') * 1;
345 y = getComputedStyle(that.scroller, null).top.replace(/[^0-9-]/g, '') * 1;
348 if (x != that.x || y != that.y) {
349 if (that.options.useTransition) that._unbind('webkitTransitionEnd');
350 else cancelFrame(that.aniTime);
356 that.absStartX = that.x; // Needed by snap threshold
357 that.absStartY = that.y;
359 that.startX = that.x;
360 that.startY = that.y;
361 that.pointX = point.pageX;
362 that.pointY = point.pageY;
364 that.startTime = e.timeStamp || (new Date()).getTime();
366 if (that.options.onScrollStart) that.options.onScrollStart.call(that, e);
370 that._bind(CANCEL_EV);
373 _move: function (e) {
375 point = hasTouch ? e.touches[0] : e,
376 deltaX = point.pageX - that.pointX,
377 deltaY = point.pageY - that.pointY,
378 newX = that.x + deltaX,
379 newY = that.y + deltaY,
381 timestamp = e.timeStamp || (new Date()).getTime();
383 if (that.options.onBeforeScrollMove) that.options.onBeforeScrollMove.call(that, e);
386 if (that.options.zoom && hasTouch && e.touches.length > 1) {
387 c1 = m.abs(e.touches[0].pageX - e.touches[1].pageX);
388 c2 = m.abs(e.touches[0].pageY - e.touches[1].pageY);
389 that.touchesDist = m.sqrt(c1*c1+c2*c2);
393 scale = 1 / that.touchesDistStart * that.touchesDist * this.scale;
395 if (scale < that.options.zoomMin) scale = 0.5 * that.options.zoomMin * Math.pow(2.0, scale / that.options.zoomMin);
396 else if (scale > that.options.zoomMax) scale = 2.0 * that.options.zoomMax * Math.pow(0.5, that.options.zoomMax / scale);
398 that.lastScale = scale / this.scale;
400 newX = this.originX - this.originX * that.lastScale + this.x,
401 newY = this.originY - this.originY * that.lastScale + this.y;
403 this.scroller.style[vendor + 'Transform'] = trnOpen + newX + 'px,' + newY + 'px' + trnClose + ' scale(' + scale + ')';
405 if (that.options.onZoom) that.options.onZoom.call(that, e);
409 that.pointX = point.pageX;
410 that.pointY = point.pageY;
412 // Slow down if outside of the boundaries
413 if (newX > 0 || newX < that.maxScrollX) {
414 newX = that.options.bounce ? that.x + (deltaX / 2) : newX >= 0 || that.maxScrollX >= 0 ? 0 : that.maxScrollX;
416 if (newY > that.minScrollY || newY < that.maxScrollY) {
417 newY = that.options.bounce ? that.y + (deltaY / 2) : newY >= that.minScrollY || that.maxScrollY >= 0 ? that.minScrollY : that.maxScrollY;
420 if (that.absDistX < 6 && that.absDistY < 6) {
421 that.distX += deltaX;
422 that.distY += deltaY;
423 that.absDistX = m.abs(that.distX);
424 that.absDistY = m.abs(that.distY);
430 if (that.options.lockDirection) {
431 if (that.absDistX > that.absDistY + 5) {
434 } else if (that.absDistY > that.absDistX + 5) {
441 that._pos(newX, newY);
442 that.dirX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
443 that.dirY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
445 if (timestamp - that.startTime > 300) {
446 that.startTime = timestamp;
447 that.startX = that.x;
448 that.startY = that.y;
451 if (that.options.onScrollMove) that.options.onScrollMove.call(that, e);
455 if (hasTouch && e.touches.length != 0) return;
458 point = hasTouch ? e.changedTouches[0] : e,
460 momentumX = { dist:0, time:0 },
461 momentumY = { dist:0, time:0 },
462 duration = (e.timeStamp || (new Date()).getTime()) - that.startTime,
470 that._unbind(MOVE_EV);
471 that._unbind(END_EV);
472 that._unbind(CANCEL_EV);
474 if (that.options.onBeforeScrollEnd) that.options.onBeforeScrollEnd.call(that, e);
477 scale = that.scale * that.lastScale;
478 scale = Math.max(that.options.zoomMin, scale);
479 scale = Math.min(that.options.zoomMax, scale);
480 that.lastScale = scale / that.scale;
483 that.x = that.originX - that.originX * that.lastScale + that.x;
484 that.y = that.originY - that.originY * that.lastScale + that.y;
486 that.scroller.style[vendor + 'TransitionDuration'] = '200ms';
487 that.scroller.style[vendor + 'Transform'] = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + that.scale + ')';
492 if (that.options.onZoomEnd) that.options.onZoomEnd.call(that, e);
498 if (that.doubleTapTimer && that.options.zoom) {
500 clearTimeout(that.doubleTapTimer);
501 that.doubleTapTimer = null;
502 if (that.options.onZoomStart) that.options.onZoomStart.call(that, e);
503 that.zoom(that.pointX, that.pointY, that.scale == 1 ? that.options.doubleTapZoom : 1);
504 if (that.options.onZoomEnd) {
505 setTimeout(function() {
506 that.options.onZoomEnd.call(that, e);
507 }, 200); // 200 is default zoom duration
510 that.doubleTapTimer = setTimeout(function () {
511 that.doubleTapTimer = null;
513 // Find the last touched element
514 target = point.target;
515 while (target.nodeType != 1) target = target.parentNode;
517 if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA') {
518 ev = document.createEvent('MouseEvents');
519 ev.initMouseEvent('click', true, true, e.view, 1,
520 point.screenX, point.screenY, point.clientX, point.clientY,
521 e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
524 target.dispatchEvent(ev);
526 }, that.options.zoom ? 250 : 0);
532 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
536 if (duration < 300 && that.options.momentum) {
537 momentumX = newPosX ? that._momentum(newPosX - that.startX, duration, -that.x, that.scrollerW - that.wrapperW + that.x, that.options.bounce ? that.wrapperW : 0) : momentumX;
538 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;
540 newPosX = that.x + momentumX.dist;
541 newPosY = that.y + momentumY.dist;
543 if ((that.x > 0 && newPosX > 0) || (that.x < that.maxScrollX && newPosX < that.maxScrollX)) momentumX = { dist:0, time:0 };
544 if ((that.y > that.minScrollY && newPosY > that.minScrollY) || (that.y < that.maxScrollY && newPosY < that.maxScrollY)) momentumY = { dist:0, time:0 };
547 if (momentumX.dist || momentumY.dist) {
548 newDuration = m.max(m.max(momentumX.time, momentumY.time), 10);
550 // Do we need to snap?
551 if (that.options.snap) {
552 distX = newPosX - that.absStartX;
553 distY = newPosY - that.absStartY;
554 if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) { that.scrollTo(that.absStartX, that.absStartY, 200); }
556 snap = that._snap(newPosX, newPosY);
559 newDuration = m.max(snap.time, newDuration);
563 that.scrollTo(m.round(newPosX), m.round(newPosY), newDuration);
565 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
569 // Do we need to snap?
570 if (that.options.snap) {
571 distX = newPosX - that.absStartX;
572 distY = newPosY - that.absStartY;
573 if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) that.scrollTo(that.absStartX, that.absStartY, 200);
575 snap = that._snap(that.x, that.y);
576 if (snap.x != that.x || snap.y != that.y) that.scrollTo(snap.x, snap.y, snap.time);
579 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
584 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
587 _resetPos: function (time) {
589 resetX = that.x >= 0 ? 0 : that.x < that.maxScrollX ? that.maxScrollX : that.x,
590 resetY = that.y >= that.minScrollY || that.maxScrollY > 0 ? that.minScrollY : that.y < that.maxScrollY ? that.maxScrollY : that.y;
592 if (resetX == that.x && resetY == that.y) {
595 if (that.options.onScrollEnd) that.options.onScrollEnd.call(that); // Execute custom code on scroll end
598 if (that.hScrollbar && that.options.hideScrollbar) {
599 if (vendor == 'webkit') that.hScrollbarWrapper.style[vendor + 'TransitionDelay'] = '300ms';
600 that.hScrollbarWrapper.style.opacity = '0';
602 if (that.vScrollbar && that.options.hideScrollbar) {
603 if (vendor == 'webkit') that.vScrollbarWrapper.style[vendor + 'TransitionDelay'] = '300ms';
604 that.vScrollbarWrapper.style.opacity = '0';
610 that.scrollTo(resetX, resetY, time || 0);
613 _wheel: function (e) {
615 wheelDeltaX, wheelDeltaY,
619 if ('wheelDeltaX' in e) {
620 wheelDeltaX = e.wheelDeltaX / 12;
621 wheelDeltaY = e.wheelDeltaY / 12;
622 } else if ('detail' in e) {
623 wheelDeltaX = wheelDeltaY = -e.detail * 3;
625 wheelDeltaX = wheelDeltaY = -e.wheelDelta;
628 if (that.options.wheelAction == 'zoom') {
629 deltaScale = that.scale * Math.pow(2, 1/3 * (wheelDeltaY ? wheelDeltaY / Math.abs(wheelDeltaY) : 0));
630 if (deltaScale < that.options.zoomMin) deltaScale = that.options.zoomMin;
631 if (deltaScale > that.options.zoomMax) deltaScale = that.options.zoomMax;
633 if (deltaScale != that.scale) {
634 if (!that.wheelZoomCount && that.options.onZoomStart) that.options.onZoomStart.call(that, e);
635 that.wheelZoomCount++;
637 that.zoom(e.pageX, e.pageY, deltaScale, 400);
639 setTimeout(function() {
640 that.wheelZoomCount--;
641 if (!that.wheelZoomCount && that.options.onZoomEnd) that.options.onZoomEnd.call(that, e);
648 deltaX = that.x + wheelDeltaX;
649 deltaY = that.y + wheelDeltaY;
651 if (deltaX > 0) deltaX = 0;
652 else if (deltaX < that.maxScrollX) deltaX = that.maxScrollX;
654 if (deltaY > that.minScrollY) deltaY = that.minScrollY;
655 else if (deltaY < that.maxScrollY) deltaY = that.maxScrollY;
657 that.scrollTo(deltaX, deltaY, 0);
660 _mouseout: function (e) {
661 var t = e.relatedTarget;
668 while (t = t.parentNode) if (t == this.wrapper) return;
673 _transitionEnd: function (e) {
676 if (e.target != that.scroller) return;
678 that._unbind('webkitTransitionEnd');
689 _startAni: function () {
691 startX = that.x, startY = that.y,
692 startTime = (new Date).getTime(),
695 if (that.animating) return;
697 if (!that.steps.length) {
702 step = that.steps.shift();
704 if (step.x == startX && step.y == startY) step.time = 0;
706 that.animating = true;
709 if (that.options.useTransition) {
710 that._transitionTime(step.time);
711 that._pos(step.x, step.y);
712 that.animating = false;
713 if (step.time) that._bind('webkitTransitionEnd');
714 else that._resetPos(0);
718 (function animate () {
719 var now = (new Date).getTime(),
722 if (now >= startTime + step.time) {
723 that._pos(step.x, step.y);
724 that.animating = false;
725 if (that.options.onAnimationEnd) that.options.onAnimationEnd.call(that); // Execute custom code on animation end
730 now = (now - startTime) / step.time - 1;
731 easeOut = m.sqrt(1 - now * now);
732 newX = (step.x - startX) * easeOut + startX;
733 newY = (step.y - startY) * easeOut + startY;
734 that._pos(newX, newY);
735 if (that.animating) that.aniTime = nextFrame(animate);
739 _transitionTime: function (time) {
741 this.scroller.style[vendor + 'TransitionDuration'] = time;
742 if (this.hScrollbar) this.hScrollbarIndicator.style[vendor + 'TransitionDuration'] = time;
743 if (this.vScrollbar) this.vScrollbarIndicator.style[vendor + 'TransitionDuration'] = time;
746 _momentum: function (dist, time, maxDistUpper, maxDistLower, size) {
747 var deceleration = 0.0006,
748 speed = m.abs(dist) / time,
749 newDist = (speed * speed) / (2 * deceleration),
750 newTime = 0, outsideDist = 0;
752 // Proportinally reduce speed if we are outside of the boundaries
753 if (dist > 0 && newDist > maxDistUpper) {
754 outsideDist = size / (6 / (newDist / speed * deceleration));
755 maxDistUpper = maxDistUpper + outsideDist;
756 speed = speed * maxDistUpper / newDist;
757 newDist = maxDistUpper;
758 } else if (dist < 0 && newDist > maxDistLower) {
759 outsideDist = size / (6 / (newDist / speed * deceleration));
760 maxDistLower = maxDistLower + outsideDist;
761 speed = speed * maxDistLower / newDist;
762 newDist = maxDistLower;
765 newDist = newDist * (dist < 0 ? -1 : 1);
766 newTime = speed / deceleration;
768 return { dist: newDist, time: m.round(newTime) };
771 _offset: function (el) {
772 var left = -el.offsetLeft,
775 while (el = el.offsetParent) {
776 left -= el.offsetLeft;
780 if (el != this.wrapper) {
785 return { left: left, top: top };
788 _snap: function (x, y) {
795 page = that.pagesX.length - 1;
796 for (i=0, l=that.pagesX.length; i<l; i++) {
797 if (x >= that.pagesX[i]) {
802 if (page == that.currPageX && page > 0 && that.dirX < 0) page--;
803 x = that.pagesX[page];
804 sizeX = m.abs(x - that.pagesX[that.currPageX]);
805 sizeX = sizeX ? m.abs(that.x - x) / sizeX * 500 : 0;
806 that.currPageX = page;
809 page = that.pagesY.length-1;
810 for (i=0; i<page; i++) {
811 if (y >= that.pagesY[i]) {
816 if (page == that.currPageY && page > 0 && that.dirY < 0) page--;
817 y = that.pagesY[page];
818 sizeY = m.abs(y - that.pagesY[that.currPageY]);
819 sizeY = sizeY ? m.abs(that.y - y) / sizeY * 500 : 0;
820 that.currPageY = page;
822 // Snap with constant speed (proportional duration)
823 time = m.round(m.max(sizeX, sizeY)) || 200;
825 return { x: x, y: y, time: time };
828 _bind: function (type, el, bubble) {
829 (el || this.scroller).addEventListener(type, this, !!bubble);
832 _unbind: function (type, el, bubble) {
833 (el || this.scroller).removeEventListener(type, this, !!bubble);
842 destroy: function () {
845 that.scroller.style[vendor + 'Transform'] = '';
847 // Remove the scrollbars
848 that.hScrollbar = false;
849 that.vScrollbar = false;
850 that._scrollbar('h');
851 that._scrollbar('v');
853 // Remove the event listeners
854 that._unbind(RESIZE_EV, window);
855 that._unbind(START_EV);
856 that._unbind(MOVE_EV);
857 that._unbind(END_EV);
858 that._unbind(CANCEL_EV);
860 if (that.options.hasTouch) {
861 that._unbind('mouseout', that.wrapper);
862 that._unbind(WHEEL_EV);
865 if (that.options.useTransition) that._unbind('webkitTransitionEnd');
867 if (that.options.checkDOMChanges) clearInterval(that.checkDOMTime);
869 if (that.options.onDestroy) that.options.onDestroy.call(that);
872 refresh: function () {
880 if (that.scale < that.options.zoomMin) that.scale = that.options.zoomMin;
881 that.wrapperW = that.wrapper.clientWidth || 1;
882 that.wrapperH = that.wrapper.clientHeight || 1;
884 that.minScrollY = -that.options.topOffset || 0;
885 that.scrollerW = m.round(that.scroller.offsetWidth * that.scale);
886 that.scrollerH = m.round((that.scroller.offsetHeight + that.minScrollY) * that.scale);
887 that.maxScrollX = that.wrapperW - that.scrollerW;
888 that.maxScrollY = that.wrapperH - that.scrollerH + that.minScrollY;
892 if (that.options.onRefresh) that.options.onRefresh.call(that);
894 that.hScroll = that.options.hScroll && that.maxScrollX < 0;
895 that.vScroll = that.options.vScroll && (!that.options.bounceLock && !that.hScroll || that.scrollerH > that.wrapperH);
897 that.hScrollbar = that.hScroll && that.options.hScrollbar;
898 that.vScrollbar = that.vScroll && that.options.vScrollbar && that.scrollerH > that.wrapperH;
900 offset = that._offset(that.wrapper);
901 that.wrapperOffsetLeft = -offset.left;
902 that.wrapperOffsetTop = -offset.top;
905 if (typeof that.options.snap == 'string') {
908 els = that.scroller.querySelectorAll(that.options.snap);
909 for (i=0, l=els.length; i<l; i++) {
910 pos = that._offset(els[i]);
911 pos.left += that.wrapperOffsetLeft;
912 pos.top += that.wrapperOffsetTop;
913 that.pagesX[i] = pos.left < that.maxScrollX ? that.maxScrollX : pos.left * that.scale;
914 that.pagesY[i] = pos.top < that.maxScrollY ? that.maxScrollY : pos.top * that.scale;
916 } else if (that.options.snap) {
918 while (pos >= that.maxScrollX) {
919 that.pagesX[page] = pos;
920 pos = pos - that.wrapperW;
923 if (that.maxScrollX%that.wrapperW) that.pagesX[that.pagesX.length] = that.maxScrollX - that.pagesX[that.pagesX.length-1] + that.pagesX[that.pagesX.length-1];
928 while (pos >= that.maxScrollY) {
929 that.pagesY[page] = pos;
930 pos = pos - that.wrapperH;
933 if (that.maxScrollY%that.wrapperH) that.pagesY[that.pagesY.length] = that.maxScrollY - that.pagesY[that.pagesY.length-1] + that.pagesY[that.pagesY.length-1];
936 // Prepare the scrollbars
937 that._scrollbar('h');
938 that._scrollbar('v');
941 that.scroller.style[vendor + 'TransitionDuration'] = '0';
946 scrollTo: function (x, y, time, relative) {
953 if (!step.length) step = [{ x: x, y: y, time: time, relative: relative }];
955 for (i=0, l=step.length; i<l; i++) {
956 if (step[i].relative) { step[i].x = that.x - step[i].x; step[i].y = that.y - step[i].y; }
957 that.steps.push({ x: step[i].x, y: step[i].y, time: step[i].time || 0 });
963 scrollToElement: function (el, time) {
964 var that = this, pos;
965 el = el.nodeType ? el : that.scroller.querySelector(el);
968 pos = that._offset(el);
969 pos.left += that.wrapperOffsetLeft;
970 pos.top += that.wrapperOffsetTop;
972 pos.left = pos.left > 0 ? 0 : pos.left < that.maxScrollX ? that.maxScrollX : pos.left;
973 pos.top = pos.top > that.minScrollY ? that.minScrollY : pos.top < that.maxScrollY ? that.maxScrollY : pos.top;
974 time = time === undefined ? m.max(m.abs(pos.left)*2, m.abs(pos.top)*2) : time;
976 that.scrollTo(pos.left, pos.top, time);
979 scrollToPage: function (pageX, pageY, time) {
980 var that = this, x, y;
982 if (that.options.snap) {
983 pageX = pageX == 'next' ? that.currPageX+1 : pageX == 'prev' ? that.currPageX-1 : pageX;
984 pageY = pageY == 'next' ? that.currPageY+1 : pageY == 'prev' ? that.currPageY-1 : pageY;
986 pageX = pageX < 0 ? 0 : pageX > that.pagesX.length-1 ? that.pagesX.length-1 : pageX;
987 pageY = pageY < 0 ? 0 : pageY > that.pagesY.length-1 ? that.pagesY.length-1 : pageY;
989 that.currPageX = pageX;
990 that.currPageY = pageY;
991 x = that.pagesX[pageX];
992 y = that.pagesY[pageY];
994 x = -that.wrapperW * pageX;
995 y = -that.wrapperH * pageY;
996 if (x < that.maxScrollX) x = that.maxScrollX;
997 if (y < that.maxScrollY) y = that.maxScrollY;
1000 that.scrollTo(x, y, time || 400);
1003 disable: function () {
1006 this.enabled = false;
1008 // If disabled after touchstart we make sure that there are no left over events
1009 this._unbind(MOVE_EV);
1010 this._unbind(END_EV);
1011 this._unbind(CANCEL_EV);
1014 enable: function () {
1015 this.enabled = true;
1019 if (this.options.useTransition) this._unbind('webkitTransitionEnd');
1020 else cancelFrame(this.aniTime);
1023 this.animating = false;
1026 zoom: function (x, y, scale, time) {
1028 relScale = scale / that.scale;
1030 if (!that.options.useTransform) return;
1033 time = time === undefined ? 200 : time;
1034 x = x - that.wrapperOffsetLeft - that.x;
1035 y = y - that.wrapperOffsetTop - that.y;
1036 that.x = x - x * relScale + that.x;
1037 that.y = y - y * relScale + that.y;
1042 that.x = that.x > 0 ? 0 : that.x < that.maxScrollX ? that.maxScrollX : that.x;
1043 that.y = that.y > that.minScrollY ? that.minScrollY : that.y < that.maxScrollY ? that.maxScrollY : that.y;
1045 that.scroller.style[vendor + 'TransitionDuration'] = time + 'ms';
1046 that.scroller.style[vendor + 'Transform'] = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + scale + ')';
1047 that.zoomed = false;
1050 isReady: function () {
1051 return !this.moved && !this.zoomed && !this.animating;
1055 //if (typeof exports !== 'undefined') exports.iScroll = iScroll;
1056 //else window.iScroll = iScroll;