iscroll-probe-wids.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  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. resizeScrollbars: true,
  212. mouseWheelSpeed: 20,
  213. snapThreshold: 0.334,
  214. // INSERT POINT: OPTIONS
  215. probeType: 3,
  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. if ( this.options.shrinkScrollbars == 'scale' ) {
  251. this.options.useTransition = false;
  252. }
  253. this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
  254. if ( this.options.probeType == 3 ) {
  255. this.options.useTransition = false; }
  256. // INSERT POINT: NORMALIZATION
  257. // Some defaults
  258. this.x = 0;
  259. this.y = 0;
  260. this.directionX = 0;
  261. this.directionY = 0;
  262. this._events = {};
  263. // INSERT POINT: DEFAULTS
  264. this._init();
  265. this.refresh();
  266. this.scrollTo(this.options.startX, this.options.startY);
  267. this.enable();
  268. }
  269. IScroll.prototype = {
  270. version: '5.1.3',
  271. _init: function () {
  272. this._initEvents();
  273. if ( this.options.scrollbars || this.options.indicators ) {
  274. this._initIndicators();
  275. }
  276. if ( this.options.mouseWheel ) {
  277. this._initWheel();
  278. }
  279. if ( this.options.snap ) {
  280. this._initSnap();
  281. }
  282. if ( this.options.keyBindings ) {
  283. this._initKeys();
  284. }
  285. // INSERT POINT: _init
  286. },
  287. destroy: function () {
  288. this._initEvents(true);
  289. this._execEvent('destroy');
  290. },
  291. _transitionEnd: function (e) {
  292. if ( e.target != this.scroller || !this.isInTransition ) {
  293. return;
  294. }
  295. this._transitionTime();
  296. if ( !this.resetPosition(this.options.bounceTime) ) {
  297. this.isInTransition = false;
  298. this._execEvent('scrollEnd');
  299. }
  300. },
  301. _start: function (e) {
  302. // React to left mouse button only
  303. if ( utils.eventType[e.type] != 1 ) {
  304. if ( e.button !== 0 ) {
  305. return;
  306. }
  307. }
  308. if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
  309. return;
  310. }
  311. if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  312. e.preventDefault();
  313. }
  314. var point = e.touches ? e.touches[0] : e,
  315. pos;
  316. this.initiated = utils.eventType[e.type];
  317. this.moved = false;
  318. this.distX = 0;
  319. this.distY = 0;
  320. this.directionX = 0;
  321. this.directionY = 0;
  322. this.directionLocked = 0;
  323. this._transitionTime();
  324. this.startTime = utils.getTime();
  325. if ( this.options.useTransition && this.isInTransition ) {
  326. this.isInTransition = false;
  327. pos = this.getComputedPosition();
  328. this._translate(Math.round(pos.x), Math.round(pos.y));
  329. this._execEvent('scrollEnd');
  330. } else if ( !this.options.useTransition && this.isAnimating ) {
  331. this.isAnimating = false;
  332. this._execEvent('scrollEnd');
  333. }
  334. this.startX = this.x;
  335. this.startY = this.y;
  336. this.absStartX = this.x;
  337. this.absStartY = this.y;
  338. this.pointX = point.pageX;
  339. this.pointY = point.pageY;
  340. this._execEvent('beforeScrollStart');
  341. },
  342. _move: function (e) {
  343. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  344. return;
  345. }
  346. if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
  347. e.preventDefault();
  348. }
  349. var point = e.touches ? e.touches[0] : e,
  350. deltaX = point.pageX - this.pointX,
  351. deltaY = point.pageY - this.pointY,
  352. timestamp = utils.getTime(),
  353. newX, newY,
  354. absDistX, absDistY;
  355. this.pointX = point.pageX;
  356. this.pointY = point.pageY;
  357. this.distX += deltaX;
  358. this.distY += deltaY;
  359. absDistX = Math.abs(this.distX);
  360. absDistY = Math.abs(this.distY);
  361. // We need to move at least 10 pixels for the scrolling to initiate
  362. if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
  363. return;
  364. }
  365. // If you are scrolling in one direction lock the other
  366. if ( !this.directionLocked && !this.options.freeScroll ) {
  367. if ( absDistX > absDistY + this.options.directionLockThreshold ) {
  368. this.directionLocked = 'h'; // lock horizontally
  369. } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
  370. this.directionLocked = 'v'; // lock vertically
  371. } else {
  372. this.directionLocked = 'n'; // no lock
  373. }
  374. }
  375. if ( this.directionLocked == 'h' ) {
  376. if ( this.options.eventPassthrough == 'vertical' ) {
  377. e.preventDefault();
  378. } else if ( this.options.eventPassthrough == 'horizontal' ) {
  379. this.initiated = false;
  380. return;
  381. }
  382. deltaY = 0;
  383. } else if ( this.directionLocked == 'v' ) {
  384. if ( this.options.eventPassthrough == 'horizontal' ) {
  385. e.preventDefault();
  386. } else if ( this.options.eventPassthrough == 'vertical' ) {
  387. this.initiated = false;
  388. return;
  389. }
  390. deltaX = 0;
  391. }
  392. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  393. deltaY = this.hasVerticalScroll ? deltaY : 0;
  394. newX = this.x + deltaX;
  395. newY = this.y + deltaY;
  396. // Slow down if outside of the boundaries
  397. if ( newX > 0 || newX < this.maxScrollX ) {
  398. newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
  399. }
  400. if ( newY > 0 || newY < this.maxScrollY ) {
  401. newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
  402. }
  403. this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
  404. this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
  405. if ( !this.moved ) {
  406. this._execEvent('scrollStart');
  407. }
  408. this.moved = true;
  409. this._translate(newX, newY);
  410. /* REPLACE START: _move */
  411. if ( timestamp - this.startTime > 300 ) {
  412. this.startTime = timestamp;
  413. this.startX = this.x;
  414. this.startY = this.y;
  415. if ( this.options.probeType == 1 ) {
  416. this._execEvent('scroll');
  417. }
  418. }
  419. if ( this.options.probeType > 1 ) {
  420. this._execEvent('scroll');
  421. }
  422. /* REPLACE END: _move */
  423. },
  424. _end: function (e) {
  425. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  426. return;
  427. }
  428. if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  429. e.preventDefault();
  430. }
  431. this._execEvent('beforeScrollEnd');
  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, 0) : { 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, 0) : { 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. y = this.y >= 0 || this.maxScrollY > 0 ? 0 : this.y < this.maxScrollY ? this.maxScrollY : this.y;
  521. if ( x == this.x && y == this.y ) {
  522. return false;
  523. }
  524. this.scrollTo(x, y, time, this.options.bounceEasing);
  525. return true;
  526. },
  527. disable: function () {
  528. this.enabled = false;
  529. },
  530. enable: function () {
  531. this.enabled = true;
  532. },
  533. refresh: function () {
  534. var rf = this.wrapper.offsetHeight; // Force reflow
  535. this.wrapperWidth = this.wrapper.clientWidth;
  536. this.wrapperHeight = this.wrapper.clientHeight;
  537. /* REPLACE START: refresh */
  538. this.scrollerWidth = this.scroller.offsetWidth;
  539. this.scrollerHeight = this.scroller.offsetHeight;
  540. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  541. this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
  542. /* REPLACE END: refresh */
  543. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
  544. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
  545. if ( !this.hasHorizontalScroll ) {
  546. this.maxScrollX = 0;
  547. this.scrollerWidth = this.wrapperWidth;
  548. }
  549. if ( !this.hasVerticalScroll ) {
  550. this.maxScrollY = 0;
  551. this.scrollerHeight = this.wrapperHeight;
  552. }
  553. this.endTime = 0;
  554. this.directionX = 0;
  555. this.directionY = 0;
  556. this.wrapperOffset = utils.offset(this.wrapper);
  557. this._execEvent('refresh');
  558. this.resetPosition();
  559. // INSERT POINT: _refresh
  560. },
  561. on: function (type, fn) {
  562. if ( !this._events[type] ) {
  563. this._events[type] = [];
  564. }
  565. this._events[type].push(fn);
  566. },
  567. off: function (type, fn) {
  568. if ( !this._events[type] ) {
  569. return;
  570. }
  571. var index = this._events[type].indexOf(fn);
  572. if ( index > -1 ) {
  573. this._events[type].splice(index, 1);
  574. }
  575. },
  576. _execEvent: function (type) {
  577. if ( !this._events[type] ) {
  578. return;
  579. }
  580. var i = 0,
  581. l = this._events[type].length;
  582. if ( !l ) {
  583. return;
  584. }
  585. for ( ; i < l; i++ ) {
  586. this._events[type][i].apply(this, [].slice.call(arguments, 1));
  587. }
  588. },
  589. scrollBy: function (x, y, time, easing) {
  590. x = this.x + x;
  591. y = this.y + y;
  592. time = time || 0;
  593. this.scrollTo(x, y, time, easing);
  594. },
  595. scrollTo: function (x, y, time, easing) {
  596. easing = easing || utils.ease.circular;
  597. this.isInTransition = this.options.useTransition && time > 0;
  598. if ( !time || (this.options.useTransition && easing.style) ) {
  599. this._transitionTimingFunction(easing.style);
  600. this._transitionTime(time);
  601. this._translate(x, y);
  602. } else {
  603. this._animate(x, y, time, easing.fn);
  604. }
  605. },
  606. scrollToElement: function (el, time, offsetX, offsetY, easing) {
  607. el = el.nodeType ? el : this.scroller.querySelector(el);
  608. if ( !el ) {
  609. return;
  610. }
  611. var pos = utils.offset(el);
  612. pos.left -= this.wrapperOffset.left;
  613. pos.top -= this.wrapperOffset.top;
  614. // if offsetX/Y are true we center the element to the screen
  615. if ( offsetX === true ) {
  616. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  617. }
  618. if ( offsetY === true ) {
  619. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  620. }
  621. pos.left -= offsetX || 0;
  622. pos.top -= offsetY || 0;
  623. pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  624. pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  625. time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
  626. this.scrollTo(pos.left, pos.top, time, easing);
  627. },
  628. _transitionTime: function (time) {
  629. time = time || 0;
  630. this.scrollerStyle[utils.style.transitionDuration] = time + 'ms';
  631. if ( !time && utils.isBadAndroid ) {
  632. this.scrollerStyle[utils.style.transitionDuration] = '0.001s';
  633. }
  634. if ( this.indicators ) {
  635. for ( var i = this.indicators.length; i--; ) {
  636. this.indicators[i].transitionTime(time);
  637. }
  638. }
  639. // INSERT POINT: _transitionTime
  640. },
  641. _transitionTimingFunction: function (easing) {
  642. this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
  643. if ( this.indicators ) {
  644. for ( var i = this.indicators.length; i--; ) {
  645. this.indicators[i].transitionTimingFunction(easing);
  646. }
  647. }
  648. // INSERT POINT: _transitionTimingFunction
  649. },
  650. _translate: function (x, y) {
  651. if ( this.options.useTransform ) {
  652. /* REPLACE START: _translate */
  653. this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
  654. /* REPLACE END: _translate */
  655. } else {
  656. x = Math.round(x);
  657. y = Math.round(y);
  658. this.scrollerStyle.left = x + 'px';
  659. this.scrollerStyle.top = y + 'px';
  660. }
  661. this.x = x;
  662. this.y = y;
  663. if ( this.indicators ) {
  664. for ( var i = this.indicators.length; i--; ) {
  665. this.indicators[i].updatePosition();
  666. }
  667. }
  668. // INSERT POINT: _translate
  669. },
  670. _initEvents: function (remove) {
  671. var eventType = remove ? utils.removeEvent : utils.addEvent,
  672. target = this.options.bindToWrapper ? this.wrapper : window;
  673. eventType(window, 'orientationchange', this);
  674. eventType(window, 'resize', this);
  675. if ( this.options.click ) {
  676. eventType(this.wrapper, 'click', this, true);
  677. }
  678. if ( !this.options.disableMouse ) {
  679. eventType(this.wrapper, 'mousedown', this);
  680. eventType(target, 'mousemove', this);
  681. eventType(target, 'mousecancel', this);
  682. eventType(target, 'mouseup', this);
  683. }
  684. if ( utils.hasPointer && !this.options.disablePointer ) {
  685. eventType(this.wrapper, utils.prefixPointerEvent('pointerdown'), this);
  686. eventType(target, utils.prefixPointerEvent('pointermove'), this);
  687. eventType(target, utils.prefixPointerEvent('pointercancel'), this);
  688. eventType(target, utils.prefixPointerEvent('pointerup'), this);
  689. }
  690. if ( utils.hasTouch && !this.options.disableTouch ) {
  691. eventType(this.wrapper, 'touchstart', this);
  692. eventType(target, 'touchmove', this);
  693. eventType(target, 'touchcancel', this);
  694. eventType(target, 'touchend', this);
  695. }
  696. eventType(this.scroller, 'transitionend', this);
  697. eventType(this.scroller, 'webkitTransitionEnd', this);
  698. eventType(this.scroller, 'oTransitionEnd', this);
  699. eventType(this.scroller, 'MSTransitionEnd', this);
  700. },
  701. getComputedPosition: function () {
  702. var matrix = window.getComputedStyle(this.scroller, null),
  703. x, y;
  704. if ( this.options.useTransform ) {
  705. matrix = matrix[utils.style.transform].split(')')[0].split(', ');
  706. x = +(matrix[12] || matrix[4]);
  707. y = +(matrix[13] || matrix[5]);
  708. } else {
  709. x = +matrix.left.replace(/[^-\d.]/g, '');
  710. y = +matrix.top.replace(/[^-\d.]/g, '');
  711. }
  712. return { x: x, y: y };
  713. },
  714. _initIndicators: function () {
  715. var interactive = this.options.interactiveScrollbars,
  716. customStyle = typeof this.options.scrollbars != 'string',
  717. indicators = [],
  718. indicator;
  719. var that = this;
  720. this.indicators = [];
  721. if ( this.options.scrollbars ) {
  722. // Vertical scrollbar
  723. if ( this.options.scrollY ) {
  724. indicator = {
  725. el: createDefaultScrollbar('v', interactive, this.options.scrollbars),
  726. interactive: interactive,
  727. defaultScrollbars: true,
  728. customStyle: customStyle,
  729. resize: this.options.resizeScrollbars,
  730. shrink: this.options.shrinkScrollbars,
  731. fade: this.options.fadeScrollbars,
  732. listenX: false
  733. };
  734. this.wrapper.appendChild(indicator.el);
  735. indicators.push(indicator);
  736. }
  737. // Horizontal scrollbar
  738. if ( this.options.scrollX ) {
  739. indicator = {
  740. el: createDefaultScrollbar('h', interactive, this.options.scrollbars),
  741. interactive: interactive,
  742. defaultScrollbars: true,
  743. customStyle: customStyle,
  744. resize: this.options.resizeScrollbars,
  745. shrink: this.options.shrinkScrollbars,
  746. fade: this.options.fadeScrollbars,
  747. listenY: false
  748. };
  749. this.wrapper.appendChild(indicator.el);
  750. indicators.push(indicator);
  751. }
  752. }
  753. if ( this.options.indicators ) {
  754. // TODO: check concat compatibility
  755. indicators = indicators.concat(this.options.indicators);
  756. }
  757. for ( var i = indicators.length; i--; ) {
  758. this.indicators.push( new Indicator(this, indicators[i]) );
  759. }
  760. // TODO: check if we can use array.map (wide compatibility and performance issues)
  761. function _indicatorsMap (fn) {
  762. for ( var i = that.indicators.length; i--; ) {
  763. fn.call(that.indicators[i]);
  764. }
  765. }
  766. if ( this.options.fadeScrollbars ) {
  767. this.on('scrollEnd', function () {
  768. _indicatorsMap(function () {
  769. this.fade();
  770. });
  771. });
  772. this.on('scrollCancel', function () {
  773. _indicatorsMap(function () {
  774. this.fade();
  775. });
  776. });
  777. this.on('scrollStart', function () {
  778. _indicatorsMap(function () {
  779. this.fade(1);
  780. });
  781. });
  782. this.on('beforeScrollStart', function () {
  783. _indicatorsMap(function () {
  784. this.fade(1, true);
  785. });
  786. });
  787. }
  788. this.on('refresh', function () {
  789. _indicatorsMap(function () {
  790. this.refresh();
  791. });
  792. });
  793. this.on('destroy', function () {
  794. _indicatorsMap(function () {
  795. this.destroy();
  796. });
  797. delete this.indicators;
  798. });
  799. },
  800. _initWheel: function () {
  801. utils.addEvent(this.wrapper, 'wheel', this);
  802. utils.addEvent(this.wrapper, 'mousewheel', this);
  803. utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
  804. this.on('destroy', function () {
  805. utils.removeEvent(this.wrapper, 'wheel', this);
  806. utils.removeEvent(this.wrapper, 'mousewheel', this);
  807. utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
  808. });
  809. },
  810. _wheel: function (e) {
  811. if ( !this.enabled ) {
  812. return;
  813. }
  814. e.preventDefault();
  815. e.stopPropagation();
  816. var wheelDeltaX, wheelDeltaY,
  817. newX, newY,
  818. that = this;
  819. if ( this.wheelTimeout === undefined ) {
  820. that._execEvent('scrollStart');
  821. }
  822. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  823. clearTimeout(this.wheelTimeout);
  824. this.wheelTimeout = setTimeout(function () {
  825. that._execEvent('scrollEnd');
  826. that.wheelTimeout = undefined;
  827. }, 400);
  828. if ( 'deltaX' in e ) {
  829. if (e.deltaMode === 1) {
  830. wheelDeltaX = -e.deltaX * this.options.mouseWheelSpeed;
  831. wheelDeltaY = -e.deltaY * this.options.mouseWheelSpeed;
  832. } else {
  833. wheelDeltaX = -e.deltaX;
  834. wheelDeltaY = -e.deltaY;
  835. }
  836. } else if ( 'wheelDeltaX' in e ) {
  837. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  838. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  839. } else if ( 'wheelDelta' in e ) {
  840. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  841. } else if ( 'detail' in e ) {
  842. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  843. } else {
  844. return;
  845. }
  846. wheelDeltaX *= this.options.invertWheelDirection;
  847. wheelDeltaY *= this.options.invertWheelDirection;
  848. if ( !this.hasVerticalScroll ) {
  849. wheelDeltaX = wheelDeltaY;
  850. wheelDeltaY = 0;
  851. }
  852. if ( this.options.snap ) {
  853. newX = this.currentPage.pageX;
  854. newY = this.currentPage.pageY;
  855. if ( wheelDeltaX > 0 ) {
  856. newX--;
  857. } else if ( wheelDeltaX < 0 ) {
  858. newX++;
  859. }
  860. if ( wheelDeltaY > 0 ) {
  861. newY--;
  862. } else if ( wheelDeltaY < 0 ) {
  863. newY++;
  864. }
  865. this.goToPage(newX, newY);
  866. return;
  867. }
  868. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  869. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  870. if ( newX > 0 ) {
  871. newX = 0;
  872. } else if ( newX < this.maxScrollX ) {
  873. newX = this.maxScrollX;
  874. }
  875. if ( newY > 0 ) {
  876. newY = 0;
  877. } else if ( newY < this.maxScrollY ) {
  878. newY = this.maxScrollY;
  879. }
  880. this.scrollTo(newX, newY, 0);
  881. if ( this.options.probeType > 1 ) {
  882. this._execEvent('scroll');
  883. }
  884. // INSERT POINT: _wheel
  885. },
  886. _initSnap: function () {
  887. this.currentPage = {};
  888. if ( typeof this.options.snap == 'string' ) {
  889. this.options.snap = this.scroller.querySelectorAll(this.options.snap);
  890. }
  891. this.on('refresh', function () {
  892. var i = 0, l,
  893. m = 0, n,
  894. cx, cy,
  895. x = 0, y,
  896. stepX = this.options.snapStepX || this.wrapperWidth,
  897. stepY = this.options.snapStepY || this.wrapperHeight,
  898. el;
  899. this.pages = [];
  900. if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
  901. return;
  902. }
  903. if ( this.options.snap === true ) {
  904. cx = Math.round( stepX / 2 );
  905. cy = Math.round( stepY / 2 );
  906. while ( x > -this.scrollerWidth ) {
  907. this.pages[i] = [];
  908. l = 0;
  909. y = 0;
  910. while ( y > -this.scrollerHeight ) {
  911. this.pages[i][l] = {
  912. x: Math.max(x, this.maxScrollX),
  913. y: Math.max(y, this.maxScrollY),
  914. width: stepX,
  915. height: stepY,
  916. cx: x - cx,
  917. cy: y - cy
  918. };
  919. y -= stepY;
  920. l++;
  921. }
  922. x -= stepX;
  923. i++;
  924. }
  925. } else {
  926. el = this.options.snap;
  927. l = el.length;
  928. n = -1;
  929. for ( ; i < l; i++ ) {
  930. if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
  931. m = 0;
  932. n++;
  933. }
  934. if ( !this.pages[m] ) {
  935. this.pages[m] = [];
  936. }
  937. x = Math.max(-el[i].offsetLeft, this.maxScrollX);
  938. y = Math.max(-el[i].offsetTop, this.maxScrollY);
  939. cx = x - Math.round(el[i].offsetWidth / 2);
  940. cy = y - Math.round(el[i].offsetHeight / 2);
  941. this.pages[m][n] = {
  942. x: x,
  943. y: y,
  944. width: el[i].offsetWidth,
  945. height: el[i].offsetHeight,
  946. cx: cx,
  947. cy: cy
  948. };
  949. if ( x > this.maxScrollX ) {
  950. m++;
  951. }
  952. }
  953. }
  954. this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
  955. // Update snap threshold if needed
  956. if ( this.options.snapThreshold % 1 === 0 ) {
  957. this.snapThresholdX = this.options.snapThreshold;
  958. this.snapThresholdY = this.options.snapThreshold;
  959. } else {
  960. this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
  961. this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
  962. }
  963. });
  964. this.on('flick', function () {
  965. var time = this.options.snapSpeed || Math.max(
  966. Math.max(
  967. Math.min(Math.abs(this.x - this.startX), 1000),
  968. Math.min(Math.abs(this.y - this.startY), 1000)
  969. ), 300);
  970. this.goToPage(
  971. this.currentPage.pageX + this.directionX,
  972. this.currentPage.pageY + this.directionY,
  973. time
  974. );
  975. });
  976. },
  977. _nearestSnap: function (x, y) {
  978. if ( !this.pages.length ) {
  979. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  980. }
  981. var i = 0,
  982. l = this.pages.length,
  983. m = 0;
  984. // Check if we exceeded the snap threshold
  985. if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
  986. Math.abs(y - this.absStartY) < this.snapThresholdY ) {
  987. return this.currentPage;
  988. }
  989. if ( x > 0 ) {
  990. x = 0;
  991. } else if ( x < this.maxScrollX ) {
  992. x = this.maxScrollX;
  993. }
  994. if ( y > 0 ) {
  995. y = 0;
  996. } else if ( y < this.maxScrollY ) {
  997. y = this.maxScrollY;
  998. }
  999. for ( ; i < l; i++ ) {
  1000. if ( x >= this.pages[i][0].cx ) {
  1001. x = this.pages[i][0].x;
  1002. break;
  1003. }
  1004. }
  1005. l = this.pages[i].length;
  1006. for ( ; m < l; m++ ) {
  1007. if ( y >= this.pages[0][m].cy ) {
  1008. y = this.pages[0][m].y;
  1009. break;
  1010. }
  1011. }
  1012. if ( i == this.currentPage.pageX ) {
  1013. i += this.directionX;
  1014. if ( i < 0 ) {
  1015. i = 0;
  1016. } else if ( i >= this.pages.length ) {
  1017. i = this.pages.length - 1;
  1018. }
  1019. x = this.pages[i][0].x;
  1020. }
  1021. if ( m == this.currentPage.pageY ) {
  1022. m += this.directionY;
  1023. if ( m < 0 ) {
  1024. m = 0;
  1025. } else if ( m >= this.pages[0].length ) {
  1026. m = this.pages[0].length - 1;
  1027. }
  1028. y = this.pages[0][m].y;
  1029. }
  1030. return {
  1031. x: x,
  1032. y: y,
  1033. pageX: i,
  1034. pageY: m
  1035. };
  1036. },
  1037. goToPage: function (x, y, time, easing) {
  1038. easing = easing || this.options.bounceEasing;
  1039. if ( x >= this.pages.length ) {
  1040. x = this.pages.length - 1;
  1041. } else if ( x < 0 ) {
  1042. x = 0;
  1043. }
  1044. if ( y >= this.pages[x].length ) {
  1045. y = this.pages[x].length - 1;
  1046. } else if ( y < 0 ) {
  1047. y = 0;
  1048. }
  1049. var posX = this.pages[x][y].x,
  1050. posY = this.pages[x][y].y;
  1051. time = time === undefined ? this.options.snapSpeed || Math.max(
  1052. Math.max(
  1053. Math.min(Math.abs(posX - this.x), 1000),
  1054. Math.min(Math.abs(posY - this.y), 1000)
  1055. ), 300) : time;
  1056. this.currentPage = {
  1057. x: posX,
  1058. y: posY,
  1059. pageX: x,
  1060. pageY: y
  1061. };
  1062. this.scrollTo(posX, posY, time, easing);
  1063. },
  1064. next: function (time, easing) {
  1065. var x = this.currentPage.pageX,
  1066. y = this.currentPage.pageY;
  1067. x++;
  1068. if ( x >= this.pages.length && this.hasVerticalScroll ) {
  1069. x = 0;
  1070. y++;
  1071. }
  1072. this.goToPage(x, y, time, easing);
  1073. },
  1074. prev: function (time, easing) {
  1075. var x = this.currentPage.pageX,
  1076. y = this.currentPage.pageY;
  1077. x--;
  1078. if ( x < 0 && this.hasVerticalScroll ) {
  1079. x = 0;
  1080. y--;
  1081. }
  1082. this.goToPage(x, y, time, easing);
  1083. },
  1084. _initKeys: function (e) {
  1085. // default key bindings
  1086. var keys = {
  1087. pageUp: 33,
  1088. pageDown: 34,
  1089. end: 35,
  1090. home: 36,
  1091. left: 37,
  1092. up: 38,
  1093. right: 39,
  1094. down: 40
  1095. };
  1096. var i;
  1097. // if you give me characters I give you keycode
  1098. if ( typeof this.options.keyBindings == 'object' ) {
  1099. for ( i in this.options.keyBindings ) {
  1100. if ( typeof this.options.keyBindings[i] == 'string' ) {
  1101. this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
  1102. }
  1103. }
  1104. } else {
  1105. this.options.keyBindings = {};
  1106. }
  1107. for ( i in keys ) {
  1108. this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
  1109. }
  1110. utils.addEvent(window, 'keydown', this);
  1111. this.on('destroy', function () {
  1112. utils.removeEvent(window, 'keydown', this);
  1113. });
  1114. },
  1115. _key: function (e) {
  1116. if ( !this.enabled ) {
  1117. return;
  1118. }
  1119. var snap = this.options.snap, // we are using this alot, better to cache it
  1120. newX = snap ? this.currentPage.pageX : this.x,
  1121. newY = snap ? this.currentPage.pageY : this.y,
  1122. now = utils.getTime(),
  1123. prevTime = this.keyTime || 0,
  1124. acceleration = 0.250,
  1125. pos;
  1126. if ( this.options.useTransition && this.isInTransition ) {
  1127. pos = this.getComputedPosition();
  1128. this._translate(Math.round(pos.x), Math.round(pos.y));
  1129. this.isInTransition = false;
  1130. }
  1131. this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
  1132. switch ( e.keyCode ) {
  1133. case this.options.keyBindings.pageUp:
  1134. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1135. newX += snap ? 1 : this.wrapperWidth;
  1136. } else {
  1137. newY += snap ? 1 : this.wrapperHeight;
  1138. }
  1139. break;
  1140. case this.options.keyBindings.pageDown:
  1141. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1142. newX -= snap ? 1 : this.wrapperWidth;
  1143. } else {
  1144. newY -= snap ? 1 : this.wrapperHeight;
  1145. }
  1146. break;
  1147. case this.options.keyBindings.end:
  1148. newX = snap ? this.pages.length-1 : this.maxScrollX;
  1149. newY = snap ? this.pages[0].length-1 : this.maxScrollY;
  1150. break;
  1151. case this.options.keyBindings.home:
  1152. newX = 0;
  1153. newY = 0;
  1154. break;
  1155. case this.options.keyBindings.left:
  1156. newX += snap ? -1 : 5 + this.keyAcceleration>>0;
  1157. break;
  1158. case this.options.keyBindings.up:
  1159. newY += snap ? 1 : 5 + this.keyAcceleration>>0;
  1160. break;
  1161. case this.options.keyBindings.right:
  1162. newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
  1163. break;
  1164. case this.options.keyBindings.down:
  1165. newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
  1166. break;
  1167. default:
  1168. return;
  1169. }
  1170. if ( snap ) {
  1171. this.goToPage(newX, newY);
  1172. return;
  1173. }
  1174. if ( newX > 0 ) {
  1175. newX = 0;
  1176. this.keyAcceleration = 0;
  1177. } else if ( newX < this.maxScrollX ) {
  1178. newX = this.maxScrollX;
  1179. this.keyAcceleration = 0;
  1180. }
  1181. if ( newY > 0 ) {
  1182. newY = 0;
  1183. this.keyAcceleration = 0;
  1184. } else if ( newY < this.maxScrollY ) {
  1185. newY = this.maxScrollY;
  1186. this.keyAcceleration = 0;
  1187. }
  1188. this.scrollTo(newX, newY, 0);
  1189. this.keyTime = now;
  1190. },
  1191. _animate: function (destX, destY, duration, easingFn) {
  1192. var that = this,
  1193. startX = this.x,
  1194. startY = this.y,
  1195. startTime = utils.getTime(),
  1196. destTime = startTime + duration;
  1197. function step () {
  1198. var now = utils.getTime(),
  1199. newX, newY,
  1200. easing;
  1201. if ( now >= destTime ) {
  1202. that.isAnimating = false;
  1203. that._translate(destX, destY);
  1204. if ( !that.resetPosition(that.options.bounceTime) ) {
  1205. that._execEvent('scrollEnd');
  1206. }
  1207. return;
  1208. }
  1209. now = ( now - startTime ) / duration;
  1210. easing = easingFn(now);
  1211. newX = ( destX - startX ) * easing + startX;
  1212. newY = ( destY - startY ) * easing + startY;
  1213. that._translate(newX, newY);
  1214. if ( that.isAnimating ) {
  1215. rAF(step);
  1216. }
  1217. if ( that.options.probeType == 3 ) {
  1218. that._execEvent('scroll');
  1219. }
  1220. }
  1221. this.isAnimating = true;
  1222. step();
  1223. },
  1224. handleEvent: function (e) {
  1225. switch ( e.type ) {
  1226. case 'touchstart':
  1227. case 'pointerdown':
  1228. case 'MSPointerDown':
  1229. case 'mousedown':
  1230. this._start(e);
  1231. break;
  1232. case 'touchmove':
  1233. case 'pointermove':
  1234. case 'MSPointerMove':
  1235. case 'mousemove':
  1236. this._move(e);
  1237. break;
  1238. case 'touchend':
  1239. case 'pointerup':
  1240. case 'MSPointerUp':
  1241. case 'mouseup':
  1242. case 'touchcancel':
  1243. case 'pointercancel':
  1244. case 'MSPointerCancel':
  1245. case 'mousecancel':
  1246. this._end(e);
  1247. break;
  1248. case 'orientationchange':
  1249. case 'resize':
  1250. this._resize();
  1251. break;
  1252. case 'transitionend':
  1253. case 'webkitTransitionEnd':
  1254. case 'oTransitionEnd':
  1255. case 'MSTransitionEnd':
  1256. this._transitionEnd(e);
  1257. break;
  1258. case 'wheel':
  1259. case 'DOMMouseScroll':
  1260. case 'mousewheel':
  1261. this._wheel(e);
  1262. break;
  1263. case 'keydown':
  1264. this._key(e);
  1265. break;
  1266. case 'click':
  1267. if ( !e._constructed ) {
  1268. e.preventDefault();
  1269. e.stopPropagation();
  1270. }
  1271. break;
  1272. }
  1273. }
  1274. };
  1275. function createDefaultScrollbar (direction, interactive, type) {
  1276. var scrollbar = document.createElement('div'),
  1277. indicator = document.createElement('div');
  1278. if ( type === true ) {
  1279. scrollbar.style.cssText = 'position:absolute;z-index:9999';
  1280. indicator.style.cssText = '-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:absolute;background:rgba(0,0,0,0.4);border:1px solid rgba(255,255,255,0.9);border-radius:3px';
  1281. }
  1282. indicator.className = 'iScrollIndicator';
  1283. if ( direction == 'h' ) {
  1284. if ( type === true ) {
  1285. scrollbar.style.cssText += ';height:6px;left:2px;right:2px;bottom:0';
  1286. indicator.style.height = '100%';
  1287. }
  1288. scrollbar.className = 'iScrollHorizontalScrollbar';
  1289. } else {
  1290. if ( type === true ) {
  1291. scrollbar.style.cssText += ';width:6px;bottom:2px;top:2px;right:1px';
  1292. indicator.style.width = '100%';
  1293. }
  1294. scrollbar.className = 'iScrollVerticalScrollbar';
  1295. }
  1296. scrollbar.style.cssText += ';overflow:hidden';
  1297. if ( !interactive ) {
  1298. scrollbar.style.pointerEvents = 'none';
  1299. }
  1300. scrollbar.appendChild(indicator);
  1301. return scrollbar;
  1302. }
  1303. function Indicator (scroller, options) {
  1304. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  1305. this.wrapperStyle = this.wrapper.style;
  1306. this.indicator = this.wrapper.children[0];
  1307. this.indicatorStyle = this.indicator.style;
  1308. this.scroller = scroller;
  1309. this.options = {
  1310. listenX: true,
  1311. listenY: true,
  1312. interactive: false,
  1313. resize: true,
  1314. defaultScrollbars: false,
  1315. shrink: false,
  1316. fade: false,
  1317. speedRatioX: 0,
  1318. speedRatioY: 0
  1319. };
  1320. for ( var i in options ) {
  1321. this.options[i] = options[i];
  1322. }
  1323. this.sizeRatioX = 1;
  1324. this.sizeRatioY = 1;
  1325. this.maxPosX = 0;
  1326. this.maxPosY = 0;
  1327. if ( this.options.interactive ) {
  1328. if ( !this.options.disableTouch ) {
  1329. utils.addEvent(this.indicator, 'touchstart', this);
  1330. utils.addEvent(window, 'touchend', this);
  1331. }
  1332. if ( !this.options.disablePointer ) {
  1333. utils.addEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1334. utils.addEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1335. }
  1336. if ( !this.options.disableMouse ) {
  1337. utils.addEvent(this.indicator, 'mousedown', this);
  1338. utils.addEvent(window, 'mouseup', this);
  1339. }
  1340. }
  1341. if ( this.options.fade ) {
  1342. this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
  1343. this.wrapperStyle[utils.style.transitionDuration] = utils.isBadAndroid ? '0.001s' : '0ms';
  1344. this.wrapperStyle.opacity = '0';
  1345. }
  1346. }
  1347. Indicator.prototype = {
  1348. handleEvent: function (e) {
  1349. switch ( e.type ) {
  1350. case 'touchstart':
  1351. case 'pointerdown':
  1352. case 'MSPointerDown':
  1353. case 'mousedown':
  1354. this._start(e);
  1355. break;
  1356. case 'touchmove':
  1357. case 'pointermove':
  1358. case 'MSPointerMove':
  1359. case 'mousemove':
  1360. this._move(e);
  1361. break;
  1362. case 'touchend':
  1363. case 'pointerup':
  1364. case 'MSPointerUp':
  1365. case 'mouseup':
  1366. case 'touchcancel':
  1367. case 'pointercancel':
  1368. case 'MSPointerCancel':
  1369. case 'mousecancel':
  1370. this._end(e);
  1371. break;
  1372. }
  1373. },
  1374. destroy: function () {
  1375. if ( this.options.interactive ) {
  1376. utils.removeEvent(this.indicator, 'touchstart', this);
  1377. utils.removeEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1378. utils.removeEvent(this.indicator, 'mousedown', this);
  1379. utils.removeEvent(window, 'touchmove', this);
  1380. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1381. utils.removeEvent(window, 'mousemove', this);
  1382. utils.removeEvent(window, 'touchend', this);
  1383. utils.removeEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1384. utils.removeEvent(window, 'mouseup', this);
  1385. }
  1386. if ( this.options.defaultScrollbars ) {
  1387. this.wrapper.parentNode.removeChild(this.wrapper);
  1388. }
  1389. },
  1390. _start: function (e) {
  1391. var point = e.touches ? e.touches[0] : e;
  1392. e.preventDefault();
  1393. e.stopPropagation();
  1394. this.transitionTime();
  1395. this.initiated = true;
  1396. this.moved = false;
  1397. this.lastPointX = point.pageX;
  1398. this.lastPointY = point.pageY;
  1399. this.startTime = utils.getTime();
  1400. if ( !this.options.disableTouch ) {
  1401. utils.addEvent(window, 'touchmove', this);
  1402. }
  1403. if ( !this.options.disablePointer ) {
  1404. utils.addEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1405. }
  1406. if ( !this.options.disableMouse ) {
  1407. utils.addEvent(window, 'mousemove', this);
  1408. }
  1409. this.scroller._execEvent('beforeScrollStart');
  1410. },
  1411. _move: function (e) {
  1412. var point = e.touches ? e.touches[0] : e,
  1413. deltaX, deltaY,
  1414. newX, newY,
  1415. timestamp = utils.getTime();
  1416. if ( !this.moved ) {
  1417. this.scroller._execEvent('scrollStart');
  1418. }
  1419. this.moved = true;
  1420. deltaX = point.pageX - this.lastPointX;
  1421. this.lastPointX = point.pageX;
  1422. deltaY = point.pageY - this.lastPointY;
  1423. this.lastPointY = point.pageY;
  1424. newX = this.x + deltaX;
  1425. newY = this.y + deltaY;
  1426. this._pos(newX, newY);
  1427. if ( this.scroller.options.probeType == 1 && timestamp - this.startTime > 300 ) {
  1428. this.startTime = timestamp;
  1429. this.scroller._execEvent('scroll');
  1430. } else if ( this.scroller.options.probeType > 1 ) {
  1431. this.scroller._execEvent('scroll');
  1432. }
  1433. // INSERT POINT: indicator._move
  1434. e.preventDefault();
  1435. e.stopPropagation();
  1436. },
  1437. _end: function (e) {
  1438. if ( !this.initiated ) {
  1439. return;
  1440. }
  1441. this.initiated = false;
  1442. e.preventDefault();
  1443. e.stopPropagation();
  1444. utils.removeEvent(window, 'touchmove', this);
  1445. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1446. utils.removeEvent(window, 'mousemove', this);
  1447. if ( this.scroller.options.snap ) {
  1448. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  1449. var time = this.options.snapSpeed || Math.max(
  1450. Math.max(
  1451. Math.min(Math.abs(this.scroller.x - snap.x), 1000),
  1452. Math.min(Math.abs(this.scroller.y - snap.y), 1000)
  1453. ), 300);
  1454. if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
  1455. this.scroller.directionX = 0;
  1456. this.scroller.directionY = 0;
  1457. this.scroller.currentPage = snap;
  1458. this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
  1459. }
  1460. }
  1461. if ( this.moved ) {
  1462. this.scroller._execEvent('scrollEnd');
  1463. }
  1464. },
  1465. transitionTime: function (time) {
  1466. time = time || 0;
  1467. this.indicatorStyle[utils.style.transitionDuration] = time + 'ms';
  1468. if ( !time && utils.isBadAndroid ) {
  1469. this.indicatorStyle[utils.style.transitionDuration] = '0.001s';
  1470. }
  1471. },
  1472. transitionTimingFunction: function (easing) {
  1473. this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
  1474. },
  1475. refresh: function () {
  1476. this.transitionTime();
  1477. if ( this.options.listenX && !this.options.listenY ) {
  1478. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  1479. } else if ( this.options.listenY && !this.options.listenX ) {
  1480. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  1481. } else {
  1482. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  1483. }
  1484. if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
  1485. utils.addClass(this.wrapper, 'iScrollBothScrollbars');
  1486. utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
  1487. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1488. if ( this.options.listenX ) {
  1489. this.wrapper.style.right = '8px';
  1490. } else {
  1491. this.wrapper.style.bottom = '8px';
  1492. }
  1493. }
  1494. } else {
  1495. utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
  1496. utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
  1497. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1498. if ( this.options.listenX ) {
  1499. this.wrapper.style.right = '2px';
  1500. } else {
  1501. this.wrapper.style.bottom = '2px';
  1502. }
  1503. }
  1504. }
  1505. var r = this.wrapper.offsetHeight; // force refresh
  1506. if ( this.options.listenX ) {
  1507. this.wrapperWidth = this.wrapper.clientWidth;
  1508. if ( this.options.resize ) {
  1509. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 35);
  1510. this.indicatorStyle.width = this.indicatorWidth + 'px';
  1511. } else {
  1512. this.indicatorWidth = this.indicator.clientWidth;
  1513. }
  1514. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  1515. if ( this.options.shrink == 'clip' ) {
  1516. this.minBoundaryX = -this.indicatorWidth + 8;
  1517. this.maxBoundaryX = this.wrapperWidth - 8;
  1518. } else {
  1519. this.minBoundaryX = 0;
  1520. this.maxBoundaryX = this.maxPosX;
  1521. }
  1522. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  1523. }
  1524. if ( this.options.listenY ) {
  1525. this.wrapperHeight = this.wrapper.clientHeight;
  1526. if ( this.options.resize ) {
  1527. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 35);
  1528. this.indicatorStyle.height = this.indicatorHeight + 'px';
  1529. } else {
  1530. this.indicatorHeight = this.indicator.clientHeight;
  1531. }
  1532. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1533. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && this.maxPosY / this.scroller.maxScrollY);
  1534. if ( this.options.shrink == 'clip' ) {
  1535. this.minBoundaryY = -this.indicatorHeight + 8;
  1536. this.maxBoundaryY = this.wrapperHeight - 8;
  1537. } else {
  1538. this.minBoundaryY = 0;
  1539. this.maxBoundaryY = this.maxPosY;
  1540. }
  1541. }
  1542. this.updatePosition();
  1543. },
  1544. updatePosition: function () {
  1545. /*var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  1546. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;*/
  1547. var x = this.options.listenX && (this.sizeRatioX * this.scroller.x) || 0,
  1548. y = this.options.listenY && (this.sizeRatioY * this.scroller.y) || 0,
  1549. shrinkScale = 15, minSize = 10;
  1550. if ( !this.options.ignoreBoundaries ) {
  1551. if ( x < this.minBoundaryX ) {
  1552. if ( this.options.shrink == 'scale' ) {
  1553. this.width = Math.max(this.indicatorWidth + x, minSize);
  1554. this.indicatorStyle.width = this.width + 'px';
  1555. }
  1556. x = this.minBoundaryX;
  1557. } else if ( x > this.maxBoundaryX ) {
  1558. if ( this.options.shrink == 'scale' ) {
  1559. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), minSize);
  1560. this.indicatorStyle.width = this.width + 'px';
  1561. x = this.maxPosX + this.indicatorWidth - this.width;
  1562. } else {
  1563. x = this.maxBoundaryX;
  1564. }
  1565. } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
  1566. this.width = this.indicatorWidth;
  1567. this.indicatorStyle.width = this.width + 'px';
  1568. }
  1569. if ( y < this.minBoundaryY ) {
  1570. if ( this.options.shrink == 'scale' ) {
  1571. this.height = Math.max(this.indicatorHeight + y * shrinkScale, minSize);
  1572. this.indicatorStyle.height = this.height + 'px';
  1573. }
  1574. y = this.minBoundaryY;
  1575. } else if ( y > this.maxBoundaryY ) {
  1576. if ( this.options.shrink == 'scale' ) {
  1577. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * shrinkScale, minSize);
  1578. this.indicatorStyle.height = this.height + 'px';
  1579. y = this.maxPosY + this.indicatorHeight - this.height;
  1580. } else {
  1581. y = this.maxBoundaryY;
  1582. }
  1583. } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
  1584. this.height = this.indicatorHeight;
  1585. this.indicatorStyle.height = this.height + 'px';
  1586. }
  1587. }
  1588. this.x = x;
  1589. this.y = y;
  1590. if ( this.scroller.options.useTransform ) {
  1591. this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
  1592. } else {
  1593. this.indicatorStyle.left = x + 'px';
  1594. this.indicatorStyle.top = y + 'px';
  1595. }
  1596. },
  1597. _pos: function (x, y) {
  1598. if ( x < 0 ) {
  1599. x = 0;
  1600. } else if ( x > this.maxPosX ) {
  1601. x = this.maxPosX;
  1602. }
  1603. if ( y < 0 ) {
  1604. y = 0;
  1605. } else if ( y > this.maxPosY ) {
  1606. y = this.maxPosY;
  1607. }
  1608. x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
  1609. y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
  1610. this.scroller.scrollTo(x, y);
  1611. },
  1612. fade: function (val, hold) {
  1613. if ( hold && !this.visible ) {
  1614. return;
  1615. }
  1616. clearTimeout(this.fadeTimeout);
  1617. this.fadeTimeout = null;
  1618. var time = val ? 250 : 500,
  1619. delay = val ? 0 : 300;
  1620. val = val ? '1' : '0';
  1621. this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
  1622. this.fadeTimeout = setTimeout((function (val) {
  1623. this.wrapperStyle.opacity = val;
  1624. this.visible = +val;
  1625. }).bind(this, val), delay);
  1626. }
  1627. };
  1628. IScroll.utils = utils;
  1629. if ( typeof module != 'undefined' && module.exports ) {
  1630. module.exports = IScroll;
  1631. } else {
  1632. window.IScroll = IScroll;
  1633. }
  1634. })(window, document, Math);