iscroll.js 49 KB

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