iscroll.js 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. /* eslint-disable */
  2. /*! iScroll v5.2.0 ~ (c) 2008-2016 Matteo Spinelli ~ http://cubiq.org/license */
  3. (function (window, document, Math) {
  4. var rAF = window.requestAnimationFrame ||
  5. window.webkitRequestAnimationFrame ||
  6. window.mozRequestAnimationFrame ||
  7. window.oRequestAnimationFrame ||
  8. window.msRequestAnimationFrame ||
  9. function (callback) { window.setTimeout(callback, 1000 / 60); };
  10. var utils = (function () {
  11. var me = {};
  12. var _elementStyle = document.createElement('div').style;
  13. var _vendor = (function () {
  14. var vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT'],
  15. transform,
  16. i = 0,
  17. l = vendors.length;
  18. for ( ; i < l; i++ ) {
  19. transform = vendors[i] + 'ransform';
  20. if ( transform in _elementStyle ) return vendors[i].substr(0, vendors[i].length-1);
  21. }
  22. return false;
  23. })();
  24. function _prefixStyle (style) {
  25. if ( _vendor === false ) return false;
  26. if ( _vendor === '' ) return style;
  27. return _vendor + style.charAt(0).toUpperCase() + style.substr(1);
  28. }
  29. me.getTime = Date.now || function getTime () { return new Date().getTime(); };
  30. me.extend = function (target, obj) {
  31. for ( var i in obj ) {
  32. target[i] = obj[i];
  33. }
  34. };
  35. me.addEvent = function (el, type, fn, capture) {
  36. el.addEventListener(type, fn, !!capture);
  37. };
  38. me.removeEvent = function (el, type, fn, capture) {
  39. el.removeEventListener(type, fn, !!capture);
  40. };
  41. me.prefixPointerEvent = function (pointerEvent) {
  42. return window.MSPointerEvent ?
  43. 'MSPointer' + pointerEvent.charAt(7).toUpperCase() + pointerEvent.substr(8):
  44. pointerEvent;
  45. };
  46. me.momentum = function (current, start, time, lowerMargin, wrapperSize, deceleration) {
  47. var distance = current - start,
  48. speed = Math.abs(distance) / time,
  49. destination,
  50. duration;
  51. deceleration = deceleration === undefined ? 0.0006 : deceleration;
  52. destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
  53. duration = speed / deceleration;
  54. if ( destination < lowerMargin ) {
  55. destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
  56. distance = Math.abs(destination - current);
  57. duration = distance / speed;
  58. } else if ( destination > 0 ) {
  59. destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
  60. distance = Math.abs(current) + destination;
  61. duration = distance / speed;
  62. }
  63. return {
  64. destination: Math.round(destination),
  65. duration: duration
  66. };
  67. };
  68. var _transform = _prefixStyle('transform');
  69. me.extend(me, {
  70. hasTransform: _transform !== false,
  71. hasPerspective: _prefixStyle('perspective') in _elementStyle,
  72. hasTouch: 'ontouchstart' in window,
  73. hasPointer: !!(window.PointerEvent || window.MSPointerEvent), // IE10 is prefixed
  74. hasTransition: _prefixStyle('transition') in _elementStyle
  75. });
  76. /*
  77. This should find all Android browsers lower than build 535.19 (both stock browser and webview)
  78. - galaxy S2 is ok
  79. - 2.3.6 : `AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1`
  80. - 4.0.4 : `AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30`
  81. - galaxy S3 is badAndroid (stock brower, webview)
  82. `AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30`
  83. - galaxy S4 is badAndroid (stock brower, webview)
  84. `AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30`
  85. - galaxy S5 is OK
  86. `AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36 (Chrome/)`
  87. - galaxy S6 is OK
  88. `AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36 (Chrome/)`
  89. */
  90. me.isBadAndroid = (function() {
  91. var appVersion = window.navigator.appVersion;
  92. // Android browser is not a chrome browser.
  93. if (/Android/.test(appVersion) && !(/Chrome\/\d/.test(appVersion))) {
  94. var safariVersion = appVersion.match(/Safari\/(\d+.\d)/);
  95. if(safariVersion && typeof safariVersion === "object" && safariVersion.length >= 2) {
  96. return parseFloat(safariVersion[1]) < 535.19;
  97. } else {
  98. return true;
  99. }
  100. } else {
  101. return false;
  102. }
  103. })();
  104. me.extend(me.style = {}, {
  105. transform: _transform,
  106. transitionTimingFunction: _prefixStyle('transitionTimingFunction'),
  107. transitionDuration: _prefixStyle('transitionDuration'),
  108. transitionDelay: _prefixStyle('transitionDelay'),
  109. transformOrigin: _prefixStyle('transformOrigin')
  110. });
  111. me.hasClass = function (e, c) {
  112. var re = new RegExp("(^|\\s)" + c + "(\\s|$)");
  113. return re.test(e.className);
  114. };
  115. me.addClass = function (e, c) {
  116. if ( me.hasClass(e, c) ) {
  117. return;
  118. }
  119. var newclass = e.className.split(' ');
  120. newclass.push(c);
  121. e.className = newclass.join(' ');
  122. };
  123. me.removeClass = function (e, c) {
  124. if ( !me.hasClass(e, c) ) {
  125. return;
  126. }
  127. var re = new RegExp("(^|\\s)" + c + "(\\s|$)", 'g');
  128. e.className = e.className.replace(re, ' ');
  129. };
  130. me.offset = function (el) {
  131. var left = -el.offsetLeft,
  132. top = -el.offsetTop;
  133. // jshint -W084
  134. while (el = el.offsetParent) {
  135. left -= el.offsetLeft;
  136. top -= el.offsetTop;
  137. }
  138. // jshint +W084
  139. return {
  140. left: left,
  141. top: top
  142. };
  143. };
  144. me.preventDefaultException = function (el, exceptions) {
  145. for ( var i in exceptions ) {
  146. if ( exceptions[i].test(el[i]) ) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. };
  152. me.extend(me.eventType = {}, {
  153. touchstart: 1,
  154. touchmove: 1,
  155. touchend: 1,
  156. mousedown: 2,
  157. mousemove: 2,
  158. mouseup: 2,
  159. pointerdown: 3,
  160. pointermove: 3,
  161. pointerup: 3,
  162. MSPointerDown: 3,
  163. MSPointerMove: 3,
  164. MSPointerUp: 3
  165. });
  166. me.extend(me.ease = {}, {
  167. quadratic: {
  168. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  169. fn: function (k) {
  170. return k * ( 2 - k );
  171. }
  172. },
  173. circular: {
  174. 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)
  175. fn: function (k) {
  176. return Math.sqrt( 1 - ( --k * k ) );
  177. }
  178. },
  179. back: {
  180. style: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)',
  181. fn: function (k) {
  182. var b = 4;
  183. return ( k = k - 1 ) * k * ( ( b + 1 ) * k + b ) + 1;
  184. }
  185. },
  186. bounce: {
  187. style: '',
  188. fn: function (k) {
  189. if ( ( k /= 1 ) < ( 1 / 2.75 ) ) {
  190. return 7.5625 * k * k;
  191. } else if ( k < ( 2 / 2.75 ) ) {
  192. return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
  193. } else if ( k < ( 2.5 / 2.75 ) ) {
  194. return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
  195. } else {
  196. return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
  197. }
  198. }
  199. },
  200. elastic: {
  201. style: '',
  202. fn: function (k) {
  203. var f = 0.22,
  204. e = 0.4;
  205. if ( k === 0 ) { return 0; }
  206. if ( k == 1 ) { return 1; }
  207. return ( e * Math.pow( 2, - 10 * k ) * Math.sin( ( k - f / 4 ) * ( 2 * Math.PI ) / f ) + 1 );
  208. }
  209. }
  210. });
  211. me.tap = function (e, eventName) {
  212. var ev = document.createEvent('Event');
  213. ev.initEvent(eventName, true, true);
  214. ev.pageX = e.pageX;
  215. ev.pageY = e.pageY;
  216. e.target.dispatchEvent(ev);
  217. };
  218. me.click = function (e) {
  219. var target = e.target,
  220. ev;
  221. if ( !(/(SELECT|INPUT|TEXTAREA)/i).test(target.tagName) ) {
  222. // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/initMouseEvent
  223. // initMouseEvent is deprecated.
  224. ev = document.createEvent(window.MouseEvent ? 'MouseEvents' : 'Event');
  225. ev.initEvent('click', true, true);
  226. ev.view = e.view || window;
  227. ev.detail = 1;
  228. ev.screenX = target.screenX || 0;
  229. ev.screenY = target.screenY || 0;
  230. ev.clientX = target.clientX || 0;
  231. ev.clientY = target.clientY || 0;
  232. ev.ctrlKey = !!e.ctrlKey;
  233. ev.altKey = !!e.altKey;
  234. ev.shiftKey = !!e.shiftKey;
  235. ev.metaKey = !!e.metaKey;
  236. ev.button = 0;
  237. ev.relatedTarget = null;
  238. ev._constructed = true;
  239. target.dispatchEvent(ev);
  240. }
  241. };
  242. return me;
  243. })();
  244. function IScroll (el, options) {
  245. this.wrapper = typeof el == 'string' ? document.querySelector(el) : el;
  246. this.scroller = this.wrapper.children[0];
  247. this.scrollerStyle = this.scroller.style; // cache style for better performance
  248. this.options = {
  249. resizeScrollbars: true,
  250. mouseWheelSpeed: 20,
  251. snapThreshold: 0.334,
  252. // INSERT POINT: OPTIONS
  253. /*disablePointer : !utils.hasPointer,
  254. disableTouch : utils.hasPointer || !utils.hasTouch,
  255. disableMouse : utils.hasPointer || utils.hasTouch,*/
  256. disablePointer: true,
  257. disableTouch: !utils.hasTouch,
  258. disableMouse: utils.hasTouch,
  259. startX: 0,
  260. startY: 0,
  261. scrollY: true,
  262. directionLockThreshold: 5,
  263. momentum: true,
  264. bounce: true,
  265. bounceTime: 600,
  266. bounceEasing: '',
  267. preventDefault: true,
  268. preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ },
  269. HWCompositing: true,
  270. useTransition: true,
  271. useTransform: true,
  272. bindToWrapper: typeof window.onmousedown === "undefined",
  273. // TODO 尝试解决 华为P10等多款手机型号上下滑动的页面都会卡顿现象,比如管理员端的所有界面,以及代预约的页面问题
  274. disablePointer : true,
  275. disableTouch : false,
  276. disableMouse : false
  277. };
  278. for ( var i in options ) {
  279. this.options[i] = options[i];
  280. }
  281. // Normalize options
  282. this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
  283. this.options.useTransition = utils.hasTransition && this.options.useTransition;
  284. this.options.useTransform = utils.hasTransform && this.options.useTransform;
  285. this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
  286. this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
  287. // If you want eventPassthrough I have to lock one of the axes
  288. this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
  289. this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
  290. // With eventPassthrough we also need lockDirection mechanism
  291. this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
  292. this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
  293. this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
  294. this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling;
  295. if ( this.options.tap === true ) {
  296. this.options.tap = 'tap';
  297. }
  298. // https://github.com/cubiq/iscroll/issues/1029
  299. if (!this.options.useTransition && !this.options.useTransform) {
  300. if(!(/relative|absolute/i).test(this.scrollerStyle.position)) {
  301. this.scrollerStyle.position = "relative";
  302. }
  303. }
  304. if ( this.options.shrinkScrollbars == 'scale' ) {
  305. this.options.useTransition = false;
  306. }
  307. this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
  308. if ( this.options.probeType == 3 ) {
  309. this.options.useTransition = false; }
  310. // INSERT POINT: NORMALIZATION
  311. // Some defaults
  312. this.x = 0;
  313. this.y = 0;
  314. this.directionX = 0;
  315. this.directionY = 0;
  316. this._events = {};
  317. // INSERT POINT: DEFAULTS
  318. this._init();
  319. this.refresh();
  320. this.scrollTo(this.options.startX, this.options.startY);
  321. this.enable();
  322. }
  323. IScroll.prototype = {
  324. version: '5.2.0',
  325. _init: function () {
  326. this._initEvents();
  327. if ( this.options.scrollbars || this.options.indicators ) {
  328. this._initIndicators();
  329. }
  330. if ( this.options.mouseWheel ) {
  331. this._initWheel();
  332. }
  333. if ( this.options.snap ) {
  334. this._initSnap();
  335. }
  336. if ( this.options.keyBindings ) {
  337. this._initKeys();
  338. }
  339. // INSERT POINT: _init
  340. },
  341. destroy: function () {
  342. this._initEvents(true);
  343. clearTimeout(this.resizeTimeout);
  344. this.resizeTimeout = null;
  345. this._execEvent('destroy');
  346. },
  347. _transitionEnd: function (e) {
  348. if ( e.target != this.scroller || !this.isInTransition ) {
  349. return;
  350. }
  351. this._transitionTime();
  352. if ( !this.resetPosition(this.options.bounceTime) ) {
  353. this.isInTransition = false;
  354. this._execEvent('scrollEnd');
  355. }
  356. },
  357. _start: function (e) {
  358. // React to left mouse button only
  359. if ( utils.eventType[e.type] != 1 ) {
  360. // for button property
  361. // http://unixpapa.com/js/mouse.html
  362. var button;
  363. if (!e.which) {
  364. /* IE case */
  365. button = (e.button < 2) ? 0 :
  366. ((e.button == 4) ? 1 : 2);
  367. } else {
  368. /* All others */
  369. button = e.button;
  370. }
  371. if ( button !== 0 ) {
  372. return;
  373. }
  374. }
  375. if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
  376. return;
  377. }
  378. if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  379. e.preventDefault();
  380. }
  381. var point = e.touches ? e.touches[0] : e,
  382. pos;
  383. this.initiated = utils.eventType[e.type];
  384. this.moved = false;
  385. this.distX = 0;
  386. this.distY = 0;
  387. this.directionX = 0;
  388. this.directionY = 0;
  389. this.directionLocked = 0;
  390. this.startTime = utils.getTime();
  391. if ( this.options.useTransition && this.isInTransition ) {
  392. this._transitionTime();
  393. this.isInTransition = false;
  394. pos = this.getComputedPosition();
  395. this._translate(Math.round(pos.x), Math.round(pos.y));
  396. this._execEvent('scrollEnd');
  397. } else if ( !this.options.useTransition && this.isAnimating ) {
  398. this.isAnimating = false;
  399. this._execEvent('scrollEnd');
  400. }
  401. this.startX = this.x;
  402. this.startY = this.y;
  403. this.absStartX = this.x;
  404. this.absStartY = this.y;
  405. this.pointX = point.pageX;
  406. this.pointY = point.pageY;
  407. this._execEvent('beforeScrollStart');
  408. },
  409. _move: function (e) {
  410. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  411. return;
  412. }
  413. if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
  414. e.preventDefault();
  415. }
  416. var point = e.touches ? e.touches[0] : e,
  417. deltaX = point.pageX - this.pointX,
  418. deltaY = point.pageY - this.pointY,
  419. timestamp = utils.getTime(),
  420. newX, newY,
  421. absDistX, absDistY;
  422. this.pointX = point.pageX;
  423. this.pointY = point.pageY;
  424. this.distX += deltaX;
  425. this.distY += deltaY;
  426. absDistX = Math.abs(this.distX);
  427. absDistY = Math.abs(this.distY);
  428. // We need to move at least 10 pixels for the scrolling to initiate
  429. if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
  430. return;
  431. }
  432. // If you are scrolling in one direction lock the other
  433. if ( !this.directionLocked && !this.options.freeScroll ) {
  434. if ( absDistX > absDistY + this.options.directionLockThreshold ) {
  435. this.directionLocked = 'h'; // lock horizontally
  436. } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
  437. this.directionLocked = 'v'; // lock vertically
  438. } else {
  439. this.directionLocked = 'n'; // no lock
  440. }
  441. }
  442. if ( this.directionLocked == 'h' ) {
  443. if ( this.options.eventPassthrough == 'vertical' ) {
  444. e.preventDefault();
  445. } else if ( this.options.eventPassthrough == 'horizontal' ) {
  446. this.initiated = false;
  447. return;
  448. }
  449. deltaY = 0;
  450. } else if ( this.directionLocked == 'v' ) {
  451. if ( this.options.eventPassthrough == 'horizontal' ) {
  452. e.preventDefault();
  453. } else if ( this.options.eventPassthrough == 'vertical' ) {
  454. this.initiated = false;
  455. return;
  456. }
  457. deltaX = 0;
  458. }
  459. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  460. deltaY = this.hasVerticalScroll ? deltaY : 0;
  461. newX = this.x + deltaX;
  462. newY = this.y + deltaY;
  463. // Slow down if outside of the boundaries
  464. if ( newX > 0 || newX < this.maxScrollX ) {
  465. newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
  466. }
  467. if ( newY > 0 || newY < this.maxScrollY ) {
  468. newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
  469. }
  470. this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
  471. this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
  472. if ( !this.moved ) {
  473. this._execEvent('scrollStart');
  474. }
  475. this.moved = true;
  476. this._translate(newX, newY);
  477. /* REPLACE START: _move */
  478. if ( timestamp - this.startTime > 300 ) {
  479. this.startTime = timestamp;
  480. this.startX = this.x;
  481. this.startY = this.y;
  482. if ( this.options.probeType == 1 ) {
  483. this._execEvent('scroll');
  484. }
  485. }
  486. if ( this.options.probeType > 1 ) {
  487. this._execEvent('scroll');
  488. }
  489. /* REPLACE END: _move */
  490. },
  491. _end: function (e) {
  492. if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
  493. return;
  494. }
  495. if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
  496. e.preventDefault();
  497. }
  498. var point = e.changedTouches ? e.changedTouches[0] : e,
  499. momentumX,
  500. momentumY,
  501. duration = utils.getTime() - this.startTime,
  502. newX = Math.round(this.x),
  503. newY = Math.round(this.y),
  504. distanceX = Math.abs(newX - this.startX),
  505. distanceY = Math.abs(newY - this.startY),
  506. time = 0,
  507. easing = '';
  508. this.isInTransition = 0;
  509. this.initiated = 0;
  510. this.endTime = utils.getTime();
  511. // reset if we are outside of the boundaries
  512. if ( this.resetPosition(this.options.bounceTime) ) {
  513. return;
  514. }
  515. this.scrollTo(newX, newY); // ensures that the last position is rounded
  516. // we scrolled less than 10 pixels
  517. if ( !this.moved ) {
  518. if ( this.options.tap ) {
  519. utils.tap(e, this.options.tap);
  520. }
  521. if ( this.options.click ) {
  522. utils.click(e);
  523. }
  524. this._execEvent('scrollCancel');
  525. return;
  526. }
  527. if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) {
  528. this._execEvent('flick');
  529. return;
  530. }
  531. // start momentum animation if needed
  532. if ( this.options.momentum && duration < 300 ) {
  533. 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 };
  534. 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 };
  535. newX = momentumX.destination;
  536. newY = momentumY.destination;
  537. time = Math.max(momentumX.duration, momentumY.duration);
  538. this.isInTransition = 1;
  539. }
  540. if ( this.options.snap ) {
  541. var snap = this._nearestSnap(newX, newY);
  542. this.currentPage = snap;
  543. time = this.options.snapSpeed || Math.max(
  544. Math.max(
  545. Math.min(Math.abs(newX - snap.x), 1000),
  546. Math.min(Math.abs(newY - snap.y), 1000)
  547. ), 300);
  548. newX = snap.x;
  549. newY = snap.y;
  550. this.directionX = 0;
  551. this.directionY = 0;
  552. easing = this.options.bounceEasing;
  553. }
  554. // INSERT POINT: _end
  555. if ( newX != this.x || newY != this.y ) {
  556. // change easing function when scroller goes out of the boundaries
  557. if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
  558. easing = utils.ease.quadratic;
  559. }
  560. this.scrollTo(newX, newY, time, easing);
  561. return;
  562. }
  563. this._execEvent('scrollEnd');
  564. },
  565. _resize: function () {
  566. var that = this;
  567. clearTimeout(this.resizeTimeout);
  568. this.resizeTimeout = setTimeout(function () {
  569. that.refresh();
  570. }, this.options.resizePolling);
  571. },
  572. resetPosition: function (time) {
  573. var x = this.x,
  574. y = this.y;
  575. time = time || 0;
  576. if ( !this.hasHorizontalScroll || this.x > 0 ) {
  577. x = 0;
  578. } else if ( this.x < this.maxScrollX ) {
  579. x = this.maxScrollX;
  580. }
  581. if ( !this.hasVerticalScroll || this.y > 0 ) {
  582. y = 0;
  583. } else if ( this.y < this.maxScrollY ) {
  584. y = this.maxScrollY;
  585. }
  586. if ( x == this.x && y == this.y ) {
  587. return false;
  588. }
  589. this.scrollTo(x, y, time, this.options.bounceEasing);
  590. return true;
  591. },
  592. disable: function () {
  593. this.enabled = false;
  594. },
  595. enable: function () {
  596. this.enabled = true;
  597. },
  598. refresh: function () {
  599. var rf = this.wrapper.offsetHeight; // Force reflow
  600. this.wrapperWidth = this.wrapper.clientWidth;
  601. this.wrapperHeight = this.wrapper.clientHeight;
  602. /* REPLACE START: refresh */
  603. this.scrollerWidth = this.scroller.offsetWidth;
  604. this.scrollerHeight = this.scroller.offsetHeight;
  605. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  606. this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
  607. /* REPLACE END: refresh */
  608. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
  609. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
  610. if ( !this.hasHorizontalScroll ) {
  611. this.maxScrollX = 0;
  612. this.scrollerWidth = this.wrapperWidth;
  613. }
  614. if ( !this.hasVerticalScroll ) {
  615. this.maxScrollY = 0;
  616. this.scrollerHeight = this.wrapperHeight;
  617. }
  618. this.endTime = 0;
  619. this.directionX = 0;
  620. this.directionY = 0;
  621. this.wrapperOffset = utils.offset(this.wrapper);
  622. this._execEvent('refresh');
  623. this.resetPosition();
  624. // INSERT POINT: _refresh
  625. },
  626. on: function (type, fn) {
  627. if ( !this._events[type] ) {
  628. this._events[type] = [];
  629. }
  630. this._events[type].push(fn);
  631. },
  632. off: function (type, fn) {
  633. if ( !this._events[type] ) {
  634. return;
  635. }
  636. var index = this._events[type].indexOf(fn);
  637. if ( index > -1 ) {
  638. this._events[type].splice(index, 1);
  639. }
  640. },
  641. _execEvent: function (type) {
  642. if ( !this._events[type] ) {
  643. return;
  644. }
  645. var i = 0,
  646. l = this._events[type].length;
  647. if ( !l ) {
  648. return;
  649. }
  650. for ( ; i < l; i++ ) {
  651. this._events[type][i].apply(this, [].slice.call(arguments, 1));
  652. }
  653. },
  654. scrollBy: function (x, y, time, easing) {
  655. x = this.x + x;
  656. y = this.y + y;
  657. time = time || 0;
  658. this.scrollTo(x, y, time, easing);
  659. },
  660. scrollTo: function (x, y, time, easing) {
  661. easing = easing || utils.ease.circular;
  662. this.isInTransition = this.options.useTransition && time > 0;
  663. var transitionType = this.options.useTransition && easing.style;
  664. if ( !time || transitionType ) {
  665. if(transitionType) {
  666. this._transitionTimingFunction(easing.style);
  667. this._transitionTime(time);
  668. }
  669. this._translate(x, y);
  670. } else {
  671. this._animate(x, y, time, easing.fn);
  672. }
  673. },
  674. scrollToElement: function (el, time, offsetX, offsetY, easing) {
  675. el = el.nodeType ? el : this.scroller.querySelector(el);
  676. if ( !el ) {
  677. return;
  678. }
  679. var pos = utils.offset(el);
  680. pos.left -= this.wrapperOffset.left;
  681. pos.top -= this.wrapperOffset.top;
  682. // if offsetX/Y are true we center the element to the screen
  683. if ( offsetX === true ) {
  684. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  685. }
  686. if ( offsetY === true ) {
  687. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  688. }
  689. pos.left -= offsetX || 0;
  690. pos.top -= offsetY || 0;
  691. pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  692. pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  693. time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
  694. this.scrollTo(pos.left, pos.top, time, easing);
  695. },
  696. _transitionTime: function (time) {
  697. if (!this.options.useTransition) {
  698. return;
  699. }
  700. time = time || 0;
  701. var durationProp = utils.style.transitionDuration;
  702. if(!durationProp) {
  703. return;
  704. }
  705. this.scrollerStyle[durationProp] = time + 'ms';
  706. if ( !time && utils.isBadAndroid ) {
  707. this.scrollerStyle[durationProp] = '0.0001ms';
  708. // remove 0.0001ms
  709. var self = this;
  710. rAF(function() {
  711. if(self.scrollerStyle[durationProp] === '0.0001ms') {
  712. self.scrollerStyle[durationProp] = '0s';
  713. }
  714. });
  715. }
  716. if ( this.indicators ) {
  717. for ( var i = this.indicators.length; i--; ) {
  718. this.indicators[i].transitionTime(time);
  719. }
  720. }
  721. // INSERT POINT: _transitionTime
  722. },
  723. _transitionTimingFunction: function (easing) {
  724. this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
  725. if ( this.indicators ) {
  726. for ( var i = this.indicators.length; i--; ) {
  727. this.indicators[i].transitionTimingFunction(easing);
  728. }
  729. }
  730. // INSERT POINT: _transitionTimingFunction
  731. },
  732. _translate: function (x, y) {
  733. if ( this.options.useTransform ) {
  734. /* REPLACE START: _translate */
  735. this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
  736. /* REPLACE END: _translate */
  737. } else {
  738. x = Math.round(x);
  739. y = Math.round(y);
  740. this.scrollerStyle.left = x + 'px';
  741. this.scrollerStyle.top = y + 'px';
  742. }
  743. this.x = x;
  744. this.y = y;
  745. if ( this.indicators ) {
  746. for ( var i = this.indicators.length; i--; ) {
  747. this.indicators[i].updatePosition();
  748. }
  749. }
  750. // INSERT POINT: _translate
  751. },
  752. _initEvents: function (remove) {
  753. var eventType = remove ? utils.removeEvent : utils.addEvent,
  754. target = this.options.bindToWrapper ? this.wrapper : window;
  755. eventType(window, 'orientationchange', this);
  756. eventType(window, 'resize', this);
  757. if ( this.options.click ) {
  758. eventType(this.wrapper, 'click', this, true);
  759. }
  760. if ( !this.options.disableMouse ) {
  761. eventType(this.wrapper, 'mousedown', this);
  762. eventType(target, 'mousemove', this);
  763. eventType(target, 'mousecancel', this);
  764. eventType(target, 'mouseup', this);
  765. }
  766. if ( utils.hasPointer && !this.options.disablePointer ) {
  767. eventType(this.wrapper, utils.prefixPointerEvent('pointerdown'), this);
  768. eventType(target, utils.prefixPointerEvent('pointermove'), this);
  769. eventType(target, utils.prefixPointerEvent('pointercancel'), this);
  770. eventType(target, utils.prefixPointerEvent('pointerup'), this);
  771. }
  772. if ( utils.hasTouch && !this.options.disableTouch ) {
  773. eventType(this.wrapper, 'touchstart', this);
  774. eventType(target, 'touchmove', this);
  775. eventType(target, 'touchcancel', this);
  776. eventType(target, 'touchend', this);
  777. }
  778. eventType(this.scroller, 'transitionend', this);
  779. eventType(this.scroller, 'webkitTransitionEnd', this);
  780. eventType(this.scroller, 'oTransitionEnd', this);
  781. eventType(this.scroller, 'MSTransitionEnd', this);
  782. },
  783. getComputedPosition: function () {
  784. var matrix = window.getComputedStyle(this.scroller, null),
  785. x, y;
  786. if ( this.options.useTransform ) {
  787. matrix = matrix[utils.style.transform].split(')')[0].split(', ');
  788. x = +(matrix[12] || matrix[4]);
  789. y = +(matrix[13] || matrix[5]);
  790. } else {
  791. x = +matrix.left.replace(/[^-\d.]/g, '');
  792. y = +matrix.top.replace(/[^-\d.]/g, '');
  793. }
  794. return { x: x, y: y };
  795. },
  796. _initIndicators: function () {
  797. var interactive = this.options.interactiveScrollbars,
  798. customStyle = typeof this.options.scrollbars != 'string',
  799. indicators = [],
  800. indicator;
  801. var that = this;
  802. this.indicators = [];
  803. if ( this.options.scrollbars ) {
  804. // Vertical scrollbar
  805. if ( this.options.scrollY ) {
  806. indicator = {
  807. el: createDefaultScrollbar('v', interactive, this.options.scrollbars),
  808. interactive: interactive,
  809. defaultScrollbars: true,
  810. customStyle: customStyle,
  811. resize: this.options.resizeScrollbars,
  812. shrink: this.options.shrinkScrollbars,
  813. fade: this.options.fadeScrollbars,
  814. listenX: false
  815. };
  816. this.wrapper.appendChild(indicator.el);
  817. indicators.push(indicator);
  818. }
  819. // Horizontal scrollbar
  820. if ( this.options.scrollX ) {
  821. indicator = {
  822. el: createDefaultScrollbar('h', interactive, this.options.scrollbars),
  823. interactive: interactive,
  824. defaultScrollbars: true,
  825. customStyle: customStyle,
  826. resize: this.options.resizeScrollbars,
  827. shrink: this.options.shrinkScrollbars,
  828. fade: this.options.fadeScrollbars,
  829. listenY: false
  830. };
  831. this.wrapper.appendChild(indicator.el);
  832. indicators.push(indicator);
  833. }
  834. }
  835. if ( this.options.indicators ) {
  836. // TODO: check concat compatibility
  837. indicators = indicators.concat(this.options.indicators);
  838. }
  839. for ( var i = indicators.length; i--; ) {
  840. this.indicators.push( new Indicator(this, indicators[i]) );
  841. }
  842. // TODO: check if we can use array.map (wide compatibility and performance issues)
  843. function _indicatorsMap (fn) {
  844. if (that.indicators) {
  845. for ( var i = that.indicators.length; i--; ) {
  846. fn.call(that.indicators[i]);
  847. }
  848. }
  849. }
  850. if ( this.options.fadeScrollbars ) {
  851. this.on('scrollEnd', function () {
  852. _indicatorsMap(function () {
  853. this.fade();
  854. });
  855. });
  856. this.on('scrollCancel', function () {
  857. _indicatorsMap(function () {
  858. this.fade();
  859. });
  860. });
  861. this.on('scrollStart', function () {
  862. _indicatorsMap(function () {
  863. this.fade(1);
  864. });
  865. });
  866. this.on('beforeScrollStart', function () {
  867. _indicatorsMap(function () {
  868. this.fade(1, true);
  869. });
  870. });
  871. }
  872. this.on('refresh', function () {
  873. _indicatorsMap(function () {
  874. this.refresh();
  875. });
  876. });
  877. this.on('destroy', function () {
  878. _indicatorsMap(function () {
  879. this.destroy();
  880. });
  881. delete this.indicators;
  882. });
  883. },
  884. _initWheel: function () {
  885. utils.addEvent(this.wrapper, 'wheel', this);
  886. utils.addEvent(this.wrapper, 'mousewheel', this);
  887. utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
  888. this.on('destroy', function () {
  889. clearTimeout(this.wheelTimeout);
  890. this.wheelTimeout = null;
  891. utils.removeEvent(this.wrapper, 'wheel', this);
  892. utils.removeEvent(this.wrapper, 'mousewheel', this);
  893. utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
  894. });
  895. },
  896. _wheel: function (e) {
  897. if ( !this.enabled ) {
  898. return;
  899. }
  900. e.preventDefault();
  901. var wheelDeltaX, wheelDeltaY,
  902. newX, newY,
  903. that = this;
  904. if ( this.wheelTimeout === undefined ) {
  905. that._execEvent('scrollStart');
  906. }
  907. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  908. clearTimeout(this.wheelTimeout);
  909. this.wheelTimeout = setTimeout(function () {
  910. if(!that.options.snap) {
  911. that._execEvent('scrollEnd');
  912. }
  913. that.wheelTimeout = undefined;
  914. }, 400);
  915. if ( 'deltaX' in e ) {
  916. if (e.deltaMode === 1) {
  917. wheelDeltaX = -e.deltaX * this.options.mouseWheelSpeed;
  918. wheelDeltaY = -e.deltaY * this.options.mouseWheelSpeed;
  919. } else {
  920. wheelDeltaX = -e.deltaX;
  921. wheelDeltaY = -e.deltaY;
  922. }
  923. } else if ( 'wheelDeltaX' in e ) {
  924. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  925. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  926. } else if ( 'wheelDelta' in e ) {
  927. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  928. } else if ( 'detail' in e ) {
  929. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  930. } else {
  931. return;
  932. }
  933. wheelDeltaX *= this.options.invertWheelDirection;
  934. wheelDeltaY *= this.options.invertWheelDirection;
  935. if ( !this.hasVerticalScroll ) {
  936. wheelDeltaX = wheelDeltaY;
  937. wheelDeltaY = 0;
  938. }
  939. if ( this.options.snap ) {
  940. newX = this.currentPage.pageX;
  941. newY = this.currentPage.pageY;
  942. if ( wheelDeltaX > 0 ) {
  943. newX--;
  944. } else if ( wheelDeltaX < 0 ) {
  945. newX++;
  946. }
  947. if ( wheelDeltaY > 0 ) {
  948. newY--;
  949. } else if ( wheelDeltaY < 0 ) {
  950. newY++;
  951. }
  952. this.goToPage(newX, newY);
  953. return;
  954. }
  955. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  956. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  957. this.directionX = wheelDeltaX > 0 ? -1 : wheelDeltaX < 0 ? 1 : 0;
  958. this.directionY = wheelDeltaY > 0 ? -1 : wheelDeltaY < 0 ? 1 : 0;
  959. if ( newX > 0 ) {
  960. newX = 0;
  961. } else if ( newX < this.maxScrollX ) {
  962. newX = this.maxScrollX;
  963. }
  964. if ( newY > 0 ) {
  965. newY = 0;
  966. } else if ( newY < this.maxScrollY ) {
  967. newY = this.maxScrollY;
  968. }
  969. this.scrollTo(newX, newY, 0);
  970. if ( this.options.probeType > 1 ) {
  971. this._execEvent('scroll');
  972. }
  973. // INSERT POINT: _wheel
  974. },
  975. _initSnap: function () {
  976. this.currentPage = {};
  977. if ( typeof this.options.snap == 'string' ) {
  978. this.options.snap = this.scroller.querySelectorAll(this.options.snap);
  979. }
  980. this.on('refresh', function () {
  981. var i = 0, l,
  982. m = 0, n,
  983. cx, cy,
  984. x = 0, y,
  985. stepX = this.options.snapStepX || this.wrapperWidth,
  986. stepY = this.options.snapStepY || this.wrapperHeight,
  987. el;
  988. this.pages = [];
  989. if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
  990. return;
  991. }
  992. if ( this.options.snap === true ) {
  993. cx = Math.round( stepX / 2 );
  994. cy = Math.round( stepY / 2 );
  995. while ( x > -this.scrollerWidth ) {
  996. this.pages[i] = [];
  997. l = 0;
  998. y = 0;
  999. while ( y > -this.scrollerHeight ) {
  1000. this.pages[i][l] = {
  1001. x: Math.max(x, this.maxScrollX),
  1002. y: Math.max(y, this.maxScrollY),
  1003. width: stepX,
  1004. height: stepY,
  1005. cx: x - cx,
  1006. cy: y - cy
  1007. };
  1008. y -= stepY;
  1009. l++;
  1010. }
  1011. x -= stepX;
  1012. i++;
  1013. }
  1014. } else {
  1015. el = this.options.snap;
  1016. l = el.length;
  1017. n = -1;
  1018. for ( ; i < l; i++ ) {
  1019. if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
  1020. m = 0;
  1021. n++;
  1022. }
  1023. if ( !this.pages[m] ) {
  1024. this.pages[m] = [];
  1025. }
  1026. x = Math.max(-el[i].offsetLeft, this.maxScrollX);
  1027. y = Math.max(-el[i].offsetTop, this.maxScrollY);
  1028. cx = x - Math.round(el[i].offsetWidth / 2);
  1029. cy = y - Math.round(el[i].offsetHeight / 2);
  1030. this.pages[m][n] = {
  1031. x: x,
  1032. y: y,
  1033. width: el[i].offsetWidth,
  1034. height: el[i].offsetHeight,
  1035. cx: cx,
  1036. cy: cy
  1037. };
  1038. if ( x > this.maxScrollX ) {
  1039. m++;
  1040. }
  1041. }
  1042. }
  1043. this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
  1044. // Update snap threshold if needed
  1045. if ( this.options.snapThreshold % 1 === 0 ) {
  1046. this.snapThresholdX = this.options.snapThreshold;
  1047. this.snapThresholdY = this.options.snapThreshold;
  1048. } else {
  1049. this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
  1050. this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
  1051. }
  1052. });
  1053. this.on('flick', function () {
  1054. var time = this.options.snapSpeed || Math.max(
  1055. Math.max(
  1056. Math.min(Math.abs(this.x - this.startX), 1000),
  1057. Math.min(Math.abs(this.y - this.startY), 1000)
  1058. ), 300);
  1059. this.goToPage(
  1060. this.currentPage.pageX + this.directionX,
  1061. this.currentPage.pageY + this.directionY,
  1062. time
  1063. );
  1064. });
  1065. },
  1066. _nearestSnap: function (x, y) {
  1067. if ( !this.pages.length ) {
  1068. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  1069. }
  1070. var i = 0,
  1071. l = this.pages.length,
  1072. m = 0;
  1073. // Check if we exceeded the snap threshold
  1074. if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
  1075. Math.abs(y - this.absStartY) < this.snapThresholdY ) {
  1076. return this.currentPage;
  1077. }
  1078. if ( x > 0 ) {
  1079. x = 0;
  1080. } else if ( x < this.maxScrollX ) {
  1081. x = this.maxScrollX;
  1082. }
  1083. if ( y > 0 ) {
  1084. y = 0;
  1085. } else if ( y < this.maxScrollY ) {
  1086. y = this.maxScrollY;
  1087. }
  1088. for ( ; i < l; i++ ) {
  1089. if ( x >= this.pages[i][0].cx ) {
  1090. x = this.pages[i][0].x;
  1091. break;
  1092. }
  1093. }
  1094. l = this.pages[i].length;
  1095. for ( ; m < l; m++ ) {
  1096. if ( y >= this.pages[0][m].cy ) {
  1097. y = this.pages[0][m].y;
  1098. break;
  1099. }
  1100. }
  1101. if ( i == this.currentPage.pageX ) {
  1102. i += this.directionX;
  1103. if ( i < 0 ) {
  1104. i = 0;
  1105. } else if ( i >= this.pages.length ) {
  1106. i = this.pages.length - 1;
  1107. }
  1108. x = this.pages[i][0].x;
  1109. }
  1110. if ( m == this.currentPage.pageY ) {
  1111. m += this.directionY;
  1112. if ( m < 0 ) {
  1113. m = 0;
  1114. } else if ( m >= this.pages[0].length ) {
  1115. m = this.pages[0].length - 1;
  1116. }
  1117. y = this.pages[0][m].y;
  1118. }
  1119. return {
  1120. x: x,
  1121. y: y,
  1122. pageX: i,
  1123. pageY: m
  1124. };
  1125. },
  1126. goToPage: function (x, y, time, easing) {
  1127. easing = easing || this.options.bounceEasing;
  1128. if ( x >= this.pages.length ) {
  1129. x = this.pages.length - 1;
  1130. } else if ( x < 0 ) {
  1131. x = 0;
  1132. }
  1133. if ( y >= this.pages[x].length ) {
  1134. y = this.pages[x].length - 1;
  1135. } else if ( y < 0 ) {
  1136. y = 0;
  1137. }
  1138. var posX = this.pages[x][y].x,
  1139. posY = this.pages[x][y].y;
  1140. time = time === undefined ? this.options.snapSpeed || Math.max(
  1141. Math.max(
  1142. Math.min(Math.abs(posX - this.x), 1000),
  1143. Math.min(Math.abs(posY - this.y), 1000)
  1144. ), 300) : time;
  1145. this.currentPage = {
  1146. x: posX,
  1147. y: posY,
  1148. pageX: x,
  1149. pageY: y
  1150. };
  1151. this.scrollTo(posX, posY, time, easing);
  1152. },
  1153. next: function (time, easing) {
  1154. var x = this.currentPage.pageX,
  1155. y = this.currentPage.pageY;
  1156. x++;
  1157. if ( x >= this.pages.length && this.hasVerticalScroll ) {
  1158. x = 0;
  1159. y++;
  1160. }
  1161. this.goToPage(x, y, time, easing);
  1162. },
  1163. prev: function (time, easing) {
  1164. var x = this.currentPage.pageX,
  1165. y = this.currentPage.pageY;
  1166. x--;
  1167. if ( x < 0 && this.hasVerticalScroll ) {
  1168. x = 0;
  1169. y--;
  1170. }
  1171. this.goToPage(x, y, time, easing);
  1172. },
  1173. _initKeys: function (e) {
  1174. // default key bindings
  1175. var keys = {
  1176. pageUp: 33,
  1177. pageDown: 34,
  1178. end: 35,
  1179. home: 36,
  1180. left: 37,
  1181. up: 38,
  1182. right: 39,
  1183. down: 40
  1184. };
  1185. var i;
  1186. // if you give me characters I give you keycode
  1187. if ( typeof this.options.keyBindings == 'object' ) {
  1188. for ( i in this.options.keyBindings ) {
  1189. if ( typeof this.options.keyBindings[i] == 'string' ) {
  1190. this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
  1191. }
  1192. }
  1193. } else {
  1194. this.options.keyBindings = {};
  1195. }
  1196. for ( i in keys ) {
  1197. this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
  1198. }
  1199. utils.addEvent(window, 'keydown', this);
  1200. this.on('destroy', function () {
  1201. utils.removeEvent(window, 'keydown', this);
  1202. });
  1203. },
  1204. _key: function (e) {
  1205. if ( !this.enabled ) {
  1206. return;
  1207. }
  1208. var snap = this.options.snap, // we are using this alot, better to cache it
  1209. newX = snap ? this.currentPage.pageX : this.x,
  1210. newY = snap ? this.currentPage.pageY : this.y,
  1211. now = utils.getTime(),
  1212. prevTime = this.keyTime || 0,
  1213. acceleration = 0.250,
  1214. pos;
  1215. if ( this.options.useTransition && this.isInTransition ) {
  1216. pos = this.getComputedPosition();
  1217. this._translate(Math.round(pos.x), Math.round(pos.y));
  1218. this.isInTransition = false;
  1219. }
  1220. this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
  1221. switch ( e.keyCode ) {
  1222. case this.options.keyBindings.pageUp:
  1223. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1224. newX += snap ? 1 : this.wrapperWidth;
  1225. } else {
  1226. newY += snap ? 1 : this.wrapperHeight;
  1227. }
  1228. break;
  1229. case this.options.keyBindings.pageDown:
  1230. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1231. newX -= snap ? 1 : this.wrapperWidth;
  1232. } else {
  1233. newY -= snap ? 1 : this.wrapperHeight;
  1234. }
  1235. break;
  1236. case this.options.keyBindings.end:
  1237. newX = snap ? this.pages.length-1 : this.maxScrollX;
  1238. newY = snap ? this.pages[0].length-1 : this.maxScrollY;
  1239. break;
  1240. case this.options.keyBindings.home:
  1241. newX = 0;
  1242. newY = 0;
  1243. break;
  1244. case this.options.keyBindings.left:
  1245. newX += snap ? -1 : 5 + this.keyAcceleration>>0;
  1246. break;
  1247. case this.options.keyBindings.up:
  1248. newY += snap ? 1 : 5 + this.keyAcceleration>>0;
  1249. break;
  1250. case this.options.keyBindings.right:
  1251. newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
  1252. break;
  1253. case this.options.keyBindings.down:
  1254. newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
  1255. break;
  1256. default:
  1257. return;
  1258. }
  1259. if ( snap ) {
  1260. this.goToPage(newX, newY);
  1261. return;
  1262. }
  1263. if ( newX > 0 ) {
  1264. newX = 0;
  1265. this.keyAcceleration = 0;
  1266. } else if ( newX < this.maxScrollX ) {
  1267. newX = this.maxScrollX;
  1268. this.keyAcceleration = 0;
  1269. }
  1270. if ( newY > 0 ) {
  1271. newY = 0;
  1272. this.keyAcceleration = 0;
  1273. } else if ( newY < this.maxScrollY ) {
  1274. newY = this.maxScrollY;
  1275. this.keyAcceleration = 0;
  1276. }
  1277. this.scrollTo(newX, newY, 0);
  1278. this.keyTime = now;
  1279. },
  1280. _animate: function (destX, destY, duration, easingFn) {
  1281. var that = this,
  1282. startX = this.x,
  1283. startY = this.y,
  1284. startTime = utils.getTime(),
  1285. destTime = startTime + duration;
  1286. function step () {
  1287. var now = utils.getTime(),
  1288. newX, newY,
  1289. easing;
  1290. if ( now >= destTime ) {
  1291. that.isAnimating = false;
  1292. that._translate(destX, destY);
  1293. if ( !that.resetPosition(that.options.bounceTime) ) {
  1294. that._execEvent('scrollEnd');
  1295. }
  1296. return;
  1297. }
  1298. now = ( now - startTime ) / duration;
  1299. easing = easingFn(now);
  1300. newX = ( destX - startX ) * easing + startX;
  1301. newY = ( destY - startY ) * easing + startY;
  1302. that._translate(newX, newY);
  1303. if ( that.isAnimating ) {
  1304. rAF(step);
  1305. }
  1306. if ( that.options.probeType == 3 ) {
  1307. that._execEvent('scroll');
  1308. }
  1309. }
  1310. this.isAnimating = true;
  1311. step();
  1312. },
  1313. handleEvent: function (e) {
  1314. switch ( e.type ) {
  1315. case 'touchstart':
  1316. case 'pointerdown':
  1317. case 'MSPointerDown':
  1318. case 'mousedown':
  1319. this._start(e);
  1320. break;
  1321. case 'touchmove':
  1322. case 'pointermove':
  1323. case 'MSPointerMove':
  1324. case 'mousemove':
  1325. this._move(e);
  1326. break;
  1327. case 'touchend':
  1328. case 'pointerup':
  1329. case 'MSPointerUp':
  1330. case 'mouseup':
  1331. case 'touchcancel':
  1332. case 'pointercancel':
  1333. case 'MSPointerCancel':
  1334. case 'mousecancel':
  1335. this._end(e);
  1336. break;
  1337. case 'orientationchange':
  1338. case 'resize':
  1339. this._resize();
  1340. break;
  1341. case 'transitionend':
  1342. case 'webkitTransitionEnd':
  1343. case 'oTransitionEnd':
  1344. case 'MSTransitionEnd':
  1345. this._transitionEnd(e);
  1346. break;
  1347. case 'wheel':
  1348. case 'DOMMouseScroll':
  1349. case 'mousewheel':
  1350. this._wheel(e);
  1351. break;
  1352. case 'keydown':
  1353. this._key(e);
  1354. break;
  1355. case 'click':
  1356. if ( this.enabled && !e._constructed ) {
  1357. e.preventDefault();
  1358. e.stopPropagation();
  1359. }
  1360. break;
  1361. }
  1362. }
  1363. };
  1364. function createDefaultScrollbar (direction, interactive, type) {
  1365. var scrollbar = document.createElement('div'),
  1366. indicator = document.createElement('div');
  1367. if ( type === true ) {
  1368. scrollbar.style.cssText = 'position:absolute;z-index:9999';
  1369. 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.5);border:1px solid rgba(255,255,255,0.9);border-radius:3px';
  1370. }
  1371. indicator.className = 'iScrollIndicator';
  1372. if ( direction == 'h' ) {
  1373. if ( type === true ) {
  1374. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1375. indicator.style.height = '100%';
  1376. }
  1377. scrollbar.className = 'iScrollHorizontalScrollbar';
  1378. } else {
  1379. if ( type === true ) {
  1380. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1381. indicator.style.width = '100%';
  1382. }
  1383. scrollbar.className = 'iScrollVerticalScrollbar';
  1384. }
  1385. scrollbar.style.cssText += ';overflow:hidden';
  1386. if ( !interactive ) {
  1387. scrollbar.style.pointerEvents = 'none';
  1388. }
  1389. scrollbar.appendChild(indicator);
  1390. return scrollbar;
  1391. }
  1392. function Indicator (scroller, options) {
  1393. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  1394. this.wrapperStyle = this.wrapper.style;
  1395. this.indicator = this.wrapper.children[0];
  1396. this.indicatorStyle = this.indicator.style;
  1397. this.scroller = scroller;
  1398. this.options = {
  1399. listenX: true,
  1400. listenY: true,
  1401. interactive: false,
  1402. resize: true,
  1403. defaultScrollbars: false,
  1404. shrink: false,
  1405. fade: false,
  1406. speedRatioX: 0,
  1407. speedRatioY: 0
  1408. };
  1409. for ( var i in options ) {
  1410. this.options[i] = options[i];
  1411. }
  1412. this.sizeRatioX = 1;
  1413. this.sizeRatioY = 1;
  1414. this.maxPosX = 0;
  1415. this.maxPosY = 0;
  1416. if ( this.options.interactive ) {
  1417. if ( !this.options.disableTouch ) {
  1418. utils.addEvent(this.indicator, 'touchstart', this);
  1419. utils.addEvent(window, 'touchend', this);
  1420. }
  1421. if ( !this.options.disablePointer ) {
  1422. utils.addEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1423. utils.addEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1424. }
  1425. if ( !this.options.disableMouse ) {
  1426. utils.addEvent(this.indicator, 'mousedown', this);
  1427. utils.addEvent(window, 'mouseup', this);
  1428. }
  1429. }
  1430. if ( this.options.fade ) {
  1431. this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
  1432. var durationProp = utils.style.transitionDuration;
  1433. if(!durationProp) {
  1434. return;
  1435. }
  1436. this.wrapperStyle[durationProp] = utils.isBadAndroid ? '0.0001ms' : '0ms';
  1437. // remove 0.0001ms
  1438. var self = this;
  1439. if(utils.isBadAndroid) {
  1440. rAF(function() {
  1441. if(self.wrapperStyle[durationProp] === '0.0001ms') {
  1442. self.wrapperStyle[durationProp] = '0s';
  1443. }
  1444. });
  1445. }
  1446. this.wrapperStyle.opacity = '0';
  1447. }
  1448. }
  1449. Indicator.prototype = {
  1450. handleEvent: function (e) {
  1451. switch ( e.type ) {
  1452. case 'touchstart':
  1453. case 'pointerdown':
  1454. case 'MSPointerDown':
  1455. case 'mousedown':
  1456. this._start(e);
  1457. break;
  1458. case 'touchmove':
  1459. case 'pointermove':
  1460. case 'MSPointerMove':
  1461. case 'mousemove':
  1462. this._move(e);
  1463. break;
  1464. case 'touchend':
  1465. case 'pointerup':
  1466. case 'MSPointerUp':
  1467. case 'mouseup':
  1468. case 'touchcancel':
  1469. case 'pointercancel':
  1470. case 'MSPointerCancel':
  1471. case 'mousecancel':
  1472. this._end(e);
  1473. break;
  1474. }
  1475. },
  1476. destroy: function () {
  1477. if ( this.options.fadeScrollbars ) {
  1478. clearTimeout(this.fadeTimeout);
  1479. this.fadeTimeout = null;
  1480. }
  1481. if ( this.options.interactive ) {
  1482. utils.removeEvent(this.indicator, 'touchstart', this);
  1483. utils.removeEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1484. utils.removeEvent(this.indicator, 'mousedown', this);
  1485. utils.removeEvent(window, 'touchmove', this);
  1486. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1487. utils.removeEvent(window, 'mousemove', this);
  1488. utils.removeEvent(window, 'touchend', this);
  1489. utils.removeEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1490. utils.removeEvent(window, 'mouseup', this);
  1491. }
  1492. if ( this.options.defaultScrollbars ) {
  1493. this.wrapper.parentNode.removeChild(this.wrapper);
  1494. }
  1495. },
  1496. _start: function (e) {
  1497. var point = e.touches ? e.touches[0] : e;
  1498. e.preventDefault();
  1499. e.stopPropagation();
  1500. this.transitionTime();
  1501. this.initiated = true;
  1502. this.moved = false;
  1503. this.lastPointX = point.pageX;
  1504. this.lastPointY = point.pageY;
  1505. this.startTime = utils.getTime();
  1506. if ( !this.options.disableTouch ) {
  1507. utils.addEvent(window, 'touchmove', this);
  1508. }
  1509. if ( !this.options.disablePointer ) {
  1510. utils.addEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1511. }
  1512. if ( !this.options.disableMouse ) {
  1513. utils.addEvent(window, 'mousemove', this);
  1514. }
  1515. this.scroller._execEvent('beforeScrollStart');
  1516. },
  1517. _move: function (e) {
  1518. var point = e.touches ? e.touches[0] : e,
  1519. deltaX, deltaY,
  1520. newX, newY,
  1521. timestamp = utils.getTime();
  1522. if ( !this.moved ) {
  1523. this.scroller._execEvent('scrollStart');
  1524. }
  1525. this.moved = true;
  1526. deltaX = point.pageX - this.lastPointX;
  1527. this.lastPointX = point.pageX;
  1528. deltaY = point.pageY - this.lastPointY;
  1529. this.lastPointY = point.pageY;
  1530. newX = this.x + deltaX;
  1531. newY = this.y + deltaY;
  1532. this._pos(newX, newY);
  1533. if ( this.scroller.options.probeType == 1 && timestamp - this.startTime > 300 ) {
  1534. this.startTime = timestamp;
  1535. this.scroller._execEvent('scroll');
  1536. } else if ( this.scroller.options.probeType > 1 ) {
  1537. this.scroller._execEvent('scroll');
  1538. }
  1539. // INSERT POINT: indicator._move
  1540. e.preventDefault();
  1541. e.stopPropagation();
  1542. },
  1543. _end: function (e) {
  1544. if ( !this.initiated ) {
  1545. return;
  1546. }
  1547. this.initiated = false;
  1548. e.preventDefault();
  1549. e.stopPropagation();
  1550. utils.removeEvent(window, 'touchmove', this);
  1551. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1552. utils.removeEvent(window, 'mousemove', this);
  1553. if ( this.scroller.options.snap ) {
  1554. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  1555. var time = this.options.snapSpeed || Math.max(
  1556. Math.max(
  1557. Math.min(Math.abs(this.scroller.x - snap.x), 1000),
  1558. Math.min(Math.abs(this.scroller.y - snap.y), 1000)
  1559. ), 300);
  1560. if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
  1561. this.scroller.directionX = 0;
  1562. this.scroller.directionY = 0;
  1563. this.scroller.currentPage = snap;
  1564. this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
  1565. }
  1566. }
  1567. if ( this.moved ) {
  1568. this.scroller._execEvent('scrollEnd');
  1569. }
  1570. },
  1571. transitionTime: function (time) {
  1572. time = time || 0;
  1573. var durationProp = utils.style.transitionDuration;
  1574. if(!durationProp) {
  1575. return;
  1576. }
  1577. this.indicatorStyle[durationProp] = time + 'ms';
  1578. if ( !time && utils.isBadAndroid ) {
  1579. this.indicatorStyle[durationProp] = '0.0001ms';
  1580. // remove 0.0001ms
  1581. var self = this;
  1582. rAF(function() {
  1583. if(self.indicatorStyle[durationProp] === '0.0001ms') {
  1584. self.indicatorStyle[durationProp] = '0s';
  1585. }
  1586. });
  1587. }
  1588. },
  1589. transitionTimingFunction: function (easing) {
  1590. this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
  1591. },
  1592. refresh: function () {
  1593. this.transitionTime();
  1594. if ( this.options.listenX && !this.options.listenY ) {
  1595. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  1596. } else if ( this.options.listenY && !this.options.listenX ) {
  1597. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  1598. } else {
  1599. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  1600. }
  1601. if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
  1602. utils.addClass(this.wrapper, 'iScrollBothScrollbars');
  1603. utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
  1604. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1605. if ( this.options.listenX ) {
  1606. this.wrapper.style.right = '8px';
  1607. } else {
  1608. this.wrapper.style.bottom = '8px';
  1609. }
  1610. }
  1611. } else {
  1612. utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
  1613. utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
  1614. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1615. if ( this.options.listenX ) {
  1616. this.wrapper.style.right = '2px';
  1617. } else {
  1618. this.wrapper.style.bottom = '2px';
  1619. }
  1620. }
  1621. }
  1622. var r = this.wrapper.offsetHeight; // force refresh
  1623. if ( this.options.listenX ) {
  1624. this.wrapperWidth = this.wrapper.clientWidth;
  1625. if ( this.options.resize ) {
  1626. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
  1627. this.indicatorStyle.width = this.indicatorWidth + 'px';
  1628. } else {
  1629. this.indicatorWidth = this.indicator.clientWidth;
  1630. }
  1631. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  1632. if ( this.options.shrink == 'clip' ) {
  1633. this.minBoundaryX = -this.indicatorWidth + 8;
  1634. this.maxBoundaryX = this.wrapperWidth - 8;
  1635. } else {
  1636. this.minBoundaryX = 0;
  1637. this.maxBoundaryX = this.maxPosX;
  1638. }
  1639. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  1640. }
  1641. if ( this.options.listenY ) {
  1642. this.wrapperHeight = this.wrapper.clientHeight;
  1643. if ( this.options.resize ) {
  1644. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
  1645. this.indicatorStyle.height = this.indicatorHeight + 'px';
  1646. } else {
  1647. this.indicatorHeight = this.indicator.clientHeight;
  1648. }
  1649. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1650. if ( this.options.shrink == 'clip' ) {
  1651. this.minBoundaryY = -this.indicatorHeight + 8;
  1652. this.maxBoundaryY = this.wrapperHeight - 8;
  1653. } else {
  1654. this.minBoundaryY = 0;
  1655. this.maxBoundaryY = this.maxPosY;
  1656. }
  1657. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1658. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
  1659. }
  1660. this.updatePosition();
  1661. },
  1662. updatePosition: function () {
  1663. var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  1664. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
  1665. if ( !this.options.ignoreBoundaries ) {
  1666. if ( x < this.minBoundaryX ) {
  1667. if ( this.options.shrink == 'scale' ) {
  1668. this.width = Math.max(this.indicatorWidth + x, 8);
  1669. this.indicatorStyle.width = this.width + 'px';
  1670. }
  1671. x = this.minBoundaryX;
  1672. } else if ( x > this.maxBoundaryX ) {
  1673. if ( this.options.shrink == 'scale' ) {
  1674. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
  1675. this.indicatorStyle.width = this.width + 'px';
  1676. x = this.maxPosX + this.indicatorWidth - this.width;
  1677. } else {
  1678. x = this.maxBoundaryX;
  1679. }
  1680. } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
  1681. this.width = this.indicatorWidth;
  1682. this.indicatorStyle.width = this.width + 'px';
  1683. }
  1684. if ( y < this.minBoundaryY ) {
  1685. if ( this.options.shrink == 'scale' ) {
  1686. this.height = Math.max(this.indicatorHeight + y * 3, 8);
  1687. this.indicatorStyle.height = this.height + 'px';
  1688. }
  1689. y = this.minBoundaryY;
  1690. } else if ( y > this.maxBoundaryY ) {
  1691. if ( this.options.shrink == 'scale' ) {
  1692. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
  1693. this.indicatorStyle.height = this.height + 'px';
  1694. y = this.maxPosY + this.indicatorHeight - this.height;
  1695. } else {
  1696. y = this.maxBoundaryY;
  1697. }
  1698. } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
  1699. this.height = this.indicatorHeight;
  1700. this.indicatorStyle.height = this.height + 'px';
  1701. }
  1702. }
  1703. this.x = x;
  1704. this.y = y;
  1705. if ( this.scroller.options.useTransform ) {
  1706. this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
  1707. } else {
  1708. this.indicatorStyle.left = x + 'px';
  1709. this.indicatorStyle.top = y + 'px';
  1710. }
  1711. },
  1712. _pos: function (x, y) {
  1713. if ( x < 0 ) {
  1714. x = 0;
  1715. } else if ( x > this.maxPosX ) {
  1716. x = this.maxPosX;
  1717. }
  1718. if ( y < 0 ) {
  1719. y = 0;
  1720. } else if ( y > this.maxPosY ) {
  1721. y = this.maxPosY;
  1722. }
  1723. x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
  1724. y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
  1725. this.scroller.scrollTo(x, y);
  1726. },
  1727. fade: function (val, hold) {
  1728. if ( hold && !this.visible ) {
  1729. return;
  1730. }
  1731. clearTimeout(this.fadeTimeout);
  1732. this.fadeTimeout = null;
  1733. var time = val ? 250 : 500,
  1734. delay = val ? 0 : 300;
  1735. val = val ? '1' : '0';
  1736. this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
  1737. this.fadeTimeout = setTimeout((function (val) {
  1738. this.wrapperStyle.opacity = val;
  1739. this.visible = +val;
  1740. }).bind(this, val), delay);
  1741. }
  1742. };
  1743. IScroll.utils = utils;
  1744. if ( typeof module != 'undefined' && module.exports ) {
  1745. module.exports = IScroll;
  1746. } else if ( typeof define == 'function' && define.amd ) {
  1747. define( function () { return IScroll; } );
  1748. } else {
  1749. window.IScroll = IScroll;
  1750. }
  1751. })(window, document, Math);