iscroll.js 52 KB

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