iscroll.js 33 KB

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