iscroll-probe.js 50 KB

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