3 * Find more about the scrolling function at
4 * http://cubiq.org/iscroll
6 * Copyright (c) 2010 Matteo Spinelli, http://cubiq.org/
7 * Released under MIT license
8 * http://cubiq.org/dropbox/mit-license.txt
10 * Version 3.6 - Last updated: 2010.08.24
14 Roo.namespace('Roo.ux');
16 Roo.ux.Iscroll = function(el, options)
19 that.element = typeof el == 'object' ? el : document.getElementById(el);
20 that.wrapper = that.element.parentNode;
22 that.element.style.webkitTransitionProperty = '-webkit-transform';
23 that.element.style.webkitTransitionTimingFunction = 'cubic-bezier(0,0,0.25,1)';
24 that.element.style.webkitTransitionDuration = '0';
25 that.element.style.webkitTransform = Roo.ux.Iscroll.translateOpen + '0,0' + Roo.ux.Iscroll.translateClose;
29 bounce: Roo.ux.Iscroll.has3d,
30 momentum: Roo.ux.Iscroll.has3d,
31 checkDOMChanges: true,
32 topOnDOMChanges: false,
33 hScrollbar: Roo.ux.Iscroll.has3d,
34 vScrollbar: Roo.ux.Iscroll.has3d,
35 fadeScrollbar: Roo.ux.Iscroll.isIphone || Roo.ux.Iscroll.isIpad || !Roo.ux.Iscroll.isTouch,
36 shrinkScrollbar: Roo.ux.Iscroll.isIphone || Roo.ux.Iscroll.isIpad || !Roo.ux.Iscroll.isTouch,
37 desktopCompatibility: false,
42 // User defined options
43 if (typeof options == 'object') {
45 that.options[i] = options[i];
49 if (that.options.desktopCompatibility) {
50 that.options.overflow = 'hidden';
53 that.wrapper.style.overflow = that.options.overflow;
57 window.addEventListener('onorientationchange' in window ? 'orientationchange' : 'resize', that, false);
59 if (Roo.ux.Iscroll.isTouch || that.options.desktopCompatibility) {
60 that.element.addEventListener(Roo.ux.Iscroll.START_EVENT, that, false);
61 that.element.addEventListener(Roo.ux.Iscroll.MOVE_EVENT, that, false);
62 that.element.addEventListener(Roo.ux.Iscroll.END_EVENT, that, false);
65 if (that.options.checkDOMChanges) {
66 that.element.addEventListener('DOMSubtreeModified', that, false);
70 Roo.apply(Roo.ux.Iscroll, {
71 has3d : ('WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix()),
73 isIphone : (/iphone/gi).test(navigator.appVersion),
74 isIpad : (/ipad/gi).test(navigator.appVersion),
75 isAndroid : (/android/gi).test(navigator.appVersion)
78 Roo.apply(Roo.ux.Iscroll, {
79 isTouch : Roo.ux.Iscroll.isIphone || Roo.ux.Iscroll.isIpad || Roo.ux.Iscroll.isAndroid,
81 START_EVENT : Roo.ux.Iscroll.isTouch ? 'touchstart' : 'mousedown',
82 MOVE_EVENT : Roo.ux.Iscroll.isTouch ? 'touchmove' : 'mousemove',
83 END_EVENT : Roo.ux.Iscroll.isTouch ? 'touchend' : 'mouseup',
85 translateOpen : 'translate' + (Roo.ux.Iscroll.has3d ? '3d(' : '('),
86 translateClose : Roo.ux.Iscroll.has3d ? ',0)' : ')',
92 Roo.ux.Iscroll.prototype = {
97 handleEvent: function (e) {
110 case 'webkitTransitionEnd':
111 that.transitionEnd();
113 case 'orientationchange':
117 case 'DOMSubtreeModified':
118 that.onDOMModified(e);
123 onDOMModified: function (e) {
126 // (Hopefully) execute onDOMModified only once
127 if (e.target.parentNode != that.element) {
131 setTimeout(function () { that.refresh(); }, 0);
133 if (that.options.topOnDOMChanges && (that.x!=0 || that.y!=0)) {
134 that.scrollTo(0,0,'0');
138 refresh: function () {
140 resetX = this.x, resetY = this.y,
143 that.scrollWidth = that.wrapper.clientWidth;
144 that.scrollHeight = that.wrapper.clientHeight;
145 that.scrollerWidth = that.element.offsetWidth;
146 that.scrollerHeight = that.element.offsetHeight;
147 that.maxScrollX = that.scrollWidth - that.scrollerWidth;
148 that.maxScrollY = that.scrollHeight - that.scrollerHeight;
153 if (that.maxScrollX >= 0) {
155 } else if (that.x < that.maxScrollX) {
156 resetX = that.maxScrollX;
160 if (that.maxScrollY >= 0) {
162 } else if (that.y < that.maxScrollY) {
163 resetY = that.maxScrollY;
167 if (that.options.snap) {
168 that.maxPageX = -Math.floor(that.maxScrollX/that.scrollWidth);
169 that.maxPageY = -Math.floor(that.maxScrollY/that.scrollHeight);
171 snap = that.snap(resetX, resetY);
176 if (resetX!=that.x || resetY!=that.y) {
177 that.setTransitionTime('0');
178 that.setPosition(resetX, resetY, true);
181 that.scrollX = that.scrollerWidth > that.scrollWidth;
182 that.scrollY = !that.scrollX || that.scrollerHeight > that.scrollHeight;
184 // Update horizontal scrollbar
185 if (that.options.hScrollbar && that.scrollX) {
186 that.scrollBarX = that.scrollBarX || new scrollbar('horizontal', that.wrapper, that.options.fadeScrollbar, that.options.shrinkScrollbar);
187 that.scrollBarX.init(that.scrollWidth, that.scrollerWidth);
188 } else if (that.scrollBarX) {
189 that.scrollBarX = that.scrollBarX.remove();
192 // Update vertical scrollbar
193 if (that.options.vScrollbar && that.scrollY && that.scrollerHeight > that.scrollHeight) {
194 that.scrollBarY = that.scrollBarY || new scrollbar('vertical', that.wrapper, that.options.fadeScrollbar, that.options.shrinkScrollbar);
195 that.scrollBarY.init(that.scrollHeight, that.scrollerHeight);
196 } else if (that.scrollBarY) {
197 that.scrollBarY = that.scrollBarY.remove();
201 setPosition: function (x, y, hideScrollBars) {
207 that.element.style.webkitTransform = Roo.ux.Iscroll.translateOpen + that.x + 'px,' + that.y + 'px' + Roo.ux.Iscroll.translateClose;
209 // Move the scrollbars
210 if (!hideScrollBars) {
211 if (that.scrollBarX) {
212 that.scrollBarX.setPosition(that.x);
214 if (that.scrollBarY) {
215 that.scrollBarY.setPosition(that.y);
220 setTransitionTime: function(time) {
224 that.element.style.webkitTransitionDuration = time;
226 if (that.scrollBarX) {
227 that.scrollBarX.bar.style.webkitTransitionDuration = time;
228 that.scrollBarX.wrapper.style.webkitTransitionDuration = Roo.ux.Iscroll.has3d && that.options.fadeScrollbar ? '300ms' : '0';
230 if (that.scrollBarY) {
231 that.scrollBarY.bar.style.webkitTransitionDuration = time;
232 that.scrollBarY.wrapper.style.webkitTransitionDuration = Roo.ux.Iscroll.has3d && that.options.fadeScrollbar ? '300ms' : '0';
236 touchStart: function(e) {
247 that.scrolling = true; // This is probably not needed, but may be useful if iScroll is used in conjuction with other frameworks
252 that.setTransitionTime('0');
254 // Check if the scroller is really where it should be
255 if (that.options.momentum || that.options.snap) {
256 matrix = new WebKitCSSMatrix(window.getComputedStyle(that.element).webkitTransform);
257 if (matrix.e != that.x || matrix.f != that.y) {
258 document.removeEventListener('webkitTransitionEnd', that, false);
259 that.setPosition(matrix.e, matrix.f);
264 that.touchStartX = Roo.ux.Iscroll.isTouch ? e.changedTouches[0].pageX : e.pageX;
265 that.scrollStartX = that.x;
267 that.touchStartY = Roo.ux.Iscroll.isTouch ? e.changedTouches[0].pageY : e.pageY;
268 that.scrollStartY = that.y;
270 that.scrollStartTime = e.timeStamp;
276 touchMove: function(e) {
278 pageX = Roo.ux.Iscroll.isTouch ? e.changedTouches[0].pageX : e.pageX,
279 pageY = Roo.ux.Iscroll.isTouch ? e.changedTouches[0].pageY : e.pageY,
280 leftDelta = that.scrollX ? pageX - that.touchStartX : 0,
281 topDelta = that.scrollY ? pageY - that.touchStartY : 0,
282 newX = that.x + leftDelta,
283 newY = that.y + topDelta;
285 if (!that.scrolling) {
289 //e.preventDefault();
290 e.stopPropagation(); // Stopping propagation just saves some cpu cycles (I presume)
292 that.touchStartX = pageX;
293 that.touchStartY = pageY;
295 // Slow down if outside of the boundaries
296 if (newX >= 0 || newX < that.maxScrollX) {
297 newX = that.options.bounce ? Math.round(that.x + leftDelta / 3) : (newX >= 0 || that.maxScrollX>=0) ? 0 : that.maxScrollX;
299 if (newY >= 0 || newY < that.maxScrollY) {
300 newY = that.options.bounce ? Math.round(that.y + topDelta / 3) : (newY >= 0 || that.maxScrollY>=0) ? 0 : that.maxScrollY;
303 if (that.dist > 5) { // 5 pixels threshold is needed on Android, but also on iPhone looks more natural
304 that.setPosition(newX, newY);
306 that.directionX = leftDelta > 0 ? -1 : 1;
307 that.directionY = topDelta > 0 ? -1 : 1;
309 that.dist+= Math.abs(leftDelta) + Math.abs(topDelta);
313 touchEnd: function(e) {
315 time = e.timeStamp - that.scrollStartTime,
316 point = Roo.ux.Iscroll.isTouch ? e.changedTouches[0] : e,
318 momentumX, momentumY,
320 newPositionX = that.x, newPositionY = that.y,
323 if (!that.scrolling) {
326 that.scrolling = false;
329 that.resetPosition();
331 if (Roo.ux.Iscroll.isTouch) {
332 // Find the last touched element
333 target = point.target;
334 while (target.nodeType != 1) {
335 target = target.parentNode;
338 // Create the fake event
339 target.style.pointerEvents = 'auto';
340 ev = document.createEvent('MouseEvents');
341 ev.initMouseEvent('click', true, true, e.view, 1,
342 point.screenX, point.screenY, point.clientX, point.clientY,
343 e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
346 target.dispatchEvent(ev);
352 if (!that.options.snap && time > 250) { // Prevent slingshot effect
353 that.resetPosition();
357 if (that.options.momentum) {
358 momentumX = that.scrollX === true
359 ? that.momentum(that.x - that.scrollStartX,
361 that.options.bounce ? -that.x + that.scrollWidth/5 : -that.x,
362 that.options.bounce ? that.x + that.scrollerWidth - that.scrollWidth + that.scrollWidth/5 : that.x + that.scrollerWidth - that.scrollWidth)
363 : { dist: 0, time: 0 };
365 momentumY = that.scrollY === true
366 ? that.momentum(that.y - that.scrollStartY,
368 that.options.bounce ? -that.y + that.scrollHeight/5 : -that.y,
369 that.options.bounce ? (that.maxScrollY < 0 ? that.y + that.scrollerHeight - that.scrollHeight : 0) + that.scrollHeight/5 : that.y + that.scrollerHeight - that.scrollHeight)
370 : { dist: 0, time: 0 };
372 newDuration = Math.max(Math.max(momentumX.time, momentumY.time), 1); // The minimum animation length must be 1ms
373 newPositionX = that.x + momentumX.dist;
374 newPositionY = that.y + momentumY.dist;
377 if (that.options.snap) {
378 snap = that.snap(newPositionX, newPositionY);
379 newPositionX = snap.x;
380 newPositionY = snap.y;
381 newDuration = Math.max(snap.time, newDuration);
384 that.scrollTo(newPositionX, newPositionY, newDuration + 'ms');
387 transitionEnd: function () {
389 document.removeEventListener('webkitTransitionEnd', that, false);
390 that.resetPosition();
393 resetPosition: function () {
400 } else if (that.x < that.maxScrollX) {
401 resetX = that.maxScrollX;
404 if (that.y >= 0 || that.maxScrollY > 0) {
406 } else if (that.y < that.maxScrollY) {
407 resetY = that.maxScrollY;
410 if (resetX != that.x || resetY != that.y) {
411 that.scrollTo(resetX, resetY);
414 that.onScrollEnd(); // Execute custom code on scroll end
418 // Hide the scrollbars
419 if (that.scrollBarX) {
420 that.scrollBarX.hide();
422 if (that.scrollBarY) {
423 that.scrollBarY.hide();
428 snap: function (x, y) {
429 var that = this, time;
431 if (that.directionX > 0) {
432 x = Math.floor(x/that.scrollWidth);
433 } else if (that.directionX < 0) {
434 x = Math.ceil(x/that.scrollWidth);
436 x = Math.round(x/that.scrollWidth);
439 x = x * that.scrollWidth;
442 } else if (x < that.maxScrollX) {
443 that.pageX = that.maxPageX;
447 if (that.directionY > 0) {
448 y = Math.floor(y/that.scrollHeight);
449 } else if (that.directionY < 0) {
450 y = Math.ceil(y/that.scrollHeight);
452 y = Math.round(y/that.scrollHeight);
455 y = y * that.scrollHeight;
458 } else if (y < that.maxScrollY) {
459 that.pageY = that.maxPageY;
463 // Snap with constant speed (proportional duration)
464 time = Math.round(Math.max(
465 Math.abs(that.x - x) / that.scrollWidth * 500,
466 Math.abs(that.y - y) / that.scrollHeight * 500
469 return { x: x, y: y, time: time };
472 scrollTo: function (destX, destY, runtime) {
475 if (that.x == destX && that.y == destY) {
476 that.resetPosition();
481 that.setTransitionTime(runtime || '350ms');
482 that.setPosition(destX, destY);
484 if (runtime==='0' || runtime=='0s' || runtime=='0ms') {
485 that.resetPosition();
487 document.addEventListener('webkitTransitionEnd', that, false); // At the end of the transition check if we are still inside of the boundaries
491 scrollToPage: function (pageX, pageY, runtime) {
492 var that = this, snap;
494 if (!that.options.snap) {
495 that.pageX = -Math.round(that.x / that.scrollWidth);
496 that.pageY = -Math.round(that.y / that.scrollHeight);
499 if (pageX == 'next') {
500 pageX = ++that.pageX;
501 } else if (pageX == 'prev') {
502 pageX = --that.pageX;
505 if (pageY == 'next') {
506 pageY = ++that.pageY;
507 } else if (pageY == 'prev') {
508 pageY = --that.pageY;
511 pageX = -pageX*that.scrollWidth;
512 pageY = -pageY*that.scrollHeight;
514 snap = that.snap(pageX, pageY);
518 that.scrollTo(pageX, pageY, runtime || '500ms');
521 scrollToElement: function (el, runtime) {
522 el = typeof el == 'object' ? el : this.element.querySelector(el);
529 x = that.scrollX ? -el.offsetLeft : 0,
530 y = that.scrollY ? -el.offsetTop : 0;
534 } else if (x < that.maxScrollX) {
540 } else if (y < that.maxScrollY) {
544 that.scrollTo(x, y, runtime);
547 momentum: function (dist, time, maxDistUpper, maxDistLower) {
550 speed = Math.abs(dist) / time * 1000,
551 newDist = speed * speed / friction / 1000,
554 // Proportinally reduce speed if we are outside of the boundaries
555 if (dist > 0 && newDist > maxDistUpper) {
556 speed = speed * maxDistUpper / newDist / friction;
557 newDist = maxDistUpper;
558 } else if (dist < 0 && newDist > maxDistLower) {
559 speed = speed * maxDistLower / newDist / friction;
560 newDist = maxDistLower;
563 newDist = newDist * (dist < 0 ? -1 : 1);
564 newTime = speed / deceleration;
566 return { dist: Math.round(newDist), time: Math.round(newTime) };
569 onScrollEnd: function () {},
571 destroy: function (full) {
574 window.removeEventListener('onorientationchange' in window ? 'orientationchange' : 'resize', that, false);
575 that.element.removeEventListener(START_EVENT, that, false);
576 that.element.removeEventListener(MOVE_EVENT, that, false);
577 that.element.removeEventListener(END_EVENT, that, false);
578 document.removeEventListener('webkitTransitionEnd', that, false);
580 if (that.options.checkDOMChanges) {
581 that.element.removeEventListener('DOMSubtreeModified', that, false);
584 if (that.scrollBarX) {
585 that.scrollBarX = that.scrollBarX.remove();
588 if (that.scrollBarY) {
589 that.scrollBarY = that.scrollBarY.remove();
593 that.wrapper.parentNode.removeChild(that.wrapper);
600 Roo.ux.Iscroll.scrollbar = function (dir, wrapper, fade, shrink) {
601 var that = this, style;
605 that.shrink = shrink;
606 that.uid = ++Roo.ux.Iscroll.uid;
608 // Create main scrollbar
609 that.bar = document.createElement('div');
611 style = 'position:absolute;top:0;left:0;-webkit-transition-timing-function:cubic-bezier(0,0,0.25,1);pointer-events:none;-webkit-transition-duration:0;-webkit-transition-delay:0;-webkit-transition-property:-webkit-transform;z-index:10;background:rgba(0,0,0,0.5);' +
612 '-webkit-transform:' + Roo.ux.Iscroll.translateOpen + '0,0' + Roo.ux.Iscroll.translateClose + ';' +
613 (dir == 'horizontal' ? '-webkit-border-radius:3px 2px;min-width:6px;min-height:5px' : '-webkit-border-radius:2px 3px;min-width:5px;min-height:6px');
615 that.bar.setAttribute('style', style);
617 // Create scrollbar wrapper
618 that.wrapper = document.createElement('div');
619 style = '-webkit-mask:-webkit-canvas(scrollbar' + that.uid + that.dir + ');position:absolute;z-index:10;pointer-events:none;overflow:hidden;opacity:0;-webkit-transition-duration:' + (fade ? '300ms' : '0') + ';-webkit-transition-delay:0;-webkit-transition-property:opacity;' +
620 (that.dir == 'horizontal' ? 'bottom:2px;left:2px;right:7px;height:5px' : 'top:2px;right:2px;bottom:7px;width:5px;');
621 that.wrapper.setAttribute('style', style);
623 // Add scrollbar to the DOM
624 that.wrapper.appendChild(that.bar);
625 wrapper.appendChild(that.wrapper);
628 Roo.ux.Iscroll.scrollbar.prototype = {
629 init: function (scroll, size) {
633 // Create scrollbar mask
634 if (that.dir == 'horizontal') {
635 if (that.maxSize != that.wrapper.offsetWidth) {
636 that.maxSize = that.wrapper.offsetWidth;
637 ctx = document.getCSSCanvasContext("2d", "scrollbar" + that.uid + that.dir, that.maxSize, 5);
638 ctx.fillStyle = "rgb(0,0,0)";
640 ctx.arc(2.5, 2.5, 2.5, Math.PI/2, -Math.PI/2, false);
641 ctx.lineTo(that.maxSize-2.5, 0);
642 ctx.arc(that.maxSize-2.5, 2.5, 2.5, -Math.PI/2, Math.PI/2, false);
647 if (that.maxSize != that.wrapper.offsetHeight) {
648 that.maxSize = that.wrapper.offsetHeight;
649 ctx = document.getCSSCanvasContext("2d", "scrollbar" + that.uid + that.dir, 5, that.maxSize);
650 ctx.fillStyle = "rgb(0,0,0)";
652 ctx.arc(2.5, 2.5, 2.5, Math.PI, 0, false);
653 ctx.lineTo(5, that.maxSize-2.5);
654 ctx.arc(2.5, that.maxSize-2.5, 2.5, 0, Math.PI, false);
660 that.size = Math.max(Math.round(that.maxSize * that.maxSize / size), 6);
661 that.maxScroll = that.maxSize - that.size;
662 that.toWrapperProp = that.maxScroll / (scroll - size);
663 that.bar.style[that.dir == 'horizontal' ? 'width' : 'height'] = that.size + 'px';
666 setPosition: function (pos) {
669 if (that.wrapper.style.opacity != '1') {
673 pos = Math.round(that.toWrapperProp * pos);
676 pos = that.shrink ? pos + pos*3 : 0;
677 if (that.size + pos < 7) {
678 pos = -that.size + 6;
680 } else if (pos > that.maxScroll) {
681 pos = that.shrink ? pos + (pos-that.maxScroll)*3 : that.maxScroll;
682 if (that.size + that.maxScroll - pos < 7) {
683 pos = that.size + that.maxScroll - 6;
687 pos = that.dir == 'horizontal'
688 ? Roo.ux.Iscroll.translateOpen + pos + 'px,0' + Roo.ux.Iscroll.translateClose
689 : Roo.ux.Iscroll.translateOpen + '0,' + pos + 'px' + Roo.ux.Iscroll.translateClose;
691 that.bar.style.webkitTransform = pos;
695 if (Roo.ux.Iscroll.has3d) {
696 this.wrapper.style.webkitTransitionDelay = '0';
698 this.wrapper.style.opacity = '1';
702 if (Roo.ux.Iscroll.has3d) {
703 this.wrapper.style.webkitTransitionDelay = '350ms';
705 this.wrapper.style.opacity = '0';
708 remove: function () {
709 this.wrapper.parentNode.removeChild(this.wrapper);
714 // Is translate3d compatible?