iscroll.js 33 KB

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