iscroll.js 49 KB

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