iscroll-infinite.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. /*! iScroll v5.1.3 ~ (c) 2008-2014 Matteo Spinelli ~ http://cubiq.org/license */
  2. (function (window, document, Math) {
  3. var rAF = window.requestAnimationFrame ||
  4. window.webkitRequestAnimationFrame ||
  5. window.mozRequestAnimationFrame ||
  6. window.oRequestAnimationFrame ||
  7. window.msRequestAnimationFrame ||
  8. function (callback) { window.setTimeout(callback, 1000 / 60); };
  9. var utils = (function () {
  10. var me = {};
  11. var _elementStyle = document.createElement('div').style;
  12. var _vendor = (function () {
  13. var vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT'],
  14. transform,
  15. i = 0,
  16. l = vendors.length;
  17. for ( ; i < l; i++ ) {
  18. transform = vendors[i] + 'ransform';
  19. if ( transform in _elementStyle ) return vendors[i].substr(0, vendors[i].length-1);
  20. }
  21. return false;
  22. })();
  23. function _prefixStyle (style) {
  24. if ( _vendor === false ) return false;
  25. if ( _vendor === '' ) return style;
  26. return _vendor + style.charAt(0).toUpperCase() + style.substr(1);
  27. }
  28. me.getTime = Date.now || function getTime () { return new Date().getTime(); };
  29. me.extend = function (target, obj) {
  30. for ( var i in obj ) {
  31. target[i] = obj[i];
  32. }
  33. };
  34. me.addEvent = function (el, type, fn, capture) {
  35. el.addEventListener(type, fn, !!capture);
  36. };
  37. me.removeEvent = function (el, type, fn, capture) {
  38. el.removeEventListener(type, fn, !!capture);
  39. };
  40. me.prefixPointerEvent = function (pointerEvent) {
  41. return window.MSPointerEvent ?
  42. 'MSPointer' + pointerEvent.charAt(9).toUpperCase() + pointerEvent.substr(10):
  43. pointerEvent;
  44. };
  45. me.momentum = function (current, start, time, lowerMargin, wrapperSize, deceleration) {
  46. var distance = current - start,
  47. speed = Math.abs(distance) / time,
  48. destination,
  49. duration;
  50. deceleration = deceleration === undefined ? 0.0006 : deceleration;
  51. destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
  52. duration = speed / deceleration;
  53. if ( destination < lowerMargin ) {
  54. destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
  55. distance = Math.abs(destination - current);
  56. duration = distance / speed;
  57. } else if ( destination > 0 ) {
  58. destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
  59. distance = Math.abs(current) + destination;
  60. duration = distance / speed;
  61. }
  62. return {
  63. destination: Math.round(destination),
  64. duration: duration
  65. };
  66. };
  67. var _transform = _prefixStyle('transform');
  68. me.extend(me, {
  69. hasTransform: _transform !== false,
  70. hasPerspective: _prefixStyle('perspective') in _elementStyle,
  71. hasTouch: 'ontouchstart' in window,
  72. hasPointer: window.PointerEvent || window.MSPointerEvent, // IE10 is prefixed
  73. hasTransition: _prefixStyle('transition') in _elementStyle
  74. });
  75. // This should find all Android browsers lower than build 535.19 (both stock browser and webview)
  76. me.isBadAndroid = /Android /.test(window.navigator.appVersion) && !(/Chrome\/\d/.test(window.navigator.appVersion));
  77. me.extend(me.style = {}, {
  78. transform: _transform,
  79. transitionTimingFunction: _prefixStyle('transitionTimingFunction'),
  80. transitionDuration: _prefixStyle('transitionDuration'),
  81. transitionDelay: _prefixStyle('transitionDelay'),
  82. transformOrigin: _prefixStyle('transformOrigin')
  83. });
  84. me.hasClass = function (e, c) {
  85. var re = new RegExp("(^|\\s)" + c + "(\\s|$)");
  86. return re.test(e.className);
  87. };
  88. me.addClass = function (e, c) {
  89. if ( me.hasClass(e, c) ) {
  90. return;
  91. }
  92. var newclass = e.className.split(' ');
  93. newclass.push(c);
  94. e.className = newclass.join(' ');
  95. };
  96. me.removeClass = function (e, c) {
  97. if ( !me.hasClass(e, c) ) {
  98. return;
  99. }
  100. var re = new RegExp("(^|\\s)" + c + "(\\s|$)", 'g');
  101. e.className = e.className.replace(re, ' ');
  102. };
  103. me.offset = function (el) {
  104. var left = -el.offsetLeft,
  105. top = -el.offsetTop;
  106. // jshint -W084
  107. while (el = el.offsetParent) {
  108. left -= el.offsetLeft;
  109. top -= el.offsetTop;
  110. }
  111. // jshint +W084
  112. return {
  113. left: left,
  114. top: top
  115. };
  116. };
  117. me.preventDefaultException = function (el, exceptions) {
  118. for ( var i in exceptions ) {
  119. if ( exceptions[i].test(el[i]) ) {
  120. return true;
  121. }
  122. }
  123. return false;
  124. };
  125. me.extend(me.eventType = {}, {
  126. touchstart: 1,
  127. touchmove: 1,
  128. touchend: 1,
  129. mousedown: 2,
  130. mousemove: 2,
  131. mouseup: 2,
  132. pointerdown: 3,
  133. pointermove: 3,
  134. pointerup: 3,
  135. MSPointerDown: 3,
  136. MSPointerMove: 3,
  137. MSPointerUp: 3
  138. });
  139. me.extend(me.ease = {}, {
  140. quadratic: {
  141. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  142. fn: function (k) {
  143. return k * ( 2 - k );
  144. }
  145. },
  146. circular: {
  147. style: 'cubic-bezier(0.1, 0.57, 0.1, 1)', // Not properly "circular" but this looks better, it should be (0.075, 0.82, 0.165, 1)
  148. fn: function (k) {
  149. return Math.sqrt( 1 - ( --k * k ) );
  150. }
  151. },
  152. back: {
  153. style: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)',
  154. fn: function (k) {
  155. var b = 4;
  156. return ( k = k - 1 ) * k * ( ( b + 1 ) * k + b ) + 1;
  157. }
  158. },
  159. bounce: {
  160. style: '',
  161. fn: function (k) {
  162. if ( ( k /= 1 ) < ( 1 / 2.75 ) ) {
  163. return 7.5625 * k * k;
  164. } else if ( k < ( 2 / 2.75 ) ) {
  165. return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
  166. } else if ( k < ( 2.5 / 2.75 ) ) {
  167. return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
  168. } else {
  169. return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
  170. }
  171. }
  172. },
  173. elastic: {
  174. style: '',
  175. fn: function (k) {
  176. var f = 0.22,
  177. e = 0.4;
  178. if ( k === 0 ) { return 0; }
  179. if ( k == 1 ) { return 1; }
  180. return ( e * Math.pow( 2, - 10 * k ) * Math.sin( ( k - f / 4 ) * ( 2 * Math.PI ) / f ) + 1 );
  181. }
  182. }
  183. });
  184. me.tap = function (e, eventName) {
  185. var ev = document.createEvent('Event');
  186. ev.initEvent(eventName, true, true);
  187. ev.pageX = e.pageX;
  188. ev.pageY = e.pageY;
  189. e.target.dispatchEvent(ev);
  190. };
  191. me.click = function (e) {
  192. var target = e.target,
  193. ev;
  194. if ( !(/(SELECT|INPUT|TEXTAREA)/i).test(target.tagName) ) {
  195. ev = document.createEvent('MouseEvents');
  196. ev.initMouseEvent('click', true, true, e.view, 1,
  197. target.screenX, target.screenY, target.clientX, target.clientY,
  198. e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
  199. 0, null);
  200. ev._constructed = true;
  201. target.dispatchEvent(ev);
  202. }
  203. };
  204. return me;
  205. })();
  206. function IScroll (el, options) {
  207. this.wrapper = typeof el == 'string' ? document.querySelector(el) : el;
  208. this.scroller = this.wrapper.children[0];
  209. this.scrollerStyle = this.scroller.style; // cache style for better performance
  210. this.options = {
  211. mouseWheelSpeed: 20,
  212. snapThreshold: 0.334,
  213. infiniteUseTransform: true,
  214. deceleration: 0.004,
  215. // INSERT POINT: OPTIONS
  216. startX: 0,
  217. startY: 0,
  218. scrollY: true,
  219. directionLockThreshold: 5,
  220. momentum: true,
  221. bounce: true,
  222. bounceTime: 600,
  223. bounceEasing: '',
  224. preventDefault: true,
  225. preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ },
  226. HWCompositing: true,
  227. useTransition: true,
  228. useTransform: true
  229. };
  230. for ( var i in options ) {
  231. this.options[i] = options[i];
  232. }
  233. // Normalize options
  234. this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
  235. this.options.useTransition = utils.hasTransition && this.options.useTransition;
  236. this.options.useTransform = utils.hasTransform && this.options.useTransform;
  237. this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
  238. this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
  239. // If you want eventPassthrough I have to lock one of the axes
  240. this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
  241. this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
  242. // With eventPassthrough we also need lockDirection mechanism
  243. this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
  244. this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
  245. this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
  246. this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling;
  247. if ( this.options.tap === true ) {
  248. this.options.tap = 'tap';
  249. }
  250. this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
  251. if ( this.options.infiniteElements ) {
  252. this.options.probeType = 3;
  253. }
  254. this.options.infiniteUseTransform = this.options.infiniteUseTransform && this.options.useTransform;
  255. if ( this.options.probeType == 3 ) {
  256. this.options.useTransition = false; }
  257. // INSERT POINT: NORMALIZATION
  258. // Some defaults
  259. this.x = 0;
  260. this.y = 0;
  261. this.directionX = 0;
  262. this.directionY = 0;
  263. this._events = {};
  264. // INSERT POINT: DEFAULTS
  265. this._init();
  266. this.refresh();
  267. this.scrollTo(this.options.startX, this.options.startY);
  268. this.enable();
  269. }
  270. IScroll.prototype = {
  271. version: '5.1.3',
  272. _init: function () {
  273. this._initEvents();
  274. if ( this.options.mouseWheel ) {
  275. this._initWheel();
  276. }
  277. if ( this.options.snap ) {
  278. this._initSnap();
  279. }
  280. if ( this.options.keyBindings ) {
  281. this._initKeys();
  282. }
  283. if ( this.options.infiniteElements ) {
  284. this._initInfinite();
  285. }
  286. // INSERT POINT: _init
  287. },
  288. destroy: function () {
  289. this._initEvents(true);
  290. this._execEvent('destroy');
  291. },
  292. _transitionEnd: function (e) {
  293. if ( e.target != this.scroller || !this.isInTransition ) {
  294. return;
  295. }
  296. this._transitionTime();
  297. if ( !this.resetPosition(this.options.bounceTime) ) {
  298. this.isInTransition = false;
  299. this._execEvent('scrollEnd');
  300. }
  301. },
  302. _start: function (e) {
  303. // React to left mouse button only
  304. if ( utils.eventType[e.type] != 1 ) {
  305. if ( e.button !== 0 ) {
  306. return;
  307. }
  308. }
  309. if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
  310. return;
  311. }
  312. if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  313. e.preventDefault();
  314. }
  315. var point = e.touches ? e.touches[0] : e,
  316. pos;
  317. this.initiated = utils.eventType[e.type];
  318. this.moved = false;
  319. this.distX = 0;
  320. this.distY = 0;
  321. this.directionX = 0;
  322. this.directionY = 0;
  323. this.directionLocked = 0;
  324. this._transitionTime();
  325. this.startTime = utils.getTime();
  326. if ( this.options.useTransition && this.isInTransition ) {
  327. this.isInTransition = false;
  328. pos = this.getComputedPosition();
  329. this._translate(Math.round(pos.x), Math.round(pos.y));
  330. this._execEvent('scrollEnd');
  331. } else if ( !this.options.useTransition && this.isAnimating ) {
  332. this.isAnimating = false;
  333. this._execEvent('scrollEnd');
  334. }
  335. this.startX = this.x;
  336. this.startY = this.y;
  337. this.absStartX = this.x;
  338. this.absStartY = this.y;
  339. this.pointX = point.pageX;
  340. this.pointY = point.pageY;
  341. this._execEvent('beforeScrollStart');
  342. },
  343. _move: function (e) {
  344. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  345. return;
  346. }
  347. if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
  348. e.preventDefault();
  349. }
  350. var point = e.touches ? e.touches[0] : e,
  351. deltaX = point.pageX - this.pointX,
  352. deltaY = point.pageY - this.pointY,
  353. timestamp = utils.getTime(),
  354. newX, newY,
  355. absDistX, absDistY;
  356. this.pointX = point.pageX;
  357. this.pointY = point.pageY;
  358. this.distX += deltaX;
  359. this.distY += deltaY;
  360. absDistX = Math.abs(this.distX);
  361. absDistY = Math.abs(this.distY);
  362. // We need to move at least 10 pixels for the scrolling to initiate
  363. if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
  364. return;
  365. }
  366. // If you are scrolling in one direction lock the other
  367. if ( !this.directionLocked && !this.options.freeScroll ) {
  368. if ( absDistX > absDistY + this.options.directionLockThreshold ) {
  369. this.directionLocked = 'h'; // lock horizontally
  370. } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
  371. this.directionLocked = 'v'; // lock vertically
  372. } else {
  373. this.directionLocked = 'n'; // no lock
  374. }
  375. }
  376. if ( this.directionLocked == 'h' ) {
  377. if ( this.options.eventPassthrough == 'vertical' ) {
  378. e.preventDefault();
  379. } else if ( this.options.eventPassthrough == 'horizontal' ) {
  380. this.initiated = false;
  381. return;
  382. }
  383. deltaY = 0;
  384. } else if ( this.directionLocked == 'v' ) {
  385. if ( this.options.eventPassthrough == 'horizontal' ) {
  386. e.preventDefault();
  387. } else if ( this.options.eventPassthrough == 'vertical' ) {
  388. this.initiated = false;
  389. return;
  390. }
  391. deltaX = 0;
  392. }
  393. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  394. deltaY = this.hasVerticalScroll ? deltaY : 0;
  395. newX = this.x + deltaX;
  396. newY = this.y + deltaY;
  397. // Slow down if outside of the boundaries
  398. if ( newX > 0 || newX < this.maxScrollX ) {
  399. newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
  400. }
  401. if ( newY > 0 || newY < this.maxScrollY ) {
  402. newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
  403. }
  404. this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
  405. this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
  406. if ( !this.moved ) {
  407. this._execEvent('scrollStart');
  408. }
  409. this.moved = true;
  410. this._translate(newX, newY);
  411. /* REPLACE START: _move */
  412. if ( timestamp - this.startTime > 300 ) {
  413. this.startTime = timestamp;
  414. this.startX = this.x;
  415. this.startY = this.y;
  416. if ( this.options.probeType == 1 ) {
  417. this._execEvent('scroll');
  418. }
  419. }
  420. if ( this.options.probeType > 1 ) {
  421. this._execEvent('scroll');
  422. }
  423. /* REPLACE END: _move */
  424. },
  425. _end: function (e) {
  426. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  427. return;
  428. }
  429. if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  430. e.preventDefault();
  431. }
  432. var point = e.changedTouches ? e.changedTouches[0] : e,
  433. momentumX,
  434. momentumY,
  435. duration = utils.getTime() - this.startTime,
  436. newX = Math.round(this.x),
  437. newY = Math.round(this.y),
  438. distanceX = Math.abs(newX - this.startX),
  439. distanceY = Math.abs(newY - this.startY),
  440. time = 0,
  441. easing = '';
  442. this.isInTransition = 0;
  443. this.initiated = 0;
  444. this.endTime = utils.getTime();
  445. // reset if we are outside of the boundaries
  446. if ( this.resetPosition(this.options.bounceTime) ) {
  447. return;
  448. }
  449. this.scrollTo(newX, newY); // ensures that the last position is rounded
  450. // we scrolled less than 10 pixels
  451. if ( !this.moved ) {
  452. if ( this.options.tap ) {
  453. utils.tap(e, this.options.tap);
  454. }
  455. if ( this.options.click ) {
  456. utils.click(e);
  457. }
  458. this._execEvent('scrollCancel');
  459. return;
  460. }
  461. if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) {
  462. this._execEvent('flick');
  463. return;
  464. }
  465. // start momentum animation if needed
  466. if ( this.options.momentum && duration < 300 ) {
  467. momentumX = this.hasHorizontalScroll ? utils.momentum(this.x, this.startX, duration, this.maxScrollX, this.options.bounce ? this.wrapperWidth : 0, this.options.deceleration) : { destination: newX, duration: 0 };
  468. momentumY = this.hasVerticalScroll ? utils.momentum(this.y, this.startY, duration, this.maxScrollY, this.options.bounce ? this.wrapperHeight : 0, this.options.deceleration) : { destination: newY, duration: 0 };
  469. newX = momentumX.destination;
  470. newY = momentumY.destination;
  471. time = Math.max(momentumX.duration, momentumY.duration);
  472. this.isInTransition = 1;
  473. }
  474. if ( this.options.snap ) {
  475. var snap = this._nearestSnap(newX, newY);
  476. this.currentPage = snap;
  477. time = this.options.snapSpeed || Math.max(
  478. Math.max(
  479. Math.min(Math.abs(newX - snap.x), 1000),
  480. Math.min(Math.abs(newY - snap.y), 1000)
  481. ), 300);
  482. newX = snap.x;
  483. newY = snap.y;
  484. this.directionX = 0;
  485. this.directionY = 0;
  486. easing = this.options.bounceEasing;
  487. }
  488. // INSERT POINT: _end
  489. if ( newX != this.x || newY != this.y ) {
  490. // change easing function when scroller goes out of the boundaries
  491. if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
  492. easing = utils.ease.quadratic;
  493. }
  494. this.scrollTo(newX, newY, time, easing);
  495. return;
  496. }
  497. this._execEvent('scrollEnd');
  498. },
  499. _resize: function () {
  500. var that = this;
  501. clearTimeout(this.resizeTimeout);
  502. this.resizeTimeout = setTimeout(function () {
  503. that.refresh();
  504. }, this.options.resizePolling);
  505. },
  506. resetPosition: function (time) {
  507. var x = this.x,
  508. y = this.y;
  509. time = time || 0;
  510. if ( !this.hasHorizontalScroll || this.x > 0 ) {
  511. x = 0;
  512. } else if ( this.x < this.maxScrollX ) {
  513. x = this.maxScrollX;
  514. }
  515. if ( !this.hasVerticalScroll || this.y > 0 ) {
  516. y = 0;
  517. } else if ( this.y < this.maxScrollY ) {
  518. y = this.maxScrollY;
  519. }
  520. if ( x == this.x && y == this.y ) {
  521. return false;
  522. }
  523. this.scrollTo(x, y, time, this.options.bounceEasing);
  524. return true;
  525. },
  526. disable: function () {
  527. this.enabled = false;
  528. },
  529. enable: function () {
  530. this.enabled = true;
  531. },
  532. refresh: function () {
  533. var rf = this.wrapper.offsetHeight; // Force reflow
  534. this.wrapperWidth = this.wrapper.clientWidth;
  535. this.wrapperHeight = this.wrapper.clientHeight;
  536. /* REPLACE START: refresh */
  537. this.scrollerWidth = this.scroller.offsetWidth;
  538. this.scrollerHeight = this.scroller.offsetHeight;
  539. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  540. var limit;
  541. if ( this.options.infiniteElements ) {
  542. this.options.infiniteLimit = this.options.infiniteLimit || Math.floor(2147483645 / this.infiniteElementHeight);
  543. limit = -this.options.infiniteLimit * this.infiniteElementHeight + this.wrapperHeight;
  544. }
  545. this.maxScrollY = limit !== undefined ? limit : this.wrapperHeight - this.scrollerHeight;
  546. /* REPLACE END: refresh */
  547. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
  548. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
  549. if ( !this.hasHorizontalScroll ) {
  550. this.maxScrollX = 0;
  551. this.scrollerWidth = this.wrapperWidth;
  552. }
  553. if ( !this.hasVerticalScroll ) {
  554. this.maxScrollY = 0;
  555. this.scrollerHeight = this.wrapperHeight;
  556. }
  557. this.endTime = 0;
  558. this.directionX = 0;
  559. this.directionY = 0;
  560. this.wrapperOffset = utils.offset(this.wrapper);
  561. this._execEvent('refresh');
  562. this.resetPosition();
  563. // INSERT POINT: _refresh
  564. },
  565. on: function (type, fn) {
  566. if ( !this._events[type] ) {
  567. this._events[type] = [];
  568. }
  569. this._events[type].push(fn);
  570. },
  571. off: function (type, fn) {
  572. if ( !this._events[type] ) {
  573. return;
  574. }
  575. var index = this._events[type].indexOf(fn);
  576. if ( index > -1 ) {
  577. this._events[type].splice(index, 1);
  578. }
  579. },
  580. _execEvent: function (type) {
  581. if ( !this._events[type] ) {
  582. return;
  583. }
  584. var i = 0,
  585. l = this._events[type].length;
  586. if ( !l ) {
  587. return;
  588. }
  589. for ( ; i < l; i++ ) {
  590. this._events[type][i].apply(this, [].slice.call(arguments, 1));
  591. }
  592. },
  593. scrollBy: function (x, y, time, easing) {
  594. x = this.x + x;
  595. y = this.y + y;
  596. time = time || 0;
  597. this.scrollTo(x, y, time, easing);
  598. },
  599. scrollTo: function (x, y, time, easing) {
  600. easing = easing || utils.ease.circular;
  601. this.isInTransition = this.options.useTransition && time > 0;
  602. if ( !time || (this.options.useTransition && easing.style) ) {
  603. this._transitionTimingFunction(easing.style);
  604. this._transitionTime(time);
  605. this._translate(x, y);
  606. } else {
  607. this._animate(x, y, time, easing.fn);
  608. }
  609. },
  610. scrollToElement: function (el, time, offsetX, offsetY, easing) {
  611. el = el.nodeType ? el : this.scroller.querySelector(el);
  612. if ( !el ) {
  613. return;
  614. }
  615. var pos = utils.offset(el);
  616. pos.left -= this.wrapperOffset.left;
  617. pos.top -= this.wrapperOffset.top;
  618. // if offsetX/Y are true we center the element to the screen
  619. if ( offsetX === true ) {
  620. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  621. }
  622. if ( offsetY === true ) {
  623. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  624. }
  625. pos.left -= offsetX || 0;
  626. pos.top -= offsetY || 0;
  627. pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  628. pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  629. time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
  630. this.scrollTo(pos.left, pos.top, time, easing);
  631. },
  632. _transitionTime: function (time) {
  633. time = time || 0;
  634. this.scrollerStyle[utils.style.transitionDuration] = time + 'ms';
  635. if ( !time && utils.isBadAndroid ) {
  636. this.scrollerStyle[utils.style.transitionDuration] = '0.001s';
  637. }
  638. // INSERT POINT: _transitionTime
  639. },
  640. _transitionTimingFunction: function (easing) {
  641. this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
  642. // INSERT POINT: _transitionTimingFunction
  643. },
  644. _translate: function (x, y) {
  645. if ( this.options.useTransform ) {
  646. /* REPLACE START: _translate */
  647. this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
  648. /* REPLACE END: _translate */
  649. } else {
  650. x = Math.round(x);
  651. y = Math.round(y);
  652. this.scrollerStyle.left = x + 'px';
  653. this.scrollerStyle.top = y + 'px';
  654. }
  655. this.x = x;
  656. this.y = y;
  657. // INSERT POINT: _translate
  658. },
  659. _initEvents: function (remove) {
  660. var eventType = remove ? utils.removeEvent : utils.addEvent,
  661. target = this.options.bindToWrapper ? this.wrapper : window;
  662. eventType(window, 'orientationchange', this);
  663. eventType(window, 'resize', this);
  664. if ( this.options.click ) {
  665. eventType(this.wrapper, 'click', this, true);
  666. }
  667. if ( !this.options.disableMouse ) {
  668. eventType(this.wrapper, 'mousedown', this);
  669. eventType(target, 'mousemove', this);
  670. eventType(target, 'mousecancel', this);
  671. eventType(target, 'mouseup', this);
  672. }
  673. if ( utils.hasPointer && !this.options.disablePointer ) {
  674. eventType(this.wrapper, utils.prefixPointerEvent('pointerdown'), this);
  675. eventType(target, utils.prefixPointerEvent('pointermove'), this);
  676. eventType(target, utils.prefixPointerEvent('pointercancel'), this);
  677. eventType(target, utils.prefixPointerEvent('pointerup'), this);
  678. }
  679. if ( utils.hasTouch && !this.options.disableTouch ) {
  680. eventType(this.wrapper, 'touchstart', this);
  681. eventType(target, 'touchmove', this);
  682. eventType(target, 'touchcancel', this);
  683. eventType(target, 'touchend', this);
  684. }
  685. eventType(this.scroller, 'transitionend', this);
  686. eventType(this.scroller, 'webkitTransitionEnd', this);
  687. eventType(this.scroller, 'oTransitionEnd', this);
  688. eventType(this.scroller, 'MSTransitionEnd', this);
  689. },
  690. getComputedPosition: function () {
  691. var matrix = window.getComputedStyle(this.scroller, null),
  692. x, y;
  693. if ( this.options.useTransform ) {
  694. matrix = matrix[utils.style.transform].split(')')[0].split(', ');
  695. x = +(matrix[12] || matrix[4]);
  696. y = +(matrix[13] || matrix[5]);
  697. } else {
  698. x = +matrix.left.replace(/[^-\d.]/g, '');
  699. y = +matrix.top.replace(/[^-\d.]/g, '');
  700. }
  701. return { x: x, y: y };
  702. },
  703. _initWheel: function () {
  704. utils.addEvent(this.wrapper, 'wheel', this);
  705. utils.addEvent(this.wrapper, 'mousewheel', this);
  706. utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
  707. this.on('destroy', function () {
  708. utils.removeEvent(this.wrapper, 'wheel', this);
  709. utils.removeEvent(this.wrapper, 'mousewheel', this);
  710. utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
  711. });
  712. },
  713. _wheel: function (e) {
  714. if ( !this.enabled ) {
  715. return;
  716. }
  717. e.preventDefault();
  718. e.stopPropagation();
  719. var wheelDeltaX, wheelDeltaY,
  720. newX, newY,
  721. that = this;
  722. if ( this.wheelTimeout === undefined ) {
  723. that._execEvent('scrollStart');
  724. }
  725. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  726. clearTimeout(this.wheelTimeout);
  727. this.wheelTimeout = setTimeout(function () {
  728. that._execEvent('scrollEnd');
  729. that.wheelTimeout = undefined;
  730. }, 400);
  731. if ( 'deltaX' in e ) {
  732. if (e.deltaMode === 1) {
  733. wheelDeltaX = -e.deltaX * this.options.mouseWheelSpeed;
  734. wheelDeltaY = -e.deltaY * this.options.mouseWheelSpeed;
  735. } else {
  736. wheelDeltaX = -e.deltaX;
  737. wheelDeltaY = -e.deltaY;
  738. }
  739. } else if ( 'wheelDeltaX' in e ) {
  740. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  741. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  742. } else if ( 'wheelDelta' in e ) {
  743. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  744. } else if ( 'detail' in e ) {
  745. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  746. } else {
  747. return;
  748. }
  749. wheelDeltaX *= this.options.invertWheelDirection;
  750. wheelDeltaY *= this.options.invertWheelDirection;
  751. if ( !this.hasVerticalScroll ) {
  752. wheelDeltaX = wheelDeltaY;
  753. wheelDeltaY = 0;
  754. }
  755. if ( this.options.snap ) {
  756. newX = this.currentPage.pageX;
  757. newY = this.currentPage.pageY;
  758. if ( wheelDeltaX > 0 ) {
  759. newX--;
  760. } else if ( wheelDeltaX < 0 ) {
  761. newX++;
  762. }
  763. if ( wheelDeltaY > 0 ) {
  764. newY--;
  765. } else if ( wheelDeltaY < 0 ) {
  766. newY++;
  767. }
  768. this.goToPage(newX, newY);
  769. return;
  770. }
  771. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  772. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  773. if ( newX > 0 ) {
  774. newX = 0;
  775. } else if ( newX < this.maxScrollX ) {
  776. newX = this.maxScrollX;
  777. }
  778. if ( newY > 0 ) {
  779. newY = 0;
  780. } else if ( newY < this.maxScrollY ) {
  781. newY = this.maxScrollY;
  782. }
  783. this.scrollTo(newX, newY, 0);
  784. if ( this.options.probeType > 1 ) {
  785. this._execEvent('scroll');
  786. }
  787. // INSERT POINT: _wheel
  788. },
  789. _initSnap: function () {
  790. this.currentPage = {};
  791. if ( typeof this.options.snap == 'string' ) {
  792. this.options.snap = this.scroller.querySelectorAll(this.options.snap);
  793. }
  794. this.on('refresh', function () {
  795. var i = 0, l,
  796. m = 0, n,
  797. cx, cy,
  798. x = 0, y,
  799. stepX = this.options.snapStepX || this.wrapperWidth,
  800. stepY = this.options.snapStepY || this.wrapperHeight,
  801. el;
  802. this.pages = [];
  803. if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
  804. return;
  805. }
  806. if ( this.options.snap === true ) {
  807. cx = Math.round( stepX / 2 );
  808. cy = Math.round( stepY / 2 );
  809. while ( x > -this.scrollerWidth ) {
  810. this.pages[i] = [];
  811. l = 0;
  812. y = 0;
  813. while ( y > -this.scrollerHeight ) {
  814. this.pages[i][l] = {
  815. x: Math.max(x, this.maxScrollX),
  816. y: Math.max(y, this.maxScrollY),
  817. width: stepX,
  818. height: stepY,
  819. cx: x - cx,
  820. cy: y - cy
  821. };
  822. y -= stepY;
  823. l++;
  824. }
  825. x -= stepX;
  826. i++;
  827. }
  828. } else {
  829. el = this.options.snap;
  830. l = el.length;
  831. n = -1;
  832. for ( ; i < l; i++ ) {
  833. if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
  834. m = 0;
  835. n++;
  836. }
  837. if ( !this.pages[m] ) {
  838. this.pages[m] = [];
  839. }
  840. x = Math.max(-el[i].offsetLeft, this.maxScrollX);
  841. y = Math.max(-el[i].offsetTop, this.maxScrollY);
  842. cx = x - Math.round(el[i].offsetWidth / 2);
  843. cy = y - Math.round(el[i].offsetHeight / 2);
  844. this.pages[m][n] = {
  845. x: x,
  846. y: y,
  847. width: el[i].offsetWidth,
  848. height: el[i].offsetHeight,
  849. cx: cx,
  850. cy: cy
  851. };
  852. if ( x > this.maxScrollX ) {
  853. m++;
  854. }
  855. }
  856. }
  857. this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
  858. // Update snap threshold if needed
  859. if ( this.options.snapThreshold % 1 === 0 ) {
  860. this.snapThresholdX = this.options.snapThreshold;
  861. this.snapThresholdY = this.options.snapThreshold;
  862. } else {
  863. this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
  864. this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
  865. }
  866. });
  867. this.on('flick', function () {
  868. var time = this.options.snapSpeed || Math.max(
  869. Math.max(
  870. Math.min(Math.abs(this.x - this.startX), 1000),
  871. Math.min(Math.abs(this.y - this.startY), 1000)
  872. ), 300);
  873. this.goToPage(
  874. this.currentPage.pageX + this.directionX,
  875. this.currentPage.pageY + this.directionY,
  876. time
  877. );
  878. });
  879. },
  880. _nearestSnap: function (x, y) {
  881. if ( !this.pages.length ) {
  882. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  883. }
  884. var i = 0,
  885. l = this.pages.length,
  886. m = 0;
  887. // Check if we exceeded the snap threshold
  888. if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
  889. Math.abs(y - this.absStartY) < this.snapThresholdY ) {
  890. return this.currentPage;
  891. }
  892. if ( x > 0 ) {
  893. x = 0;
  894. } else if ( x < this.maxScrollX ) {
  895. x = this.maxScrollX;
  896. }
  897. if ( y > 0 ) {
  898. y = 0;
  899. } else if ( y < this.maxScrollY ) {
  900. y = this.maxScrollY;
  901. }
  902. for ( ; i < l; i++ ) {
  903. if ( x >= this.pages[i][0].cx ) {
  904. x = this.pages[i][0].x;
  905. break;
  906. }
  907. }
  908. l = this.pages[i].length;
  909. for ( ; m < l; m++ ) {
  910. if ( y >= this.pages[0][m].cy ) {
  911. y = this.pages[0][m].y;
  912. break;
  913. }
  914. }
  915. if ( i == this.currentPage.pageX ) {
  916. i += this.directionX;
  917. if ( i < 0 ) {
  918. i = 0;
  919. } else if ( i >= this.pages.length ) {
  920. i = this.pages.length - 1;
  921. }
  922. x = this.pages[i][0].x;
  923. }
  924. if ( m == this.currentPage.pageY ) {
  925. m += this.directionY;
  926. if ( m < 0 ) {
  927. m = 0;
  928. } else if ( m >= this.pages[0].length ) {
  929. m = this.pages[0].length - 1;
  930. }
  931. y = this.pages[0][m].y;
  932. }
  933. return {
  934. x: x,
  935. y: y,
  936. pageX: i,
  937. pageY: m
  938. };
  939. },
  940. goToPage: function (x, y, time, easing) {
  941. easing = easing || this.options.bounceEasing;
  942. if ( x >= this.pages.length ) {
  943. x = this.pages.length - 1;
  944. } else if ( x < 0 ) {
  945. x = 0;
  946. }
  947. if ( y >= this.pages[x].length ) {
  948. y = this.pages[x].length - 1;
  949. } else if ( y < 0 ) {
  950. y = 0;
  951. }
  952. var posX = this.pages[x][y].x,
  953. posY = this.pages[x][y].y;
  954. time = time === undefined ? this.options.snapSpeed || Math.max(
  955. Math.max(
  956. Math.min(Math.abs(posX - this.x), 1000),
  957. Math.min(Math.abs(posY - this.y), 1000)
  958. ), 300) : time;
  959. this.currentPage = {
  960. x: posX,
  961. y: posY,
  962. pageX: x,
  963. pageY: y
  964. };
  965. this.scrollTo(posX, posY, time, easing);
  966. },
  967. next: function (time, easing) {
  968. var x = this.currentPage.pageX,
  969. y = this.currentPage.pageY;
  970. x++;
  971. if ( x >= this.pages.length && this.hasVerticalScroll ) {
  972. x = 0;
  973. y++;
  974. }
  975. this.goToPage(x, y, time, easing);
  976. },
  977. prev: function (time, easing) {
  978. var x = this.currentPage.pageX,
  979. y = this.currentPage.pageY;
  980. x--;
  981. if ( x < 0 && this.hasVerticalScroll ) {
  982. x = 0;
  983. y--;
  984. }
  985. this.goToPage(x, y, time, easing);
  986. },
  987. _initKeys: function (e) {
  988. // default key bindings
  989. var keys = {
  990. pageUp: 33,
  991. pageDown: 34,
  992. end: 35,
  993. home: 36,
  994. left: 37,
  995. up: 38,
  996. right: 39,
  997. down: 40
  998. };
  999. var i;
  1000. // if you give me characters I give you keycode
  1001. if ( typeof this.options.keyBindings == 'object' ) {
  1002. for ( i in this.options.keyBindings ) {
  1003. if ( typeof this.options.keyBindings[i] == 'string' ) {
  1004. this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
  1005. }
  1006. }
  1007. } else {
  1008. this.options.keyBindings = {};
  1009. }
  1010. for ( i in keys ) {
  1011. this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
  1012. }
  1013. utils.addEvent(window, 'keydown', this);
  1014. this.on('destroy', function () {
  1015. utils.removeEvent(window, 'keydown', this);
  1016. });
  1017. },
  1018. _key: function (e) {
  1019. if ( !this.enabled ) {
  1020. return;
  1021. }
  1022. var snap = this.options.snap, // we are using this alot, better to cache it
  1023. newX = snap ? this.currentPage.pageX : this.x,
  1024. newY = snap ? this.currentPage.pageY : this.y,
  1025. now = utils.getTime(),
  1026. prevTime = this.keyTime || 0,
  1027. acceleration = 0.250,
  1028. pos;
  1029. if ( this.options.useTransition && this.isInTransition ) {
  1030. pos = this.getComputedPosition();
  1031. this._translate(Math.round(pos.x), Math.round(pos.y));
  1032. this.isInTransition = false;
  1033. }
  1034. this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
  1035. switch ( e.keyCode ) {
  1036. case this.options.keyBindings.pageUp:
  1037. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1038. newX += snap ? 1 : this.wrapperWidth;
  1039. } else {
  1040. newY += snap ? 1 : this.wrapperHeight;
  1041. }
  1042. break;
  1043. case this.options.keyBindings.pageDown:
  1044. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1045. newX -= snap ? 1 : this.wrapperWidth;
  1046. } else {
  1047. newY -= snap ? 1 : this.wrapperHeight;
  1048. }
  1049. break;
  1050. case this.options.keyBindings.end:
  1051. newX = snap ? this.pages.length-1 : this.maxScrollX;
  1052. newY = snap ? this.pages[0].length-1 : this.maxScrollY;
  1053. break;
  1054. case this.options.keyBindings.home:
  1055. newX = 0;
  1056. newY = 0;
  1057. break;
  1058. case this.options.keyBindings.left:
  1059. newX += snap ? -1 : 5 + this.keyAcceleration>>0;
  1060. break;
  1061. case this.options.keyBindings.up:
  1062. newY += snap ? 1 : 5 + this.keyAcceleration>>0;
  1063. break;
  1064. case this.options.keyBindings.right:
  1065. newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
  1066. break;
  1067. case this.options.keyBindings.down:
  1068. newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
  1069. break;
  1070. default:
  1071. return;
  1072. }
  1073. if ( snap ) {
  1074. this.goToPage(newX, newY);
  1075. return;
  1076. }
  1077. if ( newX > 0 ) {
  1078. newX = 0;
  1079. this.keyAcceleration = 0;
  1080. } else if ( newX < this.maxScrollX ) {
  1081. newX = this.maxScrollX;
  1082. this.keyAcceleration = 0;
  1083. }
  1084. if ( newY > 0 ) {
  1085. newY = 0;
  1086. this.keyAcceleration = 0;
  1087. } else if ( newY < this.maxScrollY ) {
  1088. newY = this.maxScrollY;
  1089. this.keyAcceleration = 0;
  1090. }
  1091. this.scrollTo(newX, newY, 0);
  1092. this.keyTime = now;
  1093. },
  1094. _animate: function (destX, destY, duration, easingFn) {
  1095. var that = this,
  1096. startX = this.x,
  1097. startY = this.y,
  1098. startTime = utils.getTime(),
  1099. destTime = startTime + duration;
  1100. function step () {
  1101. var now = utils.getTime(),
  1102. newX, newY,
  1103. easing;
  1104. if ( now >= destTime ) {
  1105. that.isAnimating = false;
  1106. that._translate(destX, destY);
  1107. if ( !that.resetPosition(that.options.bounceTime) ) {
  1108. that._execEvent('scrollEnd');
  1109. }
  1110. return;
  1111. }
  1112. now = ( now - startTime ) / duration;
  1113. easing = easingFn(now);
  1114. newX = ( destX - startX ) * easing + startX;
  1115. newY = ( destY - startY ) * easing + startY;
  1116. that._translate(newX, newY);
  1117. if ( that.isAnimating ) {
  1118. rAF(step);
  1119. }
  1120. if ( that.options.probeType == 3 ) {
  1121. that._execEvent('scroll');
  1122. }
  1123. }
  1124. this.isAnimating = true;
  1125. step();
  1126. },
  1127. _initInfinite: function () {
  1128. var el = this.options.infiniteElements;
  1129. this.infiniteElements = typeof el == 'string' ? document.querySelectorAll(el) : el;
  1130. this.infiniteLength = this.infiniteElements.length;
  1131. this.infiniteMaster = this.infiniteElements[0];
  1132. this.infiniteElementHeight = this.infiniteMaster.offsetHeight;
  1133. this.infiniteHeight = this.infiniteLength * this.infiniteElementHeight;
  1134. this.options.cacheSize = this.options.cacheSize || 1000;
  1135. this.infiniteCacheBuffer = Math.round(this.options.cacheSize / 4);
  1136. //this.infiniteCache = {};
  1137. this.options.dataset.call(this, 0, this.options.cacheSize);
  1138. this.on('refresh', function () {
  1139. var elementsPerPage = Math.ceil(this.wrapperHeight / this.infiniteElementHeight);
  1140. this.infiniteUpperBufferSize = Math.floor((this.infiniteLength - elementsPerPage) / 2);
  1141. this.reorderInfinite();
  1142. });
  1143. this.on('scroll', this.reorderInfinite);
  1144. },
  1145. // TO-DO: clean up the mess
  1146. reorderInfinite: function () {
  1147. var center = -this.y + this.wrapperHeight / 2;
  1148. var minorPhase = Math.max(Math.floor(-this.y / this.infiniteElementHeight) - this.infiniteUpperBufferSize, 0),
  1149. majorPhase = Math.floor(minorPhase / this.infiniteLength),
  1150. phase = minorPhase - majorPhase * this.infiniteLength;
  1151. var top = 0;
  1152. var i = 0;
  1153. var update = [];
  1154. //var cachePhase = Math.floor((minorPhase + this.infiniteLength / 2) / this.infiniteCacheBuffer);
  1155. var cachePhase = Math.floor(minorPhase / this.infiniteCacheBuffer);
  1156. while ( i < this.infiniteLength ) {
  1157. top = i * this.infiniteElementHeight + majorPhase * this.infiniteHeight;
  1158. if ( phase > i ) {
  1159. top += this.infiniteElementHeight * this.infiniteLength;
  1160. }
  1161. if ( this.infiniteElements[i]._top !== top ) {
  1162. this.infiniteElements[i]._phase = top / this.infiniteElementHeight;
  1163. if ( this.infiniteElements[i]._phase < this.options.infiniteLimit ) {
  1164. this.infiniteElements[i]._top = top;
  1165. if ( this.options.infiniteUseTransform ) {
  1166. this.infiniteElements[i].style[utils.style.transform] = 'translate(0, ' + top + 'px)' + this.translateZ;
  1167. } else {
  1168. this.infiniteElements[i].style.top = top + 'px';
  1169. }
  1170. update.push(this.infiniteElements[i]);
  1171. }
  1172. }
  1173. i++;
  1174. }
  1175. if ( this.cachePhase != cachePhase && (cachePhase === 0 || minorPhase - this.infiniteCacheBuffer > 0) ) {
  1176. this.options.dataset.call(this, Math.max(cachePhase * this.infiniteCacheBuffer - this.infiniteCacheBuffer, 0), this.options.cacheSize);
  1177. }
  1178. this.cachePhase = cachePhase;
  1179. this.updateContent(update);
  1180. },
  1181. updateContent: function (els) {
  1182. if ( this.infiniteCache === undefined ) {
  1183. return;
  1184. }
  1185. for ( var i = 0, l = els.length; i < l; i++ ) {
  1186. this.options.dataFiller.call(this, els[i], this.infiniteCache[els[i]._phase]);
  1187. }
  1188. },
  1189. updateCache: function (start, data) {
  1190. var firstRun = this.infiniteCache === undefined;
  1191. this.infiniteCache = {};
  1192. for ( var i = 0, l = data.length; i < l; i++ ) {
  1193. this.infiniteCache[start++] = data[i];
  1194. }
  1195. if ( firstRun ) {
  1196. this.updateContent(this.infiniteElements);
  1197. }
  1198. },
  1199. handleEvent: function (e) {
  1200. switch ( e.type ) {
  1201. case 'touchstart':
  1202. case 'pointerdown':
  1203. case 'MSPointerDown':
  1204. case 'mousedown':
  1205. this._start(e);
  1206. break;
  1207. case 'touchmove':
  1208. case 'pointermove':
  1209. case 'MSPointerMove':
  1210. case 'mousemove':
  1211. this._move(e);
  1212. break;
  1213. case 'touchend':
  1214. case 'pointerup':
  1215. case 'MSPointerUp':
  1216. case 'mouseup':
  1217. case 'touchcancel':
  1218. case 'pointercancel':
  1219. case 'MSPointerCancel':
  1220. case 'mousecancel':
  1221. this._end(e);
  1222. break;
  1223. case 'orientationchange':
  1224. case 'resize':
  1225. this._resize();
  1226. break;
  1227. case 'transitionend':
  1228. case 'webkitTransitionEnd':
  1229. case 'oTransitionEnd':
  1230. case 'MSTransitionEnd':
  1231. this._transitionEnd(e);
  1232. break;
  1233. case 'wheel':
  1234. case 'DOMMouseScroll':
  1235. case 'mousewheel':
  1236. this._wheel(e);
  1237. break;
  1238. case 'keydown':
  1239. this._key(e);
  1240. break;
  1241. case 'click':
  1242. if ( !e._constructed ) {
  1243. e.preventDefault();
  1244. e.stopPropagation();
  1245. }
  1246. break;
  1247. }
  1248. }
  1249. };
  1250. IScroll.utils = utils;
  1251. if ( typeof module != 'undefined' && module.exports ) {
  1252. module.exports = IScroll;
  1253. } else {
  1254. window.IScroll = IScroll;
  1255. }
  1256. })(window, document, Math);