iscroll.js 32 KB

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