zepto.iscroll.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*!
  2. * iScroll v4.2.2 ~ 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,_bindArr = [],
  7. dummyStyle = doc.createElement('div').style,
  8. vendor = (function () {
  9. var vendors = 'webkitT,MozT,msT,OT,t'.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. isTouchPad = (/hp-tablet/gi).test(navigator.appVersion),
  32. has3d = prefixStyle('perspective') in dummyStyle,
  33. hasTouch = 'ontouchstart' in window && !isTouchPad,
  34. hasTransform = !!vendor,
  35. hasTransitionEnd = prefixStyle('transition') in dummyStyle,
  36. RESIZE_EV = 'onorientationchange' in window ? 'orientationchange' : 'resize',
  37. START_EV = hasTouch ? 'touchstart' : 'mousedown',
  38. MOVE_EV = hasTouch ? 'touchmove' : 'mousemove',
  39. END_EV = hasTouch ? 'touchend' : 'mouseup',
  40. CANCEL_EV = hasTouch ? 'touchcancel' : 'mouseup',
  41. TRNEND_EV = (function () {
  42. if ( vendor === false ) return false;
  43. var transitionEnd = {
  44. '' : 'transitionend',
  45. 'webkit' : 'webkitTransitionEnd',
  46. 'Moz' : 'transitionend',
  47. 'O' : 'otransitionend',
  48. 'ms' : 'MSTransitionEnd'
  49. };
  50. return transitionEnd[vendor];
  51. })(),
  52. nextFrame = (function() {
  53. return window.requestAnimationFrame ||
  54. window.webkitRequestAnimationFrame ||
  55. window.mozRequestAnimationFrame ||
  56. window.oRequestAnimationFrame ||
  57. window.msRequestAnimationFrame ||
  58. function(callback) { return setTimeout(callback, 1); };
  59. })(),
  60. cancelFrame = (function () {
  61. return window.cancelRequestAnimationFrame ||
  62. window.webkitCancelAnimationFrame ||
  63. window.webkitCancelRequestAnimationFrame ||
  64. window.mozCancelRequestAnimationFrame ||
  65. window.oCancelRequestAnimationFrame ||
  66. window.msCancelRequestAnimationFrame ||
  67. clearTimeout;
  68. })(),
  69. // Helpers
  70. translateZ = has3d ? ' translateZ(0)' : '',
  71. // Constructor
  72. iScroll = function (el, options) {
  73. var that = this,
  74. i;
  75. that.wrapper = typeof el == 'object' ? el : doc.getElementById(el);
  76. that.wrapper.style.overflow = 'hidden';
  77. that.scroller = that.wrapper.children[0];
  78. that.translateZ = translateZ;
  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. // Events
  95. onRefresh: null,
  96. onBeforeScrollStart: function (e) { e.preventDefault(); },
  97. onScrollStart: null,
  98. onBeforeScrollMove: null,
  99. onScrollMove: null,
  100. onBeforeScrollEnd: null,
  101. onScrollEnd: null,
  102. onTouchEnd: null,
  103. onDestroy: null
  104. };
  105. // User defined options
  106. for (i in options) that.options[i] = options[i];
  107. // Set starting position
  108. that.x = that.options.x;
  109. that.y = that.options.y;
  110. // Normalize options
  111. that.options.useTransform = hasTransform && that.options.useTransform;
  112. that.options.useTransition = hasTransitionEnd && that.options.useTransition;
  113. // Set some default styles
  114. that.scroller.style[transitionProperty] = that.options.useTransform ? cssVendor + 'transform' : 'top left';
  115. that.scroller.style[transitionDuration] = '0';
  116. that.scroller.style[transformOrigin] = '0 0';
  117. if (that.options.useTransition) that.scroller.style[transitionTimingFunction] = 'cubic-bezier(0.33,0.66,0.66,1)';
  118. if (that.options.useTransform) that.scroller.style[transform] = 'translate(' + that.x + 'px,' + that.y + 'px)' + translateZ;
  119. else that.scroller.style.cssText += ';position:absolute;top:' + that.y + 'px;left:' + that.x + 'px';
  120. that.refresh();
  121. that._bind(RESIZE_EV, window);
  122. that._bind(START_EV);
  123. if (that.options.checkDOMChanges) that.checkDOMTime = setInterval(function () {
  124. that._checkDOMChanges();
  125. }, 500);
  126. };
  127. // Prototype
  128. iScroll.prototype = {
  129. enabled: true,
  130. x: 0,
  131. y: 0,
  132. steps: [],
  133. scale: 1,
  134. currPageX: 0, currPageY: 0,
  135. pagesX: [], pagesY: [],
  136. aniTime: null,
  137. isStopScrollAction:false,
  138. handleEvent: function (e) {
  139. var that = this;
  140. switch(e.type) {
  141. case START_EV:
  142. if (!hasTouch && e.button !== 0) return;
  143. that._start(e);
  144. break;
  145. case MOVE_EV: that._move(e); break;
  146. case END_EV:
  147. case CANCEL_EV: that._end(e); break;
  148. case RESIZE_EV: that._resize(); break;
  149. case TRNEND_EV: that._transitionEnd(e); break;
  150. }
  151. },
  152. _checkDOMChanges: function () {
  153. if (this.moved || this.animating ||
  154. (this.scrollerW == this.scroller.offsetWidth * this.scale && this.scrollerH == this.scroller.offsetHeight * this.scale)) return;
  155. this.refresh();
  156. },
  157. _resize: function () {
  158. var that = this;
  159. setTimeout(function () { that.refresh(); }, isAndroid ? 200 : 0);
  160. },
  161. _pos: function (x, y) {
  162. x = this.hScroll ? x : 0;
  163. y = this.vScroll ? y : 0;
  164. if (this.options.useTransform) {
  165. this.scroller.style[transform] = 'translate(' + x + 'px,' + y + 'px) scale(' + this.scale + ')' + translateZ;
  166. } else {
  167. x = m.round(x);
  168. y = m.round(y);
  169. this.scroller.style.left = x + 'px';
  170. this.scroller.style.top = y + 'px';
  171. }
  172. this.x = x;
  173. this.y = y;
  174. },
  175. _start: function (e) {
  176. var that = this,
  177. point = hasTouch ? e.touches[0] : e,
  178. matrix, x, y,
  179. c1, c2;
  180. if (!that.enabled) return;
  181. if (that.options.onBeforeScrollStart) that.options.onBeforeScrollStart.call(that, e);
  182. if (that.options.useTransition ) that._transitionTime(0);
  183. that.moved = false;
  184. that.animating = false;
  185. that.distX = 0;
  186. that.distY = 0;
  187. that.absDistX = 0;
  188. that.absDistY = 0;
  189. that.dirX = 0;
  190. that.dirY = 0;
  191. that.isStopScrollAction = false;
  192. if (that.options.momentum) {
  193. if (that.options.useTransform) {
  194. // Very lame general purpose alternative to CSSMatrix
  195. matrix = getComputedStyle(that.scroller, null)[transform].replace(/[^0-9\-.,]/g, '').split(',');
  196. x = +matrix[4];
  197. y = +matrix[5];
  198. } else {
  199. x = +getComputedStyle(that.scroller, null).left.replace(/[^0-9-]/g, '');
  200. y = +getComputedStyle(that.scroller, null).top.replace(/[^0-9-]/g, '');
  201. }
  202. if (x != that.x || y != that.y) {
  203. that.isStopScrollAction = true;
  204. if (that.options.useTransition) that._unbind(TRNEND_EV);
  205. else cancelFrame(that.aniTime);
  206. that.steps = [];
  207. that._pos(x, y);
  208. if (that.options.onScrollEnd) that.options.onScrollEnd.call(that);
  209. }
  210. }
  211. that.startX = that.x;
  212. that.startY = that.y;
  213. that.pointX = point.pageX;
  214. that.pointY = point.pageY;
  215. that.startTime = e.timeStamp || Date.now();
  216. if (that.options.onScrollStart) that.options.onScrollStart.call(that, e);
  217. that._bind(MOVE_EV, window);
  218. that._bind(END_EV, window);
  219. that._bind(CANCEL_EV, window);
  220. },
  221. _move: function (e) {
  222. var that = this,
  223. point = hasTouch ? e.touches[0] : e,
  224. deltaX = point.pageX - that.pointX,
  225. deltaY = point.pageY - that.pointY,
  226. newX = that.x + deltaX,
  227. newY = that.y + deltaY,
  228. timestamp = e.timeStamp || Date.now();
  229. if (that.options.onBeforeScrollMove) that.options.onBeforeScrollMove.call(that, e);
  230. that.pointX = point.pageX;
  231. that.pointY = point.pageY;
  232. // Slow down if outside of the boundaries
  233. if (newX > 0 || newX < that.maxScrollX) {
  234. newX = that.options.bounce ? that.x + (deltaX / 2) : newX >= 0 || that.maxScrollX >= 0 ? 0 : that.maxScrollX;
  235. }
  236. if (newY > that.minScrollY || newY < that.maxScrollY) {
  237. newY = that.options.bounce ? that.y + (deltaY / 2) : newY >= that.minScrollY || that.maxScrollY >= 0 ? that.minScrollY : that.maxScrollY;
  238. }
  239. that.distX += deltaX;
  240. that.distY += deltaY;
  241. that.absDistX = m.abs(that.distX);
  242. that.absDistY = m.abs(that.distY);
  243. if (that.absDistX < 6 && that.absDistY < 6) {
  244. return;
  245. }
  246. // Lock direction
  247. if (that.options.lockDirection) {
  248. if (that.absDistX > that.absDistY + 5) {
  249. newY = that.y;
  250. deltaY = 0;
  251. } else if (that.absDistY > that.absDistX + 5) {
  252. newX = that.x;
  253. deltaX = 0;
  254. }
  255. }
  256. that.moved = true;
  257. // internal for header scroll
  258. that._beforePos ? that._beforePos(newY, deltaY) && that._pos(newX, newY) : that._pos(newX, newY);
  259. that.dirX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
  260. that.dirY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
  261. if (timestamp - that.startTime > 300) {
  262. that.startTime = timestamp;
  263. that.startX = that.x;
  264. that.startY = that.y;
  265. }
  266. if (that.options.onScrollMove) that.options.onScrollMove.call(that, e);
  267. },
  268. _end: function (e) {
  269. if (hasTouch && e.touches.length !== 0) return;
  270. var that = this,
  271. point = hasTouch ? e.changedTouches[0] : e,
  272. target, ev,
  273. momentumX = { dist:0, time:0 },
  274. momentumY = { dist:0, time:0 },
  275. duration = (e.timeStamp || Date.now()) - that.startTime,
  276. newPosX = that.x,
  277. newPosY = that.y,
  278. newDuration;
  279. that._unbind(MOVE_EV, window);
  280. that._unbind(END_EV, window);
  281. that._unbind(CANCEL_EV, window);
  282. if (that.options.onBeforeScrollEnd) that.options.onBeforeScrollEnd.call(that, e);
  283. if (!that.moved) {
  284. if (hasTouch && this.options.handleClick && !that.isStopScrollAction) {
  285. that.doubleTapTimer = setTimeout(function () {
  286. that.doubleTapTimer = null;
  287. // Find the last touched element
  288. target = point.target;
  289. while (target.nodeType != 1) target = target.parentNode;
  290. if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA') {
  291. ev = doc.createEvent('MouseEvents');
  292. ev.initMouseEvent('click', true, true, e.view, 1,
  293. point.screenX, point.screenY, point.clientX, point.clientY,
  294. e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
  295. 0, null);
  296. ev._fake = true;
  297. target.dispatchEvent(ev);
  298. }
  299. }, 0);
  300. }
  301. that._resetPos(400);
  302. if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
  303. return;
  304. }
  305. if (duration < 300 && that.options.momentum) {
  306. momentumX = newPosX ? that._momentum(newPosX - that.startX, duration, -that.x, that.scrollerW - that.wrapperW + that.x, that.options.bounce ? that.wrapperW : 0) : momentumX;
  307. 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;
  308. newPosX = that.x + momentumX.dist;
  309. newPosY = that.y + momentumY.dist;
  310. if ((that.x > 0 && newPosX > 0) || (that.x < that.maxScrollX && newPosX < that.maxScrollX)) momentumX = { dist:0, time:0 };
  311. if ((that.y > that.minScrollY && newPosY > that.minScrollY) || (that.y < that.maxScrollY && newPosY < that.maxScrollY)) momentumY = { dist:0, time:0 };
  312. }
  313. if (momentumX.dist || momentumY.dist) {
  314. newDuration = m.max(m.max(momentumX.time, momentumY.time), 10);
  315. that.scrollTo(m.round(newPosX), m.round(newPosY), newDuration);
  316. if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
  317. return;
  318. }
  319. that._resetPos(200);
  320. if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
  321. },
  322. _resetPos: function (time) {
  323. var that = this,
  324. resetX = that.x >= 0 ? 0 : that.x < that.maxScrollX ? that.maxScrollX : that.x,
  325. resetY = that.y >= that.minScrollY || that.maxScrollY > 0 ? that.minScrollY : that.y < that.maxScrollY ? that.maxScrollY : that.y;
  326. if (resetX == that.x && resetY == that.y) {
  327. if (that.moved) {
  328. that.moved = false;
  329. if (that.options.onScrollEnd) that.options.onScrollEnd.call(that); // Execute custom code on scroll end
  330. if (that._afterPos) that._afterPos();
  331. }
  332. return;
  333. }
  334. that.scrollTo(resetX, resetY, time || 0);
  335. },
  336. _transitionEnd: function (e) {
  337. var that = this;
  338. if (e.target != that.scroller) return;
  339. that._unbind(TRNEND_EV);
  340. that._startAni();
  341. },
  342. /**
  343. *
  344. * Utilities
  345. *
  346. */
  347. _startAni: function () {
  348. var that = this,
  349. startX = that.x, startY = that.y,
  350. startTime = Date.now(),
  351. step, easeOut,
  352. animate;
  353. if (that.animating) return;
  354. if (!that.steps.length) {
  355. that._resetPos(400);
  356. return;
  357. }
  358. step = that.steps.shift();
  359. if (step.x == startX && step.y == startY) step.time = 0;
  360. that.animating = true;
  361. that.moved = true;
  362. if (that.options.useTransition) {
  363. that._transitionTime(step.time);
  364. that._pos(step.x, step.y);
  365. that.animating = false;
  366. if (step.time) that._bind(TRNEND_EV);
  367. else that._resetPos(0);
  368. return;
  369. }
  370. animate = function () {
  371. var now = Date.now(),
  372. newX, newY;
  373. if (now >= startTime + step.time) {
  374. that._pos(step.x, step.y);
  375. that.animating = false;
  376. if (that.options.onAnimationEnd) that.options.onAnimationEnd.call(that); // Execute custom code on animation end
  377. that._startAni();
  378. return;
  379. }
  380. now = (now - startTime) / step.time - 1;
  381. easeOut = m.sqrt(1 - now * now);
  382. newX = (step.x - startX) * easeOut + startX;
  383. newY = (step.y - startY) * easeOut + startY;
  384. that._pos(newX, newY);
  385. if (that.animating) that.aniTime = nextFrame(animate);
  386. };
  387. animate();
  388. },
  389. _transitionTime: function (time) {
  390. time += 'ms';
  391. this.scroller.style[transitionDuration] = time;
  392. },
  393. _momentum: function (dist, time, maxDistUpper, maxDistLower, size) {
  394. var deceleration = 0.0006,
  395. speed = m.abs(dist) * (this.options.speedScale||1) / time,
  396. newDist = (speed * speed) / (2 * deceleration),
  397. newTime = 0, outsideDist = 0;
  398. // Proportinally reduce speed if we are outside of the boundaries
  399. if (dist > 0 && newDist > maxDistUpper) {
  400. outsideDist = size / (6 / (newDist / speed * deceleration));
  401. maxDistUpper = maxDistUpper + outsideDist;
  402. speed = speed * maxDistUpper / newDist;
  403. newDist = maxDistUpper;
  404. } else if (dist < 0 && newDist > maxDistLower) {
  405. outsideDist = size / (6 / (newDist / speed * deceleration));
  406. maxDistLower = maxDistLower + outsideDist;
  407. speed = speed * maxDistLower / newDist;
  408. newDist = maxDistLower;
  409. }
  410. newDist = newDist * (dist < 0 ? -1 : 1);
  411. newTime = speed / deceleration;
  412. return { dist: newDist, time: m.round(newTime) };
  413. },
  414. _offset: function (el) {
  415. var left = -el.offsetLeft,
  416. top = -el.offsetTop;
  417. while (el = el.offsetParent) {
  418. left -= el.offsetLeft;
  419. top -= el.offsetTop;
  420. }
  421. if (el != this.wrapper) {
  422. left *= this.scale;
  423. top *= this.scale;
  424. }
  425. return { left: left, top: top };
  426. },
  427. _bind: function (type, el, bubble) {
  428. _bindArr.concat([el || this.scroller, type, this]);
  429. (el || this.scroller).addEventListener(type, this, !!bubble);
  430. },
  431. _unbind: function (type, el, bubble) {
  432. (el || this.scroller).removeEventListener(type, this, !!bubble);
  433. },
  434. /**
  435. *
  436. * Public methods
  437. *
  438. */
  439. destroy: function () {
  440. var that = this;
  441. that.scroller.style[transform] = '';
  442. // Remove the event listeners
  443. that._unbind(RESIZE_EV, window);
  444. that._unbind(START_EV);
  445. that._unbind(MOVE_EV, window);
  446. that._unbind(END_EV, window);
  447. that._unbind(CANCEL_EV, window);
  448. if (that.options.useTransition) that._unbind(TRNEND_EV);
  449. if (that.options.checkDOMChanges) clearInterval(that.checkDOMTime);
  450. if (that.options.onDestroy) that.options.onDestroy.call(that);
  451. //清除所有绑定的事件
  452. for (var i = 0, l = _bindArr.length; i < l;) {
  453. _bindArr[i].removeEventListener(_bindArr[i + 1], _bindArr[i + 2]);
  454. _bindArr[i] = null;
  455. i = i + 3
  456. }
  457. _bindArr = [];
  458. //干掉外边的容器内容
  459. var div = doc.createElement('div');
  460. div.appendChild(this.wrapper);
  461. div.innerHTML = '';
  462. that.wrapper = that.scroller = div = null;
  463. },
  464. refresh: function () {
  465. var that = this,
  466. offset;
  467. that.wrapperW = that.wrapper.clientWidth || 1;
  468. that.wrapperH = that.wrapper.clientHeight || 1;
  469. that.minScrollY = -that.options.topOffset || 0;
  470. that.scrollerW = m.round(that.scroller.offsetWidth * that.scale);
  471. that.scrollerH = m.round((that.scroller.offsetHeight + that.minScrollY) * that.scale);
  472. that.maxScrollX = that.wrapperW - that.scrollerW;
  473. that.maxScrollY = that.wrapperH - that.scrollerH + that.minScrollY;
  474. that.dirX = 0;
  475. that.dirY = 0;
  476. if (that.options.onRefresh) that.options.onRefresh.call(that);
  477. that.hScroll = that.options.hScroll && that.maxScrollX < 0;
  478. that.vScroll = that.options.vScroll && (!that.options.bounceLock && !that.hScroll || that.scrollerH > that.wrapperH);
  479. offset = that._offset(that.wrapper);
  480. that.wrapperOffsetLeft = -offset.left;
  481. that.wrapperOffsetTop = -offset.top;
  482. that.scroller.style[transitionDuration] = '0';
  483. that._resetPos(400);
  484. },
  485. scrollTo: function (x, y, time, relative) {
  486. var that = this,
  487. step = x,
  488. i, l;
  489. that.stop();
  490. if (!step.length) step = [{ x: x, y: y, time: time, relative: relative }];
  491. for (i=0, l=step.length; i<l; i++) {
  492. if (step[i].relative) { step[i].x = that.x - step[i].x; step[i].y = that.y - step[i].y; }
  493. that.steps.push({ x: step[i].x, y: step[i].y, time: step[i].time || 0 });
  494. }
  495. that._startAni();
  496. },
  497. scrollToElement: function (el, time) {
  498. var that = this, pos;
  499. el = el.nodeType ? el : that.scroller.querySelector(el);
  500. if (!el) return;
  501. pos = that._offset(el);
  502. pos.left += that.wrapperOffsetLeft;
  503. pos.top += that.wrapperOffsetTop;
  504. pos.left = pos.left > 0 ? 0 : pos.left < that.maxScrollX ? that.maxScrollX : pos.left;
  505. pos.top = pos.top > that.minScrollY ? that.minScrollY : pos.top < that.maxScrollY ? that.maxScrollY : pos.top;
  506. time = time === undefined ? m.max(m.abs(pos.left)*2, m.abs(pos.top)*2) : time;
  507. that.scrollTo(pos.left, pos.top, time);
  508. },
  509. scrollToPage: function (pageX, pageY, time) {
  510. var that = this, x, y;
  511. time = time === undefined ? 400 : time;
  512. if (that.options.onScrollStart) that.options.onScrollStart.call(that);
  513. x = -that.wrapperW * pageX;
  514. y = -that.wrapperH * pageY;
  515. if (x < that.maxScrollX) x = that.maxScrollX;
  516. if (y < that.maxScrollY) y = that.maxScrollY;
  517. that.scrollTo(x, y, time);
  518. },
  519. disable: function () {
  520. this.stop();
  521. this._resetPos(0);
  522. this.enabled = false;
  523. // If disabled after touchstart we make sure that there are no left over events
  524. this._unbind(MOVE_EV, window);
  525. this._unbind(END_EV, window);
  526. this._unbind(CANCEL_EV, window);
  527. },
  528. enable: function () {
  529. this.enabled = true;
  530. },
  531. stop: function () {
  532. if (this.options.useTransition) this._unbind(TRNEND_EV);
  533. else cancelFrame(this.aniTime);
  534. this.steps = [];
  535. this.moved = false;
  536. this.animating = false;
  537. },
  538. isReady: function () {
  539. return !this.moved && !this.animating;
  540. }
  541. };
  542. function prefixStyle (style) {
  543. if ( vendor === '' ) return style;
  544. style = style.charAt(0).toUpperCase() + style.substr(1);
  545. return vendor + style;
  546. }
  547. dummyStyle = null; // for the sake of it
  548. if (typeof exports !== 'undefined') exports.iScroll = iScroll;
  549. else window.iScroll = iScroll;
  550. (function($){
  551. if(!$)return;
  552. var orgiScroll = iScroll,
  553. id = 0,
  554. cacheInstance = {};
  555. function createInstance(el,options){
  556. var uqid = 'iscroll' + id++;
  557. el.data('_iscroll_',uqid);
  558. return cacheInstance[uqid] = new orgiScroll(el[0],options)
  559. }
  560. window.iScroll = function(el,options){
  561. return createInstance($(typeof el == 'string' ? '#' + el : el),options)
  562. };
  563. $.fn.iScroll = function(method){
  564. var resultArr = [];
  565. this.each(function(i,el){
  566. if(typeof method == 'string'){
  567. var instance = cacheInstance[$(el).data('_iscroll_')],pro;
  568. if(instance && (pro = instance[method])){
  569. var result = $.isFunction(pro) ? pro.apply(instance, Array.prototype.slice.call(arguments,1)) : pro;
  570. if(result !== instance && result !== undefined){
  571. resultArr.push(result);
  572. }
  573. }
  574. }else{
  575. if(!$(el).data('_iscroll_'))
  576. createInstance($(el),method)
  577. }
  578. });
  579. return resultArr.length ? resultArr : this;
  580. }
  581. })(window.Zepto || null)
  582. })(window, document);
  583. /**
  584. * Change list
  585. * 修改记录
  586. *
  587. * 1. 2012-08-14 解决滑动中按住停止滚动,松开后被点元素触发点击事件。
  588. *
  589. * 具体修改:
  590. * a. 202行 添加isStopScrollAction: false 给iScroll的原型上添加变量
  591. * b. 365行 _start方法里面添加that.isStopScrollAction = false; 默认让这个值为false
  592. * c. 390行 if (x != that.x || y != that.y)条件语句里面 添加了 that.isStopScrollAction = true; 当目标值与实际值不一致,说明还在滚动动画中
  593. * d. 554行 that.isStopScrollAction || (that.doubleTapTimer = setTimeout(function () {
  594. * ......
  595. * ......
  596. * }, that.options.zoom ? 250 : 0));
  597. * 如果isStopScrollAction为true就不派送click事件
  598. *
  599. *
  600. * 2. 2012-08-14 给options里面添加speedScale属性,提供外部控制冲量滚动速度
  601. *
  602. * 具体修改
  603. * a. 108行 添加speedScale: 1, 给options里面添加speedScale属性,默认为1
  604. * b. 798行 speed = m.abs(dist) * this.options.speedScale / time, 在原来速度的基础上*speedScale来改变速度
  605. *
  606. * 3. 2012-08-21 修改部分代码,给iscroll_plugin墙用的
  607. *
  608. * 具体修改
  609. * a. 517行 在_pos之前,调用_beforePos,如果里面不返回true, 将不会调用_pos
  610. * // internal for header scroll
  611. * if (that._beforePos)
  612. * that._beforePos(newY, deltaY) && that._pos(newX, newY);
  613. * else
  614. * that._pos(newX, newY);
  615. *
  616. * b. 680行 在滚动结束后调用 _afterPos.
  617. * // internal for header scroll
  618. * if (that._afterPos) that._afterPos();
  619. *
  620. * c. 106行构造器里面添加以下代码
  621. * // add var to this for header scroll
  622. * that.translateZ = translateZ;
  623. *
  624. * 为处理溢出
  625. * _bind 方法
  626. * destroy 方法
  627. * 最开头的 _bindArr = []
  628. *
  629. */
  630. /**
  631. * @file GMU定制版iscroll,基于[iScroll 4.2.2](http://cubiq.org/iscroll-4), 去除zoom, pc兼容,snap, scrollbar等功能。同时把iscroll扩展到了Zepto的原型中。
  632. * @name zepto.iScroll
  633. * @import core/zepto.js
  634. * @desc GMU定制版iscroll,基于{@link[http://cubiq.org/iscroll-4] iScroll 4.2.2}, 去除zoom, pc兼容,snap, scrollbar等功能。同时把iscroll扩展到了***Zepto***的原型中。
  635. */
  636. /**
  637. * @name iScroll
  638. * @grammar new iScroll(el,[options]) ⇒ self
  639. * @grammar $('selecotr').iScroll([options]) ⇒ zepto实例
  640. * @desc 将iScroll加入到了***$.fn***中,方便用Zepto的方式调用iScroll。
  641. * **el**
  642. * - ***el {String/ElementNode}*** iscroll容器节点
  643. *
  644. * **Options**
  645. * - ***hScroll*** {Boolean}: (可选, 默认: true)横向是否可以滚动
  646. * - ***vScroll*** {Boolean}: (可选, 默认: true)竖向是否可以滚动
  647. * - ***momentum*** {Boolean}: (可选, 默认: true)是否带有滚动效果
  648. * - ***checkDOMChanges*** {Boolean, 默认: false}: (可选)每个500毫秒判断一下滚动区域的容器是否有新追加的内容,如果有就调用refresh重新渲染一次
  649. * - ***useTransition*** {Boolean, 默认: false}: (可选)是否使用css3来来实现动画,默认是false,建议开启
  650. * - ***topOffset*** {Number}: (可选, 默认: 0)可滚动区域头部缩紧多少高度,默认是0, ***主要用于头部下拉加载更多时,收起头部的提示按钮***
  651. * @example
  652. * $('div').iscroll().find('selector').atrr({'name':'aaa'}) //保持链式调用
  653. * $('div').iScroll('refresh');//调用iScroll的方法
  654. * $('div').iScroll('scrollTo', 0, 0, 200);//调用iScroll的方法, 200ms内滚动到顶部
  655. */
  656. /**
  657. * @name destroy
  658. * @desc 销毁iScroll实例,在原iScroll的destroy的基础上对创建的dom元素进行了销毁
  659. * @grammar destroy() ⇒ undefined
  660. */
  661. /**
  662. * @name refresh
  663. * @desc 更新iScroll实例,在滚动的内容增减时,或者可滚动区域发生变化时需要调用***refresh***方法来纠正。
  664. * @grammar refresh() ⇒ undefined
  665. */
  666. /**
  667. * @name scrollTo
  668. * @desc 使iScroll实例,在指定时间内滚动到指定的位置, 如果relative为true, 说明x, y的值是相对与当前位置的。
  669. * @grammar scrollTo(x, y, time, relative) ⇒ undefined
  670. */
  671. /**
  672. * @name scrollToElement
  673. * @desc 滚动到指定内部元素
  674. * @grammar scrollToElement(element, time) ⇒ undefined
  675. * @grammar scrollToElement(selector, time) ⇒ undefined
  676. */
  677. /**
  678. * @name scrollToPage
  679. * @desc 跟scrollTo很像,这里传入的是百分比。
  680. * @grammar scrollToPage(pageX, pageY, time) ⇒ undefined
  681. */
  682. /**
  683. * @name disable
  684. * @desc 禁用iScroll
  685. * @grammar disable() ⇒ undefined
  686. */
  687. /**
  688. * @name enable
  689. * @desc 启用iScroll
  690. * @grammar enable() ⇒ undefined
  691. */
  692. /**
  693. * @name stop
  694. * @desc 定制iscroll滚动
  695. * @grammar stop() ⇒ undefined
  696. */