iscroll.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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. // 2018.6.22修改
  721. var _tid = document.getElementById('numberID');
  722. // var _xin =this.maxScrollX;
  723. var _xin =parseInt(((this.maxScrollX)/5)/10)*10;
  724. var _x = parseInt((x)/5);
  725. // console.log(_x)
  726. // console.log(_xin)
  727. // console.log(parseInt((x)/5))
  728. if(_x <= 0){
  729. if(_x == 0){
  730. _tid.innerHTML='不限';
  731. }else if(_x >=_xin){
  732. _tid.innerHTML=(-parseInt(x/5))*10+'万';
  733. }else {
  734. _tid.innerHTML=1000+'万以上';
  735. }
  736. }
  737. // console.log(this.maxScrollX)
  738. /* REPLACE END: _translate */
  739. } else {
  740. x = Math.round(x);
  741. y = Math.round(y);
  742. this.scrollerStyle.left = x + 'px';
  743. this.scrollerStyle.top = y + 'px';
  744. }
  745. this.x = x;
  746. this.y = y;
  747. if ( this.indicators ) {
  748. for ( var i = this.indicators.length; i--; ) {
  749. this.indicators[i].updatePosition();
  750. }
  751. }
  752. // console.log(x)
  753. // INSERT POINT: _translate
  754. },
  755. _initEvents: function (remove) {
  756. var eventType = remove ? utils.removeEvent : utils.addEvent,
  757. target = this.options.bindToWrapper ? this.wrapper : window;
  758. eventType(window, 'orientationchange', this);
  759. eventType(window, 'resize', this);
  760. if ( this.options.click ) {
  761. eventType(this.wrapper, 'click', this, true);
  762. }
  763. if ( !this.options.disableMouse ) {
  764. eventType(this.wrapper, 'mousedown', this);
  765. eventType(target, 'mousemove', this);
  766. eventType(target, 'mousecancel', this);
  767. eventType(target, 'mouseup', this);
  768. }
  769. if ( utils.hasPointer && !this.options.disablePointer ) {
  770. eventType(this.wrapper, utils.prefixPointerEvent('pointerdown'), this);
  771. eventType(target, utils.prefixPointerEvent('pointermove'), this);
  772. eventType(target, utils.prefixPointerEvent('pointercancel'), this);
  773. eventType(target, utils.prefixPointerEvent('pointerup'), this);
  774. }
  775. if ( utils.hasTouch && !this.options.disableTouch ) {
  776. eventType(this.wrapper, 'touchstart', this);
  777. eventType(target, 'touchmove', this);
  778. eventType(target, 'touchcancel', this);
  779. eventType(target, 'touchend', this);
  780. }
  781. eventType(this.scroller, 'transitionend', this);
  782. eventType(this.scroller, 'webkitTransitionEnd', this);
  783. eventType(this.scroller, 'oTransitionEnd', this);
  784. eventType(this.scroller, 'MSTransitionEnd', this);
  785. },
  786. getComputedPosition: function () {
  787. var matrix = window.getComputedStyle(this.scroller, null),
  788. x, y;
  789. if ( this.options.useTransform ) {
  790. matrix = matrix[utils.style.transform].split(')')[0].split(', ');
  791. x = +(matrix[12] || matrix[4]);
  792. y = +(matrix[13] || matrix[5]);
  793. } else {
  794. x = +matrix.left.replace(/[^-\d.]/g, '');
  795. y = +matrix.top.replace(/[^-\d.]/g, '');
  796. }
  797. return { x: x, y: y };
  798. },
  799. _initIndicators: function () {
  800. var interactive = this.options.interactiveScrollbars,
  801. customStyle = typeof this.options.scrollbars != 'string',
  802. indicators = [],
  803. indicator;
  804. var that = this;
  805. this.indicators = [];
  806. if ( this.options.scrollbars ) {
  807. // Vertical scrollbar
  808. if ( this.options.scrollY ) {
  809. indicator = {
  810. el: createDefaultScrollbar('v', interactive, this.options.scrollbars),
  811. interactive: interactive,
  812. defaultScrollbars: true,
  813. customStyle: customStyle,
  814. resize: this.options.resizeScrollbars,
  815. shrink: this.options.shrinkScrollbars,
  816. fade: this.options.fadeScrollbars,
  817. listenX: false
  818. };
  819. this.wrapper.appendChild(indicator.el);
  820. indicators.push(indicator);
  821. }
  822. // Horizontal scrollbar
  823. if ( this.options.scrollX ) {
  824. indicator = {
  825. el: createDefaultScrollbar('h', interactive, this.options.scrollbars),
  826. interactive: interactive,
  827. defaultScrollbars: true,
  828. customStyle: customStyle,
  829. resize: this.options.resizeScrollbars,
  830. shrink: this.options.shrinkScrollbars,
  831. fade: this.options.fadeScrollbars,
  832. listenY: false
  833. };
  834. this.wrapper.appendChild(indicator.el);
  835. indicators.push(indicator);
  836. }
  837. }
  838. if ( this.options.indicators ) {
  839. // TODO: check concat compatibility
  840. indicators = indicators.concat(this.options.indicators);
  841. }
  842. for ( var i = indicators.length; i--; ) {
  843. this.indicators.push( new Indicator(this, indicators[i]) );
  844. }
  845. // TODO: check if we can use array.map (wide compatibility and performance issues)
  846. function _indicatorsMap (fn) {
  847. if (that.indicators) {
  848. for ( var i = that.indicators.length; i--; ) {
  849. fn.call(that.indicators[i]);
  850. }
  851. }
  852. }
  853. if ( this.options.fadeScrollbars ) {
  854. this.on('scrollEnd', function () {
  855. _indicatorsMap(function () {
  856. this.fade();
  857. });
  858. });
  859. this.on('scrollCancel', function () {
  860. _indicatorsMap(function () {
  861. this.fade();
  862. });
  863. });
  864. this.on('scrollStart', function () {
  865. _indicatorsMap(function () {
  866. this.fade(1);
  867. });
  868. });
  869. this.on('beforeScrollStart', function () {
  870. _indicatorsMap(function () {
  871. this.fade(1, true);
  872. });
  873. });
  874. }
  875. this.on('refresh', function () {
  876. _indicatorsMap(function () {
  877. this.refresh();
  878. });
  879. });
  880. this.on('destroy', function () {
  881. _indicatorsMap(function () {
  882. this.destroy();
  883. });
  884. delete this.indicators;
  885. });
  886. },
  887. _initWheel: function () {
  888. utils.addEvent(this.wrapper, 'wheel', this);
  889. utils.addEvent(this.wrapper, 'mousewheel', this);
  890. utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
  891. this.on('destroy', function () {
  892. clearTimeout(this.wheelTimeout);
  893. this.wheelTimeout = null;
  894. utils.removeEvent(this.wrapper, 'wheel', this);
  895. utils.removeEvent(this.wrapper, 'mousewheel', this);
  896. utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
  897. });
  898. },
  899. _wheel: function (e) {
  900. if ( !this.enabled ) {
  901. return;
  902. }
  903. e.preventDefault();
  904. var wheelDeltaX, wheelDeltaY,
  905. newX, newY,
  906. that = this;
  907. if ( this.wheelTimeout === undefined ) {
  908. that._execEvent('scrollStart');
  909. }
  910. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  911. clearTimeout(this.wheelTimeout);
  912. this.wheelTimeout = setTimeout(function () {
  913. if(!that.options.snap) {
  914. that._execEvent('scrollEnd');
  915. }
  916. that.wheelTimeout = undefined;
  917. }, 400);
  918. if ( 'deltaX' in e ) {
  919. if (e.deltaMode === 1) {
  920. wheelDeltaX = -e.deltaX * this.options.mouseWheelSpeed;
  921. wheelDeltaY = -e.deltaY * this.options.mouseWheelSpeed;
  922. } else {
  923. wheelDeltaX = -e.deltaX;
  924. wheelDeltaY = -e.deltaY;
  925. }
  926. } else if ( 'wheelDeltaX' in e ) {
  927. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  928. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  929. } else if ( 'wheelDelta' in e ) {
  930. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  931. } else if ( 'detail' in e ) {
  932. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  933. } else {
  934. return;
  935. }
  936. wheelDeltaX *= this.options.invertWheelDirection;
  937. wheelDeltaY *= this.options.invertWheelDirection;
  938. if ( !this.hasVerticalScroll ) {
  939. wheelDeltaX = wheelDeltaY;
  940. wheelDeltaY = 0;
  941. }
  942. if ( this.options.snap ) {
  943. newX = this.currentPage.pageX;
  944. newY = this.currentPage.pageY;
  945. if ( wheelDeltaX > 0 ) {
  946. newX--;
  947. } else if ( wheelDeltaX < 0 ) {
  948. newX++;
  949. }
  950. if ( wheelDeltaY > 0 ) {
  951. newY--;
  952. } else if ( wheelDeltaY < 0 ) {
  953. newY++;
  954. }
  955. this.goToPage(newX, newY);
  956. return;
  957. }
  958. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  959. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  960. this.directionX = wheelDeltaX > 0 ? -1 : wheelDeltaX < 0 ? 1 : 0;
  961. this.directionY = wheelDeltaY > 0 ? -1 : wheelDeltaY < 0 ? 1 : 0;
  962. if ( newX > 0 ) {
  963. newX = 0;
  964. } else if ( newX < this.maxScrollX ) {
  965. newX = this.maxScrollX;
  966. }
  967. if ( newY > 0 ) {
  968. newY = 0;
  969. } else if ( newY < this.maxScrollY ) {
  970. newY = this.maxScrollY;
  971. }
  972. this.scrollTo(newX, newY, 0);
  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. }
  1307. this.isAnimating = true;
  1308. step();
  1309. },
  1310. handleEvent: function (e) {
  1311. switch ( e.type ) {
  1312. case 'touchstart':
  1313. case 'pointerdown':
  1314. case 'MSPointerDown':
  1315. case 'mousedown':
  1316. this._start(e);
  1317. break;
  1318. case 'touchmove':
  1319. case 'pointermove':
  1320. case 'MSPointerMove':
  1321. case 'mousemove':
  1322. this._move(e);
  1323. break;
  1324. case 'touchend':
  1325. case 'pointerup':
  1326. case 'MSPointerUp':
  1327. case 'mouseup':
  1328. case 'touchcancel':
  1329. case 'pointercancel':
  1330. case 'MSPointerCancel':
  1331. case 'mousecancel':
  1332. this._end(e);
  1333. break;
  1334. case 'orientationchange':
  1335. case 'resize':
  1336. this._resize();
  1337. break;
  1338. case 'transitionend':
  1339. case 'webkitTransitionEnd':
  1340. case 'oTransitionEnd':
  1341. case 'MSTransitionEnd':
  1342. this._transitionEnd(e);
  1343. break;
  1344. case 'wheel':
  1345. case 'DOMMouseScroll':
  1346. case 'mousewheel':
  1347. this._wheel(e);
  1348. break;
  1349. case 'keydown':
  1350. this._key(e);
  1351. break;
  1352. case 'click':
  1353. if ( this.enabled && !e._constructed ) {
  1354. e.preventDefault();
  1355. e.stopPropagation();
  1356. }
  1357. break;
  1358. }
  1359. }
  1360. };
  1361. function createDefaultScrollbar (direction, interactive, type) {
  1362. var scrollbar = document.createElement('div'),
  1363. indicator = document.createElement('div');
  1364. if ( type === true ) {
  1365. scrollbar.style.cssText = 'position:absolute;z-index:9999';
  1366. 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';
  1367. }
  1368. indicator.className = 'iScrollIndicator';
  1369. if ( direction == 'h' ) {
  1370. if ( type === true ) {
  1371. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1372. indicator.style.height = '100%';
  1373. }
  1374. scrollbar.className = 'iScrollHorizontalScrollbar';
  1375. } else {
  1376. if ( type === true ) {
  1377. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1378. indicator.style.width = '100%';
  1379. }
  1380. scrollbar.className = 'iScrollVerticalScrollbar';
  1381. }
  1382. scrollbar.style.cssText += ';overflow:hidden';
  1383. if ( !interactive ) {
  1384. scrollbar.style.pointerEvents = 'none';
  1385. }
  1386. scrollbar.appendChild(indicator);
  1387. return scrollbar;
  1388. }
  1389. function Indicator (scroller, options) {
  1390. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  1391. this.wrapperStyle = this.wrapper.style;
  1392. this.indicator = this.wrapper.children[0];
  1393. this.indicatorStyle = this.indicator.style;
  1394. this.scroller = scroller;
  1395. this.options = {
  1396. listenX: true,
  1397. listenY: true,
  1398. interactive: false,
  1399. resize: true,
  1400. defaultScrollbars: false,
  1401. shrink: false,
  1402. fade: false,
  1403. speedRatioX: 0,
  1404. speedRatioY: 0
  1405. };
  1406. for ( var i in options ) {
  1407. this.options[i] = options[i];
  1408. }
  1409. this.sizeRatioX = 1;
  1410. this.sizeRatioY = 1;
  1411. this.maxPosX = 0;
  1412. this.maxPosY = 0;
  1413. if ( this.options.interactive ) {
  1414. if ( !this.options.disableTouch ) {
  1415. utils.addEvent(this.indicator, 'touchstart', this);
  1416. utils.addEvent(window, 'touchend', this);
  1417. }
  1418. if ( !this.options.disablePointer ) {
  1419. utils.addEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1420. utils.addEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1421. }
  1422. if ( !this.options.disableMouse ) {
  1423. utils.addEvent(this.indicator, 'mousedown', this);
  1424. utils.addEvent(window, 'mouseup', this);
  1425. }
  1426. }
  1427. if ( this.options.fade ) {
  1428. this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
  1429. var durationProp = utils.style.transitionDuration;
  1430. if(!durationProp) {
  1431. return;
  1432. }
  1433. this.wrapperStyle[durationProp] = utils.isBadAndroid ? '0.0001ms' : '0ms';
  1434. // remove 0.0001ms
  1435. var self = this;
  1436. if(utils.isBadAndroid) {
  1437. rAF(function() {
  1438. if(self.wrapperStyle[durationProp] === '0.0001ms') {
  1439. self.wrapperStyle[durationProp] = '0s';
  1440. }
  1441. });
  1442. }
  1443. this.wrapperStyle.opacity = '0';
  1444. }
  1445. }
  1446. Indicator.prototype = {
  1447. handleEvent: function (e) {
  1448. switch ( e.type ) {
  1449. case 'touchstart':
  1450. case 'pointerdown':
  1451. case 'MSPointerDown':
  1452. case 'mousedown':
  1453. this._start(e);
  1454. break;
  1455. case 'touchmove':
  1456. case 'pointermove':
  1457. case 'MSPointerMove':
  1458. case 'mousemove':
  1459. this._move(e);
  1460. break;
  1461. case 'touchend':
  1462. case 'pointerup':
  1463. case 'MSPointerUp':
  1464. case 'mouseup':
  1465. case 'touchcancel':
  1466. case 'pointercancel':
  1467. case 'MSPointerCancel':
  1468. case 'mousecancel':
  1469. this._end(e);
  1470. break;
  1471. }
  1472. },
  1473. destroy: function () {
  1474. if ( this.options.fadeScrollbars ) {
  1475. clearTimeout(this.fadeTimeout);
  1476. this.fadeTimeout = null;
  1477. }
  1478. if ( this.options.interactive ) {
  1479. utils.removeEvent(this.indicator, 'touchstart', this);
  1480. utils.removeEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1481. utils.removeEvent(this.indicator, 'mousedown', this);
  1482. utils.removeEvent(window, 'touchmove', this);
  1483. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1484. utils.removeEvent(window, 'mousemove', this);
  1485. utils.removeEvent(window, 'touchend', this);
  1486. utils.removeEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1487. utils.removeEvent(window, 'mouseup', this);
  1488. }
  1489. if ( this.options.defaultScrollbars ) {
  1490. this.wrapper.parentNode.removeChild(this.wrapper);
  1491. }
  1492. },
  1493. _start: function (e) {
  1494. var point = e.touches ? e.touches[0] : e;
  1495. e.preventDefault();
  1496. e.stopPropagation();
  1497. this.transitionTime();
  1498. this.initiated = true;
  1499. this.moved = false;
  1500. this.lastPointX = point.pageX;
  1501. this.lastPointY = point.pageY;
  1502. this.startTime = utils.getTime();
  1503. if ( !this.options.disableTouch ) {
  1504. utils.addEvent(window, 'touchmove', this);
  1505. }
  1506. if ( !this.options.disablePointer ) {
  1507. utils.addEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1508. }
  1509. if ( !this.options.disableMouse ) {
  1510. utils.addEvent(window, 'mousemove', this);
  1511. }
  1512. this.scroller._execEvent('beforeScrollStart');
  1513. },
  1514. _move: function (e) {
  1515. var point = e.touches ? e.touches[0] : e,
  1516. deltaX, deltaY,
  1517. newX, newY,
  1518. timestamp = utils.getTime();
  1519. if ( !this.moved ) {
  1520. this.scroller._execEvent('scrollStart');
  1521. }
  1522. this.moved = true;
  1523. deltaX = point.pageX - this.lastPointX;
  1524. this.lastPointX = point.pageX;
  1525. deltaY = point.pageY - this.lastPointY;
  1526. this.lastPointY = point.pageY;
  1527. newX = this.x + deltaX;
  1528. newY = this.y + deltaY;
  1529. this._pos(newX, newY);
  1530. // INSERT POINT: indicator._move
  1531. e.preventDefault();
  1532. e.stopPropagation();
  1533. },
  1534. _end: function (e) {
  1535. if ( !this.initiated ) {
  1536. return;
  1537. }
  1538. this.initiated = false;
  1539. e.preventDefault();
  1540. e.stopPropagation();
  1541. utils.removeEvent(window, 'touchmove', this);
  1542. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1543. utils.removeEvent(window, 'mousemove', this);
  1544. if ( this.scroller.options.snap ) {
  1545. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  1546. var time = this.options.snapSpeed || Math.max(
  1547. Math.max(
  1548. Math.min(Math.abs(this.scroller.x - snap.x), 1000),
  1549. Math.min(Math.abs(this.scroller.y - snap.y), 1000)
  1550. ), 300);
  1551. if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
  1552. this.scroller.directionX = 0;
  1553. this.scroller.directionY = 0;
  1554. this.scroller.currentPage = snap;
  1555. this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
  1556. }
  1557. }
  1558. if ( this.moved ) {
  1559. this.scroller._execEvent('scrollEnd');
  1560. }
  1561. },
  1562. transitionTime: function (time) {
  1563. time = time || 0;
  1564. var durationProp = utils.style.transitionDuration;
  1565. if(!durationProp) {
  1566. return;
  1567. }
  1568. this.indicatorStyle[durationProp] = time + 'ms';
  1569. if ( !time && utils.isBadAndroid ) {
  1570. this.indicatorStyle[durationProp] = '0.0001ms';
  1571. // remove 0.0001ms
  1572. var self = this;
  1573. rAF(function() {
  1574. if(self.indicatorStyle[durationProp] === '0.0001ms') {
  1575. self.indicatorStyle[durationProp] = '0s';
  1576. }
  1577. });
  1578. }
  1579. },
  1580. transitionTimingFunction: function (easing) {
  1581. this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
  1582. },
  1583. refresh: function () {
  1584. this.transitionTime();
  1585. if ( this.options.listenX && !this.options.listenY ) {
  1586. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  1587. } else if ( this.options.listenY && !this.options.listenX ) {
  1588. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  1589. } else {
  1590. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  1591. }
  1592. if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
  1593. utils.addClass(this.wrapper, 'iScrollBothScrollbars');
  1594. utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
  1595. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1596. if ( this.options.listenX ) {
  1597. this.wrapper.style.right = '8px';
  1598. } else {
  1599. this.wrapper.style.bottom = '8px';
  1600. }
  1601. }
  1602. } else {
  1603. utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
  1604. utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
  1605. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1606. if ( this.options.listenX ) {
  1607. this.wrapper.style.right = '2px';
  1608. } else {
  1609. this.wrapper.style.bottom = '2px';
  1610. }
  1611. }
  1612. }
  1613. var r = this.wrapper.offsetHeight; // force refresh
  1614. if ( this.options.listenX ) {
  1615. this.wrapperWidth = this.wrapper.clientWidth;
  1616. if ( this.options.resize ) {
  1617. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
  1618. this.indicatorStyle.width = this.indicatorWidth + 'px';
  1619. } else {
  1620. this.indicatorWidth = this.indicator.clientWidth;
  1621. }
  1622. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  1623. if ( this.options.shrink == 'clip' ) {
  1624. this.minBoundaryX = -this.indicatorWidth + 8;
  1625. this.maxBoundaryX = this.wrapperWidth - 8;
  1626. } else {
  1627. this.minBoundaryX = 0;
  1628. this.maxBoundaryX = this.maxPosX;
  1629. }
  1630. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  1631. }
  1632. if ( this.options.listenY ) {
  1633. this.wrapperHeight = this.wrapper.clientHeight;
  1634. if ( this.options.resize ) {
  1635. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
  1636. this.indicatorStyle.height = this.indicatorHeight + 'px';
  1637. } else {
  1638. this.indicatorHeight = this.indicator.clientHeight;
  1639. }
  1640. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1641. if ( this.options.shrink == 'clip' ) {
  1642. this.minBoundaryY = -this.indicatorHeight + 8;
  1643. this.maxBoundaryY = this.wrapperHeight - 8;
  1644. } else {
  1645. this.minBoundaryY = 0;
  1646. this.maxBoundaryY = this.maxPosY;
  1647. }
  1648. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1649. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
  1650. }
  1651. this.updatePosition();
  1652. },
  1653. updatePosition: function () {
  1654. var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  1655. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
  1656. if ( !this.options.ignoreBoundaries ) {
  1657. if ( x < this.minBoundaryX ) {
  1658. if ( this.options.shrink == 'scale' ) {
  1659. this.width = Math.max(this.indicatorWidth + x, 8);
  1660. this.indicatorStyle.width = this.width + 'px';
  1661. }
  1662. x = this.minBoundaryX;
  1663. } else if ( x > this.maxBoundaryX ) {
  1664. if ( this.options.shrink == 'scale' ) {
  1665. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
  1666. this.indicatorStyle.width = this.width + 'px';
  1667. x = this.maxPosX + this.indicatorWidth - this.width;
  1668. } else {
  1669. x = this.maxBoundaryX;
  1670. }
  1671. } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
  1672. this.width = this.indicatorWidth;
  1673. this.indicatorStyle.width = this.width + 'px';
  1674. }
  1675. if ( y < this.minBoundaryY ) {
  1676. if ( this.options.shrink == 'scale' ) {
  1677. this.height = Math.max(this.indicatorHeight + y * 3, 8);
  1678. this.indicatorStyle.height = this.height + 'px';
  1679. }
  1680. y = this.minBoundaryY;
  1681. } else if ( y > this.maxBoundaryY ) {
  1682. if ( this.options.shrink == 'scale' ) {
  1683. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
  1684. this.indicatorStyle.height = this.height + 'px';
  1685. y = this.maxPosY + this.indicatorHeight - this.height;
  1686. } else {
  1687. y = this.maxBoundaryY;
  1688. }
  1689. } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
  1690. this.height = this.indicatorHeight;
  1691. this.indicatorStyle.height = this.height + 'px';
  1692. }
  1693. }
  1694. this.x = x;
  1695. this.y = y;
  1696. if ( this.scroller.options.useTransform ) {
  1697. this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
  1698. } else {
  1699. this.indicatorStyle.left = x + 'px';
  1700. this.indicatorStyle.top = y + 'px';
  1701. }
  1702. },
  1703. _pos: function (x, y) {
  1704. if ( x < 0 ) {
  1705. x = 0;
  1706. } else if ( x > this.maxPosX ) {
  1707. x = this.maxPosX;
  1708. }
  1709. if ( y < 0 ) {
  1710. y = 0;
  1711. } else if ( y > this.maxPosY ) {
  1712. y = this.maxPosY;
  1713. }
  1714. x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
  1715. y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
  1716. this.scroller.scrollTo(x, y);
  1717. },
  1718. fade: function (val, hold) {
  1719. if ( hold && !this.visible ) {
  1720. return;
  1721. }
  1722. clearTimeout(this.fadeTimeout);
  1723. this.fadeTimeout = null;
  1724. var time = val ? 250 : 500,
  1725. delay = val ? 0 : 300;
  1726. val = val ? '1' : '0';
  1727. this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
  1728. this.fadeTimeout = setTimeout((function (val) {
  1729. this.wrapperStyle.opacity = val;
  1730. this.visible = +val;
  1731. }).bind(this, val), delay);
  1732. }
  1733. };
  1734. IScroll.utils = utils;
  1735. if ( typeof module != 'undefined' && module.exports ) {
  1736. module.exports = IScroll;
  1737. } else if ( typeof define == 'function' && define.amd ) {
  1738. define( function () { return IScroll; } );
  1739. } else {
  1740. window.IScroll = IScroll;
  1741. }
  1742. })(window, document, Math);