scrolloverflow.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  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. var wheelDeltaX, wheelDeltaY,
  885. newX, newY,
  886. that = this;
  887. if ( this.wheelTimeout === undefined ) {
  888. that._execEvent('scrollStart');
  889. }
  890. // Execute the scrollEnd event after 400ms the wheel stopped scrolling
  891. clearTimeout(this.wheelTimeout);
  892. this.wheelTimeout = setTimeout(function () {
  893. if(!that.options.snap) {
  894. that._execEvent('scrollEnd');
  895. }
  896. that.wheelTimeout = undefined;
  897. }, 400);
  898. if ( 'deltaX' in e ) {
  899. if (e.deltaMode === 1) {
  900. wheelDeltaX = -e.deltaX * this.options.mouseWheelSpeed;
  901. wheelDeltaY = -e.deltaY * this.options.mouseWheelSpeed;
  902. } else {
  903. wheelDeltaX = -e.deltaX;
  904. wheelDeltaY = -e.deltaY;
  905. }
  906. } else if ( 'wheelDeltaX' in e ) {
  907. wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
  908. wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
  909. } else if ( 'wheelDelta' in e ) {
  910. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
  911. } else if ( 'detail' in e ) {
  912. wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
  913. } else {
  914. return;
  915. }
  916. wheelDeltaX *= this.options.invertWheelDirection;
  917. wheelDeltaY *= this.options.invertWheelDirection;
  918. if ( !this.hasVerticalScroll ) {
  919. wheelDeltaX = wheelDeltaY;
  920. wheelDeltaY = 0;
  921. }
  922. if ( this.options.snap ) {
  923. newX = this.currentPage.pageX;
  924. newY = this.currentPage.pageY;
  925. if ( wheelDeltaX > 0 ) {
  926. newX--;
  927. } else if ( wheelDeltaX < 0 ) {
  928. newX++;
  929. }
  930. if ( wheelDeltaY > 0 ) {
  931. newY--;
  932. } else if ( wheelDeltaY < 0 ) {
  933. newY++;
  934. }
  935. this.goToPage(newX, newY);
  936. return;
  937. }
  938. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  939. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  940. this.directionX = wheelDeltaX > 0 ? -1 : wheelDeltaX < 0 ? 1 : 0;
  941. this.directionY = wheelDeltaY > 0 ? -1 : wheelDeltaY < 0 ? 1 : 0;
  942. if ( newX > 0 ) {
  943. newX = 0;
  944. } else if ( newX < this.maxScrollX ) {
  945. newX = this.maxScrollX;
  946. }
  947. if ( newY > 0 ) {
  948. newY = 0;
  949. } else if ( newY < this.maxScrollY ) {
  950. newY = this.maxScrollY;
  951. }
  952. this.scrollTo(newX, newY, 0);
  953. // INSERT POINT: _wheel
  954. },
  955. _initSnap: function () {
  956. this.currentPage = {};
  957. if ( typeof this.options.snap == 'string' ) {
  958. this.options.snap = this.scroller.querySelectorAll(this.options.snap);
  959. }
  960. this.on('refresh', function () {
  961. var i = 0, l,
  962. m = 0, n,
  963. cx, cy,
  964. x = 0, y,
  965. stepX = this.options.snapStepX || this.wrapperWidth,
  966. stepY = this.options.snapStepY || this.wrapperHeight,
  967. el;
  968. this.pages = [];
  969. if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
  970. return;
  971. }
  972. if ( this.options.snap === true ) {
  973. cx = Math.round( stepX / 2 );
  974. cy = Math.round( stepY / 2 );
  975. while ( x > -this.scrollerWidth ) {
  976. this.pages[i] = [];
  977. l = 0;
  978. y = 0;
  979. while ( y > -this.scrollerHeight ) {
  980. this.pages[i][l] = {
  981. x: Math.max(x, this.maxScrollX),
  982. y: Math.max(y, this.maxScrollY),
  983. width: stepX,
  984. height: stepY,
  985. cx: x - cx,
  986. cy: y - cy
  987. };
  988. y -= stepY;
  989. l++;
  990. }
  991. x -= stepX;
  992. i++;
  993. }
  994. } else {
  995. el = this.options.snap;
  996. l = el.length;
  997. n = -1;
  998. for ( ; i < l; i++ ) {
  999. if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
  1000. m = 0;
  1001. n++;
  1002. }
  1003. if ( !this.pages[m] ) {
  1004. this.pages[m] = [];
  1005. }
  1006. x = Math.max(-el[i].offsetLeft, this.maxScrollX);
  1007. y = Math.max(-el[i].offsetTop, this.maxScrollY);
  1008. cx = x - Math.round(el[i].offsetWidth / 2);
  1009. cy = y - Math.round(el[i].offsetHeight / 2);
  1010. this.pages[m][n] = {
  1011. x: x,
  1012. y: y,
  1013. width: el[i].offsetWidth,
  1014. height: el[i].offsetHeight,
  1015. cx: cx,
  1016. cy: cy
  1017. };
  1018. if ( x > this.maxScrollX ) {
  1019. m++;
  1020. }
  1021. }
  1022. }
  1023. this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
  1024. // Update snap threshold if needed
  1025. if ( this.options.snapThreshold % 1 === 0 ) {
  1026. this.snapThresholdX = this.options.snapThreshold;
  1027. this.snapThresholdY = this.options.snapThreshold;
  1028. } else {
  1029. this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
  1030. this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
  1031. }
  1032. });
  1033. this.on('flick', function () {
  1034. var time = this.options.snapSpeed || Math.max(
  1035. Math.max(
  1036. Math.min(Math.abs(this.x - this.startX), 1000),
  1037. Math.min(Math.abs(this.y - this.startY), 1000)
  1038. ), 300);
  1039. this.goToPage(
  1040. this.currentPage.pageX + this.directionX,
  1041. this.currentPage.pageY + this.directionY,
  1042. time
  1043. );
  1044. });
  1045. },
  1046. _nearestSnap: function (x, y) {
  1047. if ( !this.pages.length ) {
  1048. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  1049. }
  1050. var i = 0,
  1051. l = this.pages.length,
  1052. m = 0;
  1053. // Check if we exceeded the snap threshold
  1054. if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
  1055. Math.abs(y - this.absStartY) < this.snapThresholdY ) {
  1056. return this.currentPage;
  1057. }
  1058. if ( x > 0 ) {
  1059. x = 0;
  1060. } else if ( x < this.maxScrollX ) {
  1061. x = this.maxScrollX;
  1062. }
  1063. if ( y > 0 ) {
  1064. y = 0;
  1065. } else if ( y < this.maxScrollY ) {
  1066. y = this.maxScrollY;
  1067. }
  1068. for ( ; i < l; i++ ) {
  1069. if ( x >= this.pages[i][0].cx ) {
  1070. x = this.pages[i][0].x;
  1071. break;
  1072. }
  1073. }
  1074. l = this.pages[i].length;
  1075. for ( ; m < l; m++ ) {
  1076. if ( y >= this.pages[0][m].cy ) {
  1077. y = this.pages[0][m].y;
  1078. break;
  1079. }
  1080. }
  1081. if ( i == this.currentPage.pageX ) {
  1082. i += this.directionX;
  1083. if ( i < 0 ) {
  1084. i = 0;
  1085. } else if ( i >= this.pages.length ) {
  1086. i = this.pages.length - 1;
  1087. }
  1088. x = this.pages[i][0].x;
  1089. }
  1090. if ( m == this.currentPage.pageY ) {
  1091. m += this.directionY;
  1092. if ( m < 0 ) {
  1093. m = 0;
  1094. } else if ( m >= this.pages[0].length ) {
  1095. m = this.pages[0].length - 1;
  1096. }
  1097. y = this.pages[0][m].y;
  1098. }
  1099. return {
  1100. x: x,
  1101. y: y,
  1102. pageX: i,
  1103. pageY: m
  1104. };
  1105. },
  1106. goToPage: function (x, y, time, easing) {
  1107. easing = easing || this.options.bounceEasing;
  1108. if ( x >= this.pages.length ) {
  1109. x = this.pages.length - 1;
  1110. } else if ( x < 0 ) {
  1111. x = 0;
  1112. }
  1113. if ( y >= this.pages[x].length ) {
  1114. y = this.pages[x].length - 1;
  1115. } else if ( y < 0 ) {
  1116. y = 0;
  1117. }
  1118. var posX = this.pages[x][y].x,
  1119. posY = this.pages[x][y].y;
  1120. time = time === undefined ? this.options.snapSpeed || Math.max(
  1121. Math.max(
  1122. Math.min(Math.abs(posX - this.x), 1000),
  1123. Math.min(Math.abs(posY - this.y), 1000)
  1124. ), 300) : time;
  1125. this.currentPage = {
  1126. x: posX,
  1127. y: posY,
  1128. pageX: x,
  1129. pageY: y
  1130. };
  1131. this.scrollTo(posX, posY, time, easing);
  1132. },
  1133. next: function (time, easing) {
  1134. var x = this.currentPage.pageX,
  1135. y = this.currentPage.pageY;
  1136. x++;
  1137. if ( x >= this.pages.length && this.hasVerticalScroll ) {
  1138. x = 0;
  1139. y++;
  1140. }
  1141. this.goToPage(x, y, time, easing);
  1142. },
  1143. prev: function (time, easing) {
  1144. var x = this.currentPage.pageX,
  1145. y = this.currentPage.pageY;
  1146. x--;
  1147. if ( x < 0 && this.hasVerticalScroll ) {
  1148. x = 0;
  1149. y--;
  1150. }
  1151. this.goToPage(x, y, time, easing);
  1152. },
  1153. _initKeys: function (e) {
  1154. // default key bindings
  1155. var keys = {
  1156. pageUp: 33,
  1157. pageDown: 34,
  1158. end: 35,
  1159. home: 36,
  1160. left: 37,
  1161. up: 38,
  1162. right: 39,
  1163. down: 40
  1164. };
  1165. var i;
  1166. // if you give me characters I give you keycode
  1167. if ( typeof this.options.keyBindings == 'object' ) {
  1168. for ( i in this.options.keyBindings ) {
  1169. if ( typeof this.options.keyBindings[i] == 'string' ) {
  1170. this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
  1171. }
  1172. }
  1173. } else {
  1174. this.options.keyBindings = {};
  1175. }
  1176. for ( i in keys ) {
  1177. this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
  1178. }
  1179. utils.addEvent(window, 'keydown', this);
  1180. this.on('destroy', function () {
  1181. utils.removeEvent(window, 'keydown', this);
  1182. });
  1183. },
  1184. _key: function (e) {
  1185. if ( !this.enabled ) {
  1186. return;
  1187. }
  1188. var snap = this.options.snap, // we are using this alot, better to cache it
  1189. newX = snap ? this.currentPage.pageX : this.x,
  1190. newY = snap ? this.currentPage.pageY : this.y,
  1191. now = utils.getTime(),
  1192. prevTime = this.keyTime || 0,
  1193. acceleration = 0.250,
  1194. pos;
  1195. if ( this.options.useTransition && this.isInTransition ) {
  1196. pos = this.getComputedPosition();
  1197. this._translate(Math.round(pos.x), Math.round(pos.y));
  1198. this.isInTransition = false;
  1199. }
  1200. this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
  1201. switch ( e.keyCode ) {
  1202. case this.options.keyBindings.pageUp:
  1203. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1204. newX += snap ? 1 : this.wrapperWidth;
  1205. } else {
  1206. newY += snap ? 1 : this.wrapperHeight;
  1207. }
  1208. break;
  1209. case this.options.keyBindings.pageDown:
  1210. if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
  1211. newX -= snap ? 1 : this.wrapperWidth;
  1212. } else {
  1213. newY -= snap ? 1 : this.wrapperHeight;
  1214. }
  1215. break;
  1216. case this.options.keyBindings.end:
  1217. newX = snap ? this.pages.length-1 : this.maxScrollX;
  1218. newY = snap ? this.pages[0].length-1 : this.maxScrollY;
  1219. break;
  1220. case this.options.keyBindings.home:
  1221. newX = 0;
  1222. newY = 0;
  1223. break;
  1224. case this.options.keyBindings.left:
  1225. newX += snap ? -1 : 5 + this.keyAcceleration>>0;
  1226. break;
  1227. case this.options.keyBindings.up:
  1228. newY += snap ? 1 : 5 + this.keyAcceleration>>0;
  1229. break;
  1230. case this.options.keyBindings.right:
  1231. newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
  1232. break;
  1233. case this.options.keyBindings.down:
  1234. newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
  1235. break;
  1236. default:
  1237. return;
  1238. }
  1239. if ( snap ) {
  1240. this.goToPage(newX, newY);
  1241. return;
  1242. }
  1243. if ( newX > 0 ) {
  1244. newX = 0;
  1245. this.keyAcceleration = 0;
  1246. } else if ( newX < this.maxScrollX ) {
  1247. newX = this.maxScrollX;
  1248. this.keyAcceleration = 0;
  1249. }
  1250. if ( newY > 0 ) {
  1251. newY = 0;
  1252. this.keyAcceleration = 0;
  1253. } else if ( newY < this.maxScrollY ) {
  1254. newY = this.maxScrollY;
  1255. this.keyAcceleration = 0;
  1256. }
  1257. this.scrollTo(newX, newY, 0);
  1258. this.keyTime = now;
  1259. },
  1260. _animate: function (destX, destY, duration, easingFn) {
  1261. var that = this,
  1262. startX = this.x,
  1263. startY = this.y,
  1264. startTime = utils.getTime(),
  1265. destTime = startTime + duration;
  1266. function step () {
  1267. var now = utils.getTime(),
  1268. newX, newY,
  1269. easing;
  1270. if ( now >= destTime ) {
  1271. that.isAnimating = false;
  1272. that._translate(destX, destY);
  1273. if ( !that.resetPosition(that.options.bounceTime) ) {
  1274. that._execEvent('scrollEnd');
  1275. }
  1276. return;
  1277. }
  1278. now = ( now - startTime ) / duration;
  1279. easing = easingFn(now);
  1280. newX = ( destX - startX ) * easing + startX;
  1281. newY = ( destY - startY ) * easing + startY;
  1282. that._translate(newX, newY);
  1283. if ( that.isAnimating ) {
  1284. rAF(step);
  1285. }
  1286. }
  1287. this.isAnimating = true;
  1288. step();
  1289. },
  1290. handleEvent: function (e) {
  1291. switch ( e.type ) {
  1292. case 'touchstart':
  1293. case 'pointerdown':
  1294. case 'MSPointerDown':
  1295. case 'mousedown':
  1296. this._start(e);
  1297. break;
  1298. case 'touchmove':
  1299. case 'pointermove':
  1300. case 'MSPointerMove':
  1301. case 'mousemove':
  1302. this._move(e);
  1303. break;
  1304. case 'touchend':
  1305. case 'pointerup':
  1306. case 'MSPointerUp':
  1307. case 'mouseup':
  1308. case 'touchcancel':
  1309. case 'pointercancel':
  1310. case 'MSPointerCancel':
  1311. case 'mousecancel':
  1312. this._end(e);
  1313. break;
  1314. case 'orientationchange':
  1315. case 'resize':
  1316. this._resize();
  1317. break;
  1318. case 'transitionend':
  1319. case 'webkitTransitionEnd':
  1320. case 'oTransitionEnd':
  1321. case 'MSTransitionEnd':
  1322. this._transitionEnd(e);
  1323. break;
  1324. case 'wheel':
  1325. case 'DOMMouseScroll':
  1326. case 'mousewheel':
  1327. this._wheel(e);
  1328. break;
  1329. case 'keydown':
  1330. this._key(e);
  1331. break;
  1332. case 'click':
  1333. if ( this.enabled && !e._constructed ) {
  1334. e.preventDefault();
  1335. e.stopPropagation();
  1336. }
  1337. break;
  1338. }
  1339. }
  1340. };
  1341. function createDefaultScrollbar (direction, interactive, type) {
  1342. var scrollbar = document.createElement('div'),
  1343. indicator = document.createElement('div');
  1344. if ( type === true ) {
  1345. scrollbar.style.cssText = 'position:absolute;z-index:9999';
  1346. 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';
  1347. }
  1348. indicator.className = 'iScrollIndicator';
  1349. if ( direction == 'h' ) {
  1350. if ( type === true ) {
  1351. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1352. indicator.style.height = '100%';
  1353. }
  1354. scrollbar.className = 'iScrollHorizontalScrollbar';
  1355. } else {
  1356. if ( type === true ) {
  1357. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1358. indicator.style.width = '100%';
  1359. }
  1360. scrollbar.className = 'iScrollVerticalScrollbar';
  1361. }
  1362. scrollbar.style.cssText += ';overflow:hidden';
  1363. if ( !interactive ) {
  1364. scrollbar.style.pointerEvents = 'none';
  1365. }
  1366. scrollbar.appendChild(indicator);
  1367. return scrollbar;
  1368. }
  1369. function Indicator (scroller, options) {
  1370. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  1371. this.wrapperStyle = this.wrapper.style;
  1372. this.indicator = this.wrapper.children[0];
  1373. this.indicatorStyle = this.indicator.style;
  1374. this.scroller = scroller;
  1375. this.options = {
  1376. listenX: true,
  1377. listenY: true,
  1378. interactive: false,
  1379. resize: true,
  1380. defaultScrollbars: false,
  1381. shrink: false,
  1382. fade: false,
  1383. speedRatioX: 0,
  1384. speedRatioY: 0
  1385. };
  1386. for ( var i in options ) {
  1387. this.options[i] = options[i];
  1388. }
  1389. this.sizeRatioX = 1;
  1390. this.sizeRatioY = 1;
  1391. this.maxPosX = 0;
  1392. this.maxPosY = 0;
  1393. if ( this.options.interactive ) {
  1394. if ( !this.options.disableTouch ) {
  1395. utils.addEvent(this.indicator, 'touchstart', this);
  1396. utils.addEvent(window, 'touchend', this);
  1397. }
  1398. if ( !this.options.disablePointer ) {
  1399. utils.addEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1400. utils.addEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1401. }
  1402. if ( !this.options.disableMouse ) {
  1403. utils.addEvent(this.indicator, 'mousedown', this);
  1404. utils.addEvent(window, 'mouseup', this);
  1405. }
  1406. }
  1407. if ( this.options.fade ) {
  1408. this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
  1409. var durationProp = utils.style.transitionDuration;
  1410. if(!durationProp) {
  1411. return;
  1412. }
  1413. this.wrapperStyle[durationProp] = utils.isBadAndroid ? '0.0001ms' : '0ms';
  1414. // remove 0.0001ms
  1415. var self = this;
  1416. if(utils.isBadAndroid) {
  1417. rAF(function() {
  1418. if(self.wrapperStyle[durationProp] === '0.0001ms') {
  1419. self.wrapperStyle[durationProp] = '0s';
  1420. }
  1421. });
  1422. }
  1423. this.wrapperStyle.opacity = '0';
  1424. }
  1425. }
  1426. Indicator.prototype = {
  1427. handleEvent: function (e) {
  1428. switch ( e.type ) {
  1429. case 'touchstart':
  1430. case 'pointerdown':
  1431. case 'MSPointerDown':
  1432. case 'mousedown':
  1433. this._start(e);
  1434. break;
  1435. case 'touchmove':
  1436. case 'pointermove':
  1437. case 'MSPointerMove':
  1438. case 'mousemove':
  1439. this._move(e);
  1440. break;
  1441. case 'touchend':
  1442. case 'pointerup':
  1443. case 'MSPointerUp':
  1444. case 'mouseup':
  1445. case 'touchcancel':
  1446. case 'pointercancel':
  1447. case 'MSPointerCancel':
  1448. case 'mousecancel':
  1449. this._end(e);
  1450. break;
  1451. }
  1452. },
  1453. destroy: function () {
  1454. if ( this.options.fadeScrollbars ) {
  1455. clearTimeout(this.fadeTimeout);
  1456. this.fadeTimeout = null;
  1457. }
  1458. if ( this.options.interactive ) {
  1459. utils.removeEvent(this.indicator, 'touchstart', this);
  1460. utils.removeEvent(this.indicator, utils.prefixPointerEvent('pointerdown'), this);
  1461. utils.removeEvent(this.indicator, 'mousedown', this);
  1462. utils.removeEvent(window, 'touchmove', this);
  1463. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1464. utils.removeEvent(window, 'mousemove', this);
  1465. utils.removeEvent(window, 'touchend', this);
  1466. utils.removeEvent(window, utils.prefixPointerEvent('pointerup'), this);
  1467. utils.removeEvent(window, 'mouseup', this);
  1468. }
  1469. if ( this.options.defaultScrollbars ) {
  1470. this.wrapper.parentNode.removeChild(this.wrapper);
  1471. }
  1472. },
  1473. _start: function (e) {
  1474. var point = e.touches ? e.touches[0] : e;
  1475. e.preventDefault();
  1476. e.stopPropagation();
  1477. this.transitionTime();
  1478. this.initiated = true;
  1479. this.moved = false;
  1480. this.lastPointX = point.pageX;
  1481. this.lastPointY = point.pageY;
  1482. this.startTime = utils.getTime();
  1483. if ( !this.options.disableTouch ) {
  1484. utils.addEvent(window, 'touchmove', this);
  1485. }
  1486. if ( !this.options.disablePointer ) {
  1487. utils.addEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1488. }
  1489. if ( !this.options.disableMouse ) {
  1490. utils.addEvent(window, 'mousemove', this);
  1491. }
  1492. this.scroller._execEvent('beforeScrollStart');
  1493. },
  1494. _move: function (e) {
  1495. var point = e.touches ? e.touches[0] : e,
  1496. deltaX, deltaY,
  1497. newX, newY,
  1498. timestamp = utils.getTime();
  1499. if ( !this.moved ) {
  1500. this.scroller._execEvent('scrollStart');
  1501. }
  1502. this.moved = true;
  1503. deltaX = point.pageX - this.lastPointX;
  1504. this.lastPointX = point.pageX;
  1505. deltaY = point.pageY - this.lastPointY;
  1506. this.lastPointY = point.pageY;
  1507. newX = this.x + deltaX;
  1508. newY = this.y + deltaY;
  1509. this._pos(newX, newY);
  1510. // INSERT POINT: indicator._move
  1511. e.preventDefault();
  1512. e.stopPropagation();
  1513. },
  1514. _end: function (e) {
  1515. if ( !this.initiated ) {
  1516. return;
  1517. }
  1518. this.initiated = false;
  1519. e.preventDefault();
  1520. e.stopPropagation();
  1521. utils.removeEvent(window, 'touchmove', this);
  1522. utils.removeEvent(window, utils.prefixPointerEvent('pointermove'), this);
  1523. utils.removeEvent(window, 'mousemove', this);
  1524. if ( this.scroller.options.snap ) {
  1525. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  1526. var time = this.options.snapSpeed || Math.max(
  1527. Math.max(
  1528. Math.min(Math.abs(this.scroller.x - snap.x), 1000),
  1529. Math.min(Math.abs(this.scroller.y - snap.y), 1000)
  1530. ), 300);
  1531. if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
  1532. this.scroller.directionX = 0;
  1533. this.scroller.directionY = 0;
  1534. this.scroller.currentPage = snap;
  1535. this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
  1536. }
  1537. }
  1538. if ( this.moved ) {
  1539. this.scroller._execEvent('scrollEnd');
  1540. }
  1541. },
  1542. transitionTime: function (time) {
  1543. time = time || 0;
  1544. var durationProp = utils.style.transitionDuration;
  1545. if(!durationProp) {
  1546. return;
  1547. }
  1548. this.indicatorStyle[durationProp] = time + 'ms';
  1549. if ( !time && utils.isBadAndroid ) {
  1550. this.indicatorStyle[durationProp] = '0.0001ms';
  1551. // remove 0.0001ms
  1552. var self = this;
  1553. rAF(function() {
  1554. if(self.indicatorStyle[durationProp] === '0.0001ms') {
  1555. self.indicatorStyle[durationProp] = '0s';
  1556. }
  1557. });
  1558. }
  1559. },
  1560. transitionTimingFunction: function (easing) {
  1561. this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
  1562. },
  1563. refresh: function () {
  1564. this.transitionTime();
  1565. if ( this.options.listenX && !this.options.listenY ) {
  1566. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  1567. } else if ( this.options.listenY && !this.options.listenX ) {
  1568. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  1569. } else {
  1570. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  1571. }
  1572. if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
  1573. utils.addClass(this.wrapper, 'iScrollBothScrollbars');
  1574. utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
  1575. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1576. if ( this.options.listenX ) {
  1577. this.wrapper.style.right = '8px';
  1578. } else {
  1579. this.wrapper.style.bottom = '8px';
  1580. }
  1581. }
  1582. } else {
  1583. utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
  1584. utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
  1585. if ( this.options.defaultScrollbars && this.options.customStyle ) {
  1586. if ( this.options.listenX ) {
  1587. this.wrapper.style.right = '2px';
  1588. } else {
  1589. this.wrapper.style.bottom = '2px';
  1590. }
  1591. }
  1592. }
  1593. var r = this.wrapper.offsetHeight; // force refresh
  1594. if ( this.options.listenX ) {
  1595. this.wrapperWidth = this.wrapper.clientWidth;
  1596. if ( this.options.resize ) {
  1597. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
  1598. this.indicatorStyle.width = this.indicatorWidth + 'px';
  1599. } else {
  1600. this.indicatorWidth = this.indicator.clientWidth;
  1601. }
  1602. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  1603. if ( this.options.shrink == 'clip' ) {
  1604. this.minBoundaryX = -this.indicatorWidth + 8;
  1605. this.maxBoundaryX = this.wrapperWidth - 8;
  1606. } else {
  1607. this.minBoundaryX = 0;
  1608. this.maxBoundaryX = this.maxPosX;
  1609. }
  1610. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  1611. }
  1612. if ( this.options.listenY ) {
  1613. this.wrapperHeight = this.wrapper.clientHeight;
  1614. if ( this.options.resize ) {
  1615. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
  1616. this.indicatorStyle.height = this.indicatorHeight + 'px';
  1617. } else {
  1618. this.indicatorHeight = this.indicator.clientHeight;
  1619. }
  1620. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1621. if ( this.options.shrink == 'clip' ) {
  1622. this.minBoundaryY = -this.indicatorHeight + 8;
  1623. this.maxBoundaryY = this.wrapperHeight - 8;
  1624. } else {
  1625. this.minBoundaryY = 0;
  1626. this.maxBoundaryY = this.maxPosY;
  1627. }
  1628. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  1629. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
  1630. }
  1631. this.updatePosition();
  1632. },
  1633. updatePosition: function () {
  1634. var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  1635. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
  1636. if ( !this.options.ignoreBoundaries ) {
  1637. if ( x < this.minBoundaryX ) {
  1638. if ( this.options.shrink == 'scale' ) {
  1639. this.width = Math.max(this.indicatorWidth + x, 8);
  1640. this.indicatorStyle.width = this.width + 'px';
  1641. }
  1642. x = this.minBoundaryX;
  1643. } else if ( x > this.maxBoundaryX ) {
  1644. if ( this.options.shrink == 'scale' ) {
  1645. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
  1646. this.indicatorStyle.width = this.width + 'px';
  1647. x = this.maxPosX + this.indicatorWidth - this.width;
  1648. } else {
  1649. x = this.maxBoundaryX;
  1650. }
  1651. } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
  1652. this.width = this.indicatorWidth;
  1653. this.indicatorStyle.width = this.width + 'px';
  1654. }
  1655. if ( y < this.minBoundaryY ) {
  1656. if ( this.options.shrink == 'scale' ) {
  1657. this.height = Math.max(this.indicatorHeight + y * 3, 8);
  1658. this.indicatorStyle.height = this.height + 'px';
  1659. }
  1660. y = this.minBoundaryY;
  1661. } else if ( y > this.maxBoundaryY ) {
  1662. if ( this.options.shrink == 'scale' ) {
  1663. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
  1664. this.indicatorStyle.height = this.height + 'px';
  1665. y = this.maxPosY + this.indicatorHeight - this.height;
  1666. } else {
  1667. y = this.maxBoundaryY;
  1668. }
  1669. } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
  1670. this.height = this.indicatorHeight;
  1671. this.indicatorStyle.height = this.height + 'px';
  1672. }
  1673. }
  1674. this.x = x;
  1675. this.y = y;
  1676. if ( this.scroller.options.useTransform ) {
  1677. this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
  1678. } else {
  1679. this.indicatorStyle.left = x + 'px';
  1680. this.indicatorStyle.top = y + 'px';
  1681. }
  1682. },
  1683. _pos: function (x, y) {
  1684. if ( x < 0 ) {
  1685. x = 0;
  1686. } else if ( x > this.maxPosX ) {
  1687. x = this.maxPosX;
  1688. }
  1689. if ( y < 0 ) {
  1690. y = 0;
  1691. } else if ( y > this.maxPosY ) {
  1692. y = this.maxPosY;
  1693. }
  1694. x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
  1695. y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
  1696. this.scroller.scrollTo(x, y);
  1697. },
  1698. fade: function (val, hold) {
  1699. if ( hold && !this.visible ) {
  1700. return;
  1701. }
  1702. clearTimeout(this.fadeTimeout);
  1703. this.fadeTimeout = null;
  1704. var time = val ? 250 : 500,
  1705. delay = val ? 0 : 300;
  1706. val = val ? '1' : '0';
  1707. this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
  1708. this.fadeTimeout = setTimeout((function (val) {
  1709. this.wrapperStyle.opacity = val;
  1710. this.visible = +val;
  1711. }).bind(this, val), delay);
  1712. }
  1713. };
  1714. IScroll.utils = utils;
  1715. if ( typeof module != 'undefined' && module.exports ) {
  1716. module.exports = IScroll;
  1717. } else if ( typeof define == 'function' && define.amd ) {
  1718. define( function () { return IScroll; } );
  1719. } else {
  1720. window.IScroll = IScroll;
  1721. }
  1722. })(window, document, Math);