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) {
58 that.wrapper = typeof el == 'object' ? el : doc.getElementById(el);
60 var wrap = Roo.get(that.wrapper.children[0]);
63 //that.wrapper.style.overflow = 'hidden';
64 that.scroller = wrap.wrap();
77 checkDOMChanges: false, // Experimental
82 fixedScrollbar: isAndroid,
83 hideScrollbar: isIDevice,
84 fadeScrollbar: isIDevice && has3d,
92 wheelAction: 'scroll',
100 onBeforeScrollStart: function (e) { e.preventDefault(); },
102 onBeforeScrollMove: null,
104 onBeforeScrollEnd: null,
113 // User defined options
114 for (i in options) that.options[i] = options[i];
117 that.options.useTransform = hasTransform ? that.options.useTransform : false;
118 that.options.hScrollbar = that.options.hScroll && that.options.hScrollbar;
119 that.options.vScrollbar = that.options.vScroll && that.options.vScrollbar;
120 that.options.zoom = that.options.useTransform && that.options.zoom;
121 that.options.useTransition = hasTransitionEnd && that.options.useTransition;
123 // Set some default styles
124 that.scroller.style[vendor + 'TransitionProperty'] = that.options.useTransform ? '-' + vendor.toLowerCase() + '-transform' : 'top left';
125 that.scroller.style[vendor + 'TransitionDuration'] = '0';
126 that.scroller.style[vendor + 'TransformOrigin'] = '0 0';
127 if (that.options.useTransition) that.scroller.style[vendor + 'TransitionTimingFunction'] = 'cubic-bezier(0.33,0.66,0.66,1)';
129 if (that.options.useTransform) that.scroller.style[vendor + 'Transform'] = trnOpen + '0,0' + trnClose;
130 else that.scroller.style.cssText += ';position:absolute;top:0;left:0';
132 if (that.options.useTransition) that.options.fixedScrollbar = true;
136 that._bind(RESIZE_EV, window);
137 that._bind(START_EV);
139 that._bind('mouseout', that.wrapper);
140 that._bind(WHEEL_EV);
143 if (that.options.checkDOMChanges) that.checkDOMTime = setInterval(function () {
144 that._checkDOMChanges();
149 Roo.ux.iScroll.prototype = {
155 currPageX: 0, currPageY: 0,
156 pagesX: [], pagesY: [],
160 handleEvent: function (e) {
164 if (!hasTouch && e.button !== 0) return;
167 case MOVE_EV: that._move(e); break;
169 case CANCEL_EV: that._end(e); break;
170 case RESIZE_EV: that._resize(); break;
171 case WHEEL_EV: that._wheel(e); break;
172 case 'mouseout': that._mouseout(e); break;
173 case 'webkitTransitionEnd': that._transitionEnd(e); break;
177 _checkDOMChanges: function () {
178 if (this.moved || this.zoomed || this.animating ||
179 (this.scrollerW == this.scroller.offsetWidth * this.scale && this.scrollerH == this.scroller.offsetHeight * this.scale)) return;
184 _scrollbar: function (dir) {
189 if (!that[dir + 'Scrollbar']) {
190 if (that[dir + 'ScrollbarWrapper']) {
191 if (hasTransform) that[dir + 'ScrollbarIndicator'].style[vendor + 'Transform'] = '';
192 that[dir + 'ScrollbarWrapper'].parentNode.removeChild(that[dir + 'ScrollbarWrapper']);
193 that[dir + 'ScrollbarWrapper'] = null;
194 that[dir + 'ScrollbarIndicator'] = null;
200 if (!that[dir + 'ScrollbarWrapper']) {
201 // Create the scrollbar wrapper
202 bar = doc.createElement('div');
204 if (that.options.scrollbarClass) bar.className = that.options.scrollbarClass + dir.toUpperCase();
205 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');
207 bar.style.cssText += ';pointer-events:none;-' +
208 vendor + '-transition-property:opacity;-' +
209 vendor + '-transition-duration:' + (that.options.fadeScrollbar ? '350ms' : '0') +
210 ';overflow:hidden;opacity:' + (that.options.hideScrollbar ? '0' : '1');
212 that.wrapper.appendChild(bar);
213 that[dir + 'ScrollbarWrapper'] = bar;
215 // Create the scrollbar indicator
216 bar = doc.createElement('div');
217 if (!that.options.scrollbarClass) {
218 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';
220 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;
221 if (that.options.useTransition) bar.style.cssText += ';-' + vendor + '-transition-timing-function:cubic-bezier(0.33,0.66,0.66,1)';
223 that[dir + 'ScrollbarWrapper'].appendChild(bar);
224 that[dir + 'ScrollbarIndicator'] = bar;
228 that.hScrollbarSize = that.hScrollbarWrapper.clientWidth;
229 that.hScrollbarIndicatorSize = m.max(m.round(that.hScrollbarSize * that.hScrollbarSize / that.scrollerW), 8);
230 that.hScrollbarIndicator.style.width = that.hScrollbarIndicatorSize + 'px';
231 that.hScrollbarMaxScroll = that.hScrollbarSize - that.hScrollbarIndicatorSize;
232 that.hScrollbarProp = that.hScrollbarMaxScroll / that.maxScrollX;
234 that.vScrollbarSize = that.vScrollbarWrapper.clientHeight;
235 that.vScrollbarIndicatorSize = m.max(m.round(that.vScrollbarSize * that.vScrollbarSize / that.scrollerH), 8);
236 that.vScrollbarIndicator.style.height = that.vScrollbarIndicatorSize + 'px';
237 that.vScrollbarMaxScroll = that.vScrollbarSize - that.vScrollbarIndicatorSize;
238 that.vScrollbarProp = that.vScrollbarMaxScroll / that.maxScrollY;
242 that._scrollbarPos(dir, true);
245 _resize: function () {
247 setTimeout(function () { that.refresh(); }, isAndroid ? 200 : 0);
250 _pos: function (x, y) {
251 x = this.hScroll ? x : 0;
252 y = this.vScroll ? y : 0;
254 if (this.options.useTransform) {
255 this.scroller.style[vendor + 'Transform'] = trnOpen + x + 'px,' + y + 'px' + trnClose + ' scale(' + this.scale + ')';
259 this.scroller.style.left = x + 'px';
260 this.scroller.style.top = y + 'px';
266 this._scrollbarPos('h');
267 this._scrollbarPos('v');
270 _scrollbarPos: function (dir, hidden) {
272 pos = dir == 'h' ? that.x : that.y,
275 if (!that[dir + 'Scrollbar']) return;
277 pos = that[dir + 'ScrollbarProp'] * pos;
280 if (!that.options.fixedScrollbar) {
281 size = that[dir + 'ScrollbarIndicatorSize'] + m.round(pos * 3);
282 if (size < 8) size = 8;
283 that[dir + 'ScrollbarIndicator'].style[dir == 'h' ? 'width' : 'height'] = size + 'px';
286 } else if (pos > that[dir + 'ScrollbarMaxScroll']) {
287 if (!that.options.fixedScrollbar) {
288 size = that[dir + 'ScrollbarIndicatorSize'] - m.round((pos - that[dir + 'ScrollbarMaxScroll']) * 3);
289 if (size < 8) size = 8;
290 that[dir + 'ScrollbarIndicator'].style[dir == 'h' ? 'width' : 'height'] = size + 'px';
291 pos = that[dir + 'ScrollbarMaxScroll'] + (that[dir + 'ScrollbarIndicatorSize'] - size);
293 pos = that[dir + 'ScrollbarMaxScroll'];
297 that[dir + 'ScrollbarWrapper'].style[vendor + 'TransitionDelay'] = '0';
298 that[dir + 'ScrollbarWrapper'].style.opacity = hidden && that.options.hideScrollbar ? '0' : '1';
299 that[dir + 'ScrollbarIndicator'].style[vendor + 'Transform'] = trnOpen + (dir == 'h' ? pos + 'px,0' : '0,' + pos + 'px') + trnClose;
302 _start: function (e) {
304 point = hasTouch ? e.touches[0] : e,
308 if (!that.enabled) return;
310 if (that.options.onBeforeScrollStart) that.options.onBeforeScrollStart.call(that, e);
312 if (that.options.useTransition || that.options.zoom) that._transitionTime(0);
315 that.animating = false;
325 if (that.options.zoom && hasTouch && e.touches.length > 1) {
326 c1 = m.abs(e.touches[0].pageX-e.touches[1].pageX);
327 c2 = m.abs(e.touches[0].pageY-e.touches[1].pageY);
328 that.touchesDistStart = m.sqrt(c1 * c1 + c2 * c2);
330 that.originX = m.abs(e.touches[0].pageX + e.touches[1].pageX - that.wrapperOffsetLeft * 2) / 2 - that.x;
331 that.originY = m.abs(e.touches[0].pageY + e.touches[1].pageY - that.wrapperOffsetTop * 2) / 2 - that.y;
333 if (that.options.onZoomStart) that.options.onZoomStart.call(that, e);
336 if (that.options.momentum) {
337 if (that.options.useTransform) {
338 // Very lame general purpose alternative to CSSMatrix
339 matrix = getComputedStyle(that.scroller, null)[vendor + 'Transform'].replace(/[^0-9-.,]/g, '').split(',');
343 x = getComputedStyle(that.scroller, null).left.replace(/[^0-9-]/g, '') * 1;
344 y = getComputedStyle(that.scroller, null).top.replace(/[^0-9-]/g, '') * 1;
347 if (x != that.x || y != that.y) {
348 if (that.options.useTransition) that._unbind('webkitTransitionEnd');
349 else cancelFrame(that.aniTime);
355 that.absStartX = that.x; // Needed by snap threshold
356 that.absStartY = that.y;
358 that.startX = that.x;
359 that.startY = that.y;
360 that.pointX = point.pageX;
361 that.pointY = point.pageY;
363 that.startTime = e.timeStamp || (new Date()).getTime();
365 if (that.options.onScrollStart) that.options.onScrollStart.call(that, e);
369 that._bind(CANCEL_EV);
372 _move: function (e) {
374 point = hasTouch ? e.touches[0] : e,
375 deltaX = point.pageX - that.pointX,
376 deltaY = point.pageY - that.pointY,
377 newX = that.x + deltaX,
378 newY = that.y + deltaY,
380 timestamp = e.timeStamp || (new Date()).getTime();
382 if (that.options.onBeforeScrollMove) that.options.onBeforeScrollMove.call(that, e);
385 if (that.options.zoom && hasTouch && e.touches.length > 1) {
386 c1 = m.abs(e.touches[0].pageX - e.touches[1].pageX);
387 c2 = m.abs(e.touches[0].pageY - e.touches[1].pageY);
388 that.touchesDist = m.sqrt(c1*c1+c2*c2);
392 scale = 1 / that.touchesDistStart * that.touchesDist * this.scale;
394 if (scale < that.options.zoomMin) scale = 0.5 * that.options.zoomMin * Math.pow(2.0, scale / that.options.zoomMin);
395 else if (scale > that.options.zoomMax) scale = 2.0 * that.options.zoomMax * Math.pow(0.5, that.options.zoomMax / scale);
397 that.lastScale = scale / this.scale;
399 newX = this.originX - this.originX * that.lastScale + this.x,
400 newY = this.originY - this.originY * that.lastScale + this.y;
402 this.scroller.style[vendor + 'Transform'] = trnOpen + newX + 'px,' + newY + 'px' + trnClose + ' scale(' + scale + ')';
404 if (that.options.onZoom) that.options.onZoom.call(that, e);
408 that.pointX = point.pageX;
409 that.pointY = point.pageY;
411 // Slow down if outside of the boundaries
412 if (newX > 0 || newX < that.maxScrollX) {
413 newX = that.options.bounce ? that.x + (deltaX / 2) : newX >= 0 || that.maxScrollX >= 0 ? 0 : that.maxScrollX;
415 if (newY > that.minScrollY || newY < that.maxScrollY) {
416 newY = that.options.bounce ? that.y + (deltaY / 2) : newY >= that.minScrollY || that.maxScrollY >= 0 ? that.minScrollY : that.maxScrollY;
419 if (that.absDistX < 6 && that.absDistY < 6) {
420 that.distX += deltaX;
421 that.distY += deltaY;
422 that.absDistX = m.abs(that.distX);
423 that.absDistY = m.abs(that.distY);
429 if (that.options.lockDirection) {
430 if (that.absDistX > that.absDistY + 5) {
433 } else if (that.absDistY > that.absDistX + 5) {
440 that._pos(newX, newY);
441 that.dirX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
442 that.dirY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
444 if (timestamp - that.startTime > 300) {
445 that.startTime = timestamp;
446 that.startX = that.x;
447 that.startY = that.y;
450 if (that.options.onScrollMove) that.options.onScrollMove.call(that, e);
454 if (hasTouch && e.touches.length != 0) return;
457 point = hasTouch ? e.changedTouches[0] : e,
459 momentumX = { dist:0, time:0 },
460 momentumY = { dist:0, time:0 },
461 duration = (e.timeStamp || (new Date()).getTime()) - that.startTime,
469 that._unbind(MOVE_EV);
470 that._unbind(END_EV);
471 that._unbind(CANCEL_EV);
473 if (that.options.onBeforeScrollEnd) that.options.onBeforeScrollEnd.call(that, e);
476 scale = that.scale * that.lastScale;
477 scale = Math.max(that.options.zoomMin, scale);
478 scale = Math.min(that.options.zoomMax, scale);
479 that.lastScale = scale / that.scale;
482 that.x = that.originX - that.originX * that.lastScale + that.x;
483 that.y = that.originY - that.originY * that.lastScale + that.y;
485 that.scroller.style[vendor + 'TransitionDuration'] = '200ms';
486 that.scroller.style[vendor + 'Transform'] = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + that.scale + ')';
491 if (that.options.onZoomEnd) that.options.onZoomEnd.call(that, e);
497 if (that.doubleTapTimer && that.options.zoom) {
499 clearTimeout(that.doubleTapTimer);
500 that.doubleTapTimer = null;
501 if (that.options.onZoomStart) that.options.onZoomStart.call(that, e);
502 that.zoom(that.pointX, that.pointY, that.scale == 1 ? that.options.doubleTapZoom : 1);
503 if (that.options.onZoomEnd) {
504 setTimeout(function() {
505 that.options.onZoomEnd.call(that, e);
506 }, 200); // 200 is default zoom duration
509 that.doubleTapTimer = setTimeout(function () {
510 that.doubleTapTimer = null;
512 // Find the last touched element
513 target = point.target;
514 while (target.nodeType != 1) target = target.parentNode;
516 if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA') {
517 ev = document.createEvent('MouseEvents');
518 ev.initMouseEvent('click', true, true, e.view, 1,
519 point.screenX, point.screenY, point.clientX, point.clientY,
520 e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
523 target.dispatchEvent(ev);
525 }, that.options.zoom ? 250 : 0);
531 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
535 if (duration < 300 && that.options.momentum) {
536 momentumX = newPosX ? that._momentum(newPosX - that.startX, duration, -that.x, that.scrollerW - that.wrapperW + that.x, that.options.bounce ? that.wrapperW : 0) : momentumX;
537 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;
539 newPosX = that.x + momentumX.dist;
540 newPosY = that.y + momentumY.dist;
542 if ((that.x > 0 && newPosX > 0) || (that.x < that.maxScrollX && newPosX < that.maxScrollX)) momentumX = { dist:0, time:0 };
543 if ((that.y > that.minScrollY && newPosY > that.minScrollY) || (that.y < that.maxScrollY && newPosY < that.maxScrollY)) momentumY = { dist:0, time:0 };
546 if (momentumX.dist || momentumY.dist) {
547 newDuration = m.max(m.max(momentumX.time, momentumY.time), 10);
549 // Do we need to snap?
550 if (that.options.snap) {
551 distX = newPosX - that.absStartX;
552 distY = newPosY - that.absStartY;
553 if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) { that.scrollTo(that.absStartX, that.absStartY, 200); }
555 snap = that._snap(newPosX, newPosY);
558 newDuration = m.max(snap.time, newDuration);
562 that.scrollTo(m.round(newPosX), m.round(newPosY), newDuration);
564 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
568 // Do we need to snap?
569 if (that.options.snap) {
570 distX = newPosX - that.absStartX;
571 distY = newPosY - that.absStartY;
572 if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) that.scrollTo(that.absStartX, that.absStartY, 200);
574 snap = that._snap(that.x, that.y);
575 if (snap.x != that.x || snap.y != that.y) that.scrollTo(snap.x, snap.y, snap.time);
578 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
583 if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
586 _resetPos: function (time) {
588 resetX = that.x >= 0 ? 0 : that.x < that.maxScrollX ? that.maxScrollX : that.x,
589 resetY = that.y >= that.minScrollY || that.maxScrollY > 0 ? that.minScrollY : that.y < that.maxScrollY ? that.maxScrollY : that.y;
591 if (resetX == that.x && resetY == that.y) {
594 if (that.options.onScrollEnd) that.options.onScrollEnd.call(that); // Execute custom code on scroll end
597 if (that.hScrollbar && that.options.hideScrollbar) {
598 if (vendor == 'webkit') that.hScrollbarWrapper.style[vendor + 'TransitionDelay'] = '300ms';
599 that.hScrollbarWrapper.style.opacity = '0';
601 if (that.vScrollbar && that.options.hideScrollbar) {
602 if (vendor == 'webkit') that.vScrollbarWrapper.style[vendor + 'TransitionDelay'] = '300ms';
603 that.vScrollbarWrapper.style.opacity = '0';
609 that.scrollTo(resetX, resetY, time || 0);
612 _wheel: function (e) {
614 wheelDeltaX, wheelDeltaY,
618 if ('wheelDeltaX' in e) {
619 wheelDeltaX = e.wheelDeltaX / 12;
620 wheelDeltaY = e.wheelDeltaY / 12;
621 } else if ('detail' in e) {
622 wheelDeltaX = wheelDeltaY = -e.detail * 3;
624 wheelDeltaX = wheelDeltaY = -e.wheelDelta;
627 if (that.options.wheelAction == 'zoom') {
628 deltaScale = that.scale * Math.pow(2, 1/3 * (wheelDeltaY ? wheelDeltaY / Math.abs(wheelDeltaY) : 0));
629 if (deltaScale < that.options.zoomMin) deltaScale = that.options.zoomMin;
630 if (deltaScale > that.options.zoomMax) deltaScale = that.options.zoomMax;
632 if (deltaScale != that.scale) {
633 if (!that.wheelZoomCount && that.options.onZoomStart) that.options.onZoomStart.call(that, e);
634 that.wheelZoomCount++;
636 that.zoom(e.pageX, e.pageY, deltaScale, 400);
638 setTimeout(function() {
639 that.wheelZoomCount--;
640 if (!that.wheelZoomCount && that.options.onZoomEnd) that.options.onZoomEnd.call(that, e);
647 deltaX = that.x + wheelDeltaX;
648 deltaY = that.y + wheelDeltaY;
650 if (deltaX > 0) deltaX = 0;
651 else if (deltaX < that.maxScrollX) deltaX = that.maxScrollX;
653 if (deltaY > that.minScrollY) deltaY = that.minScrollY;
654 else if (deltaY < that.maxScrollY) deltaY = that.maxScrollY;
656 that.scrollTo(deltaX, deltaY, 0);
659 _mouseout: function (e) {
660 var t = e.relatedTarget;
667 while (t = t.parentNode) if (t == this.wrapper) return;
672 _transitionEnd: function (e) {
675 if (e.target != that.scroller) return;
677 that._unbind('webkitTransitionEnd');
688 _startAni: function () {
690 startX = that.x, startY = that.y,
691 startTime = (new Date).getTime(),
694 if (that.animating) return;
696 if (!that.steps.length) {
701 step = that.steps.shift();
703 if (step.x == startX && step.y == startY) step.time = 0;
705 that.animating = true;
708 if (that.options.useTransition) {
709 that._transitionTime(step.time);
710 that._pos(step.x, step.y);
711 that.animating = false;
712 if (step.time) that._bind('webkitTransitionEnd');
713 else that._resetPos(0);
717 (function animate () {
718 var now = (new Date).getTime(),
721 if (now >= startTime + step.time) {
722 that._pos(step.x, step.y);
723 that.animating = false;
724 if (that.options.onAnimationEnd) that.options.onAnimationEnd.call(that); // Execute custom code on animation end
729 now = (now - startTime) / step.time - 1;
730 easeOut = m.sqrt(1 - now * now);
731 newX = (step.x - startX) * easeOut + startX;
732 newY = (step.y - startY) * easeOut + startY;
733 that._pos(newX, newY);
734 if (that.animating) that.aniTime = nextFrame(animate);
738 _transitionTime: function (time) {
740 this.scroller.style[vendor + 'TransitionDuration'] = time;
741 if (this.hScrollbar) this.hScrollbarIndicator.style[vendor + 'TransitionDuration'] = time;
742 if (this.vScrollbar) this.vScrollbarIndicator.style[vendor + 'TransitionDuration'] = time;
745 _momentum: function (dist, time, maxDistUpper, maxDistLower, size) {
746 var deceleration = 0.0006,
747 speed = m.abs(dist) / time,
748 newDist = (speed * speed) / (2 * deceleration),
749 newTime = 0, outsideDist = 0;
751 // Proportinally reduce speed if we are outside of the boundaries
752 if (dist > 0 && newDist > maxDistUpper) {
753 outsideDist = size / (6 / (newDist / speed * deceleration));
754 maxDistUpper = maxDistUpper + outsideDist;
755 speed = speed * maxDistUpper / newDist;
756 newDist = maxDistUpper;
757 } else if (dist < 0 && newDist > maxDistLower) {
758 outsideDist = size / (6 / (newDist / speed * deceleration));
759 maxDistLower = maxDistLower + outsideDist;
760 speed = speed * maxDistLower / newDist;
761 newDist = maxDistLower;
764 newDist = newDist * (dist < 0 ? -1 : 1);
765 newTime = speed / deceleration;
767 return { dist: newDist, time: m.round(newTime) };
770 _offset: function (el) {
771 var left = -el.offsetLeft,
774 while (el = el.offsetParent) {
775 left -= el.offsetLeft;
779 if (el != this.wrapper) {
784 return { left: left, top: top };
787 _snap: function (x, y) {
794 page = that.pagesX.length - 1;
795 for (i=0, l=that.pagesX.length; i<l; i++) {
796 if (x >= that.pagesX[i]) {
801 if (page == that.currPageX && page > 0 && that.dirX < 0) page--;
802 x = that.pagesX[page];
803 sizeX = m.abs(x - that.pagesX[that.currPageX]);
804 sizeX = sizeX ? m.abs(that.x - x) / sizeX * 500 : 0;
805 that.currPageX = page;
808 page = that.pagesY.length-1;
809 for (i=0; i<page; i++) {
810 if (y >= that.pagesY[i]) {
815 if (page == that.currPageY && page > 0 && that.dirY < 0) page--;
816 y = that.pagesY[page];
817 sizeY = m.abs(y - that.pagesY[that.currPageY]);
818 sizeY = sizeY ? m.abs(that.y - y) / sizeY * 500 : 0;
819 that.currPageY = page;
821 // Snap with constant speed (proportional duration)
822 time = m.round(m.max(sizeX, sizeY)) || 200;
824 return { x: x, y: y, time: time };
827 _bind: function (type, el, bubble) {
828 (el || this.scroller).addEventListener(type, this, !!bubble);
831 _unbind: function (type, el, bubble) {
832 (el || this.scroller).removeEventListener(type, this, !!bubble);
841 destroy: function () {
844 that.scroller.style[vendor + 'Transform'] = '';
846 // Remove the scrollbars
847 that.hScrollbar = false;
848 that.vScrollbar = false;
849 that._scrollbar('h');
850 that._scrollbar('v');
852 // Remove the event listeners
853 that._unbind(RESIZE_EV, window);
854 that._unbind(START_EV);
855 that._unbind(MOVE_EV);
856 that._unbind(END_EV);
857 that._unbind(CANCEL_EV);
859 if (that.options.hasTouch) {
860 that._unbind('mouseout', that.wrapper);
861 that._unbind(WHEEL_EV);
864 if (that.options.useTransition) that._unbind('webkitTransitionEnd');
866 if (that.options.checkDOMChanges) clearInterval(that.checkDOMTime);
868 if (that.options.onDestroy) that.options.onDestroy.call(that);
871 refresh: function () {
879 if (that.scale < that.options.zoomMin) that.scale = that.options.zoomMin;
880 that.wrapperW = that.wrapper.clientWidth || 1;
881 that.wrapperH = that.wrapper.clientHeight || 1;
883 that.minScrollY = -that.options.topOffset || 0;
884 that.scrollerW = m.round(that.scroller.offsetWidth * that.scale);
885 that.scrollerH = m.round((that.scroller.offsetHeight + that.minScrollY) * that.scale);
886 that.maxScrollX = that.wrapperW - that.scrollerW;
887 that.maxScrollY = that.wrapperH - that.scrollerH + that.minScrollY;
891 if (that.options.onRefresh) that.options.onRefresh.call(that);
893 that.hScroll = that.options.hScroll && that.maxScrollX < 0;
894 that.vScroll = that.options.vScroll && (!that.options.bounceLock && !that.hScroll || that.scrollerH > that.wrapperH);
896 that.hScrollbar = that.hScroll && that.options.hScrollbar;
897 that.vScrollbar = that.vScroll && that.options.vScrollbar && that.scrollerH > that.wrapperH;
899 offset = that._offset(that.wrapper);
900 that.wrapperOffsetLeft = -offset.left;
901 that.wrapperOffsetTop = -offset.top;
904 if (typeof that.options.snap == 'string') {
907 els = that.scroller.querySelectorAll(that.options.snap);
908 for (i=0, l=els.length; i<l; i++) {
909 pos = that._offset(els[i]);
910 pos.left += that.wrapperOffsetLeft;
911 pos.top += that.wrapperOffsetTop;
912 that.pagesX[i] = pos.left < that.maxScrollX ? that.maxScrollX : pos.left * that.scale;
913 that.pagesY[i] = pos.top < that.maxScrollY ? that.maxScrollY : pos.top * that.scale;
915 } else if (that.options.snap) {
917 while (pos >= that.maxScrollX) {
918 that.pagesX[page] = pos;
919 pos = pos - that.wrapperW;
922 if (that.maxScrollX%that.wrapperW) that.pagesX[that.pagesX.length] = that.maxScrollX - that.pagesX[that.pagesX.length-1] + that.pagesX[that.pagesX.length-1];
927 while (pos >= that.maxScrollY) {
928 that.pagesY[page] = pos;
929 pos = pos - that.wrapperH;
932 if (that.maxScrollY%that.wrapperH) that.pagesY[that.pagesY.length] = that.maxScrollY - that.pagesY[that.pagesY.length-1] + that.pagesY[that.pagesY.length-1];
935 // Prepare the scrollbars
936 that._scrollbar('h');
937 that._scrollbar('v');
940 that.scroller.style[vendor + 'TransitionDuration'] = '0';
945 scrollTo: function (x, y, time, relative) {
952 if (!step.length) step = [{ x: x, y: y, time: time, relative: relative }];
954 for (i=0, l=step.length; i<l; i++) {
955 if (step[i].relative) { step[i].x = that.x - step[i].x; step[i].y = that.y - step[i].y; }
956 that.steps.push({ x: step[i].x, y: step[i].y, time: step[i].time || 0 });
962 scrollToElement: function (el, time) {
963 var that = this, pos;
964 el = el.nodeType ? el : that.scroller.querySelector(el);
967 pos = that._offset(el);
968 pos.left += that.wrapperOffsetLeft;
969 pos.top += that.wrapperOffsetTop;
971 pos.left = pos.left > 0 ? 0 : pos.left < that.maxScrollX ? that.maxScrollX : pos.left;
972 pos.top = pos.top > that.minScrollY ? that.minScrollY : pos.top < that.maxScrollY ? that.maxScrollY : pos.top;
973 time = time === undefined ? m.max(m.abs(pos.left)*2, m.abs(pos.top)*2) : time;
975 that.scrollTo(pos.left, pos.top, time);
978 scrollToPage: function (pageX, pageY, time) {
979 var that = this, x, y;
981 if (that.options.snap) {
982 pageX = pageX == 'next' ? that.currPageX+1 : pageX == 'prev' ? that.currPageX-1 : pageX;
983 pageY = pageY == 'next' ? that.currPageY+1 : pageY == 'prev' ? that.currPageY-1 : pageY;
985 pageX = pageX < 0 ? 0 : pageX > that.pagesX.length-1 ? that.pagesX.length-1 : pageX;
986 pageY = pageY < 0 ? 0 : pageY > that.pagesY.length-1 ? that.pagesY.length-1 : pageY;
988 that.currPageX = pageX;
989 that.currPageY = pageY;
990 x = that.pagesX[pageX];
991 y = that.pagesY[pageY];
993 x = -that.wrapperW * pageX;
994 y = -that.wrapperH * pageY;
995 if (x < that.maxScrollX) x = that.maxScrollX;
996 if (y < that.maxScrollY) y = that.maxScrollY;
999 that.scrollTo(x, y, time || 400);
1002 disable: function () {
1005 this.enabled = false;
1007 // If disabled after touchstart we make sure that there are no left over events
1008 this._unbind(MOVE_EV);
1009 this._unbind(END_EV);
1010 this._unbind(CANCEL_EV);
1013 enable: function () {
1014 this.enabled = true;
1018 if (this.options.useTransition) this._unbind('webkitTransitionEnd');
1019 else cancelFrame(this.aniTime);
1022 this.animating = false;
1025 zoom: function (x, y, scale, time) {
1027 relScale = scale / that.scale;
1029 if (!that.options.useTransform) return;
1032 time = time === undefined ? 200 : time;
1033 x = x - that.wrapperOffsetLeft - that.x;
1034 y = y - that.wrapperOffsetTop - that.y;
1035 that.x = x - x * relScale + that.x;
1036 that.y = y - y * relScale + that.y;
1041 that.x = that.x > 0 ? 0 : that.x < that.maxScrollX ? that.maxScrollX : that.x;
1042 that.y = that.y > that.minScrollY ? that.minScrollY : that.y < that.maxScrollY ? that.maxScrollY : that.y;
1044 that.scroller.style[vendor + 'TransitionDuration'] = time + 'ms';
1045 that.scroller.style[vendor + 'Transform'] = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + scale + ')';
1046 that.zoomed = false;
1049 isReady: function () {
1050 return !this.moved && !this.zoomed && !this.animating;
1054 //if (typeof exports !== 'undefined') exports.iScroll = iScroll;
1055 //else window.iScroll = iScroll;