iscroll.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  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);