bscroll.esm.js 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464
  1. /*!
  2. * better-normal-scroll v1.15.2
  3. * (c) 2016-2019 ustbhuangyi
  4. * Released under the MIT License.
  5. */
  6. // As of V8 6.6, depending on the size of the array, this is anywhere
  7. // between 1.5-10x faster than the two-arg version of Array#splice()
  8. function spliceOne(list, index) {
  9. for (; index + 1 < list.length; index++) {
  10. list[index] = list[index + 1];
  11. }
  12. list.pop();
  13. }
  14. var slicedToArray = function () {
  15. function sliceIterator(arr, i) {
  16. var _arr = [];
  17. var _n = true;
  18. var _d = false;
  19. var _e = undefined;
  20. try {
  21. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  22. _arr.push(_s.value);
  23. if (i && _arr.length === i) break;
  24. }
  25. } catch (err) {
  26. _d = true;
  27. _e = err;
  28. } finally {
  29. try {
  30. if (!_n && _i["return"]) _i["return"]();
  31. } finally {
  32. if (_d) throw _e;
  33. }
  34. }
  35. return _arr;
  36. }
  37. return function (arr, i) {
  38. if (Array.isArray(arr)) {
  39. return arr;
  40. } else if (Symbol.iterator in Object(arr)) {
  41. return sliceIterator(arr, i);
  42. } else {
  43. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  44. }
  45. };
  46. }();
  47. var toConsumableArray = function (arr) {
  48. if (Array.isArray(arr)) {
  49. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  50. return arr2;
  51. } else {
  52. return Array.from(arr);
  53. }
  54. };
  55. function eventMixin(BScroll) {
  56. BScroll.prototype.on = function (type, fn) {
  57. var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this;
  58. if (!this._events[type]) {
  59. this._events[type] = [];
  60. }
  61. this._events[type].push([fn, context]);
  62. };
  63. BScroll.prototype.once = function (type, fn) {
  64. var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this;
  65. function magic() {
  66. this.off(type, magic);
  67. fn.apply(context, arguments);
  68. }
  69. // To expose the corresponding function method in order to execute the off method
  70. magic.fn = fn;
  71. this.on(type, magic);
  72. };
  73. BScroll.prototype.off = function (type, fn) {
  74. var _events = this._events[type];
  75. if (!_events) {
  76. return;
  77. }
  78. var count = _events.length;
  79. while (count--) {
  80. if (_events[count][0] === fn || _events[count][0] && _events[count][0].fn === fn) {
  81. spliceOne(_events, count);
  82. }
  83. }
  84. };
  85. BScroll.prototype.trigger = function (type) {
  86. var events = this._events[type];
  87. if (!events) {
  88. return;
  89. }
  90. var len = events.length;
  91. var eventsCopy = [].concat(toConsumableArray(events));
  92. for (var i = 0; i < len; i++) {
  93. var event = eventsCopy[i];
  94. var _event = slicedToArray(event, 2),
  95. fn = _event[0],
  96. context = _event[1];
  97. if (fn) {
  98. fn.apply(context, [].slice.call(arguments, 1));
  99. }
  100. }
  101. };
  102. }
  103. // ssr support
  104. var inBrowser = typeof window !== 'undefined';
  105. var ua = inBrowser && navigator.userAgent.toLowerCase();
  106. var isWeChatDevTools = ua && /wechatdevtools/.test(ua);
  107. var isAndroid = ua && ua.indexOf('android') > 0;
  108. function getNow() {
  109. return window.performance && window.performance.now ? window.performance.now() + window.performance.timing.navigationStart : +new Date();
  110. }
  111. function extend(target) {
  112. for (var _len = arguments.length, rest = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  113. rest[_key - 1] = arguments[_key];
  114. }
  115. for (var i = 0; i < rest.length; i++) {
  116. var source = rest[i];
  117. for (var key in source) {
  118. target[key] = source[key];
  119. }
  120. }
  121. return target;
  122. }
  123. function isUndef(v) {
  124. return v === undefined || v === null;
  125. }
  126. function getDistance(x, y) {
  127. return Math.sqrt(x * x + y * y);
  128. }
  129. var elementStyle = inBrowser && document.createElement('div').style;
  130. var vendor = function () {
  131. if (!inBrowser) {
  132. return false;
  133. }
  134. // first pick up standard to fix #743
  135. var transformNames = {
  136. standard: 'transform',
  137. webkit: 'webkitTransform',
  138. Moz: 'MozTransform',
  139. O: 'OTransform',
  140. ms: 'msTransform'
  141. };
  142. for (var key in transformNames) {
  143. if (elementStyle[transformNames[key]] !== undefined) {
  144. return key;
  145. }
  146. }
  147. return false;
  148. }();
  149. function prefixStyle(style) {
  150. if (vendor === false) {
  151. return false;
  152. }
  153. if (vendor === 'standard') {
  154. if (style === 'transitionEnd') {
  155. return 'transitionend';
  156. }
  157. return style;
  158. }
  159. return vendor + style.charAt(0).toUpperCase() + style.substr(1);
  160. }
  161. function addEvent(el, type, fn, capture) {
  162. el.addEventListener(type, fn, { passive: false, capture: !!capture });
  163. }
  164. function removeEvent(el, type, fn, capture) {
  165. el.removeEventListener(type, fn, { passive: false, capture: !!capture });
  166. }
  167. function offset(el) {
  168. var left = 0;
  169. var top = 0;
  170. while (el) {
  171. left -= el.offsetLeft;
  172. top -= el.offsetTop;
  173. el = el.offsetParent;
  174. }
  175. return {
  176. left: left,
  177. top: top
  178. };
  179. }
  180. function offsetToBody(el) {
  181. var rect = el.getBoundingClientRect();
  182. return {
  183. left: -(rect.left + window.pageXOffset),
  184. top: -(rect.top + window.pageYOffset)
  185. };
  186. }
  187. var cssVendor = vendor && vendor !== 'standard' ? '-' + vendor.toLowerCase() + '-' : '';
  188. var transform = prefixStyle('transform');
  189. var transition = prefixStyle('transition');
  190. var hasPerspective = inBrowser && prefixStyle('perspective') in elementStyle;
  191. // fix issue #361
  192. var hasTouch = inBrowser && ('ontouchstart' in window || isWeChatDevTools);
  193. var hasTransform = transform !== false;
  194. var hasTransition = inBrowser && transition in elementStyle;
  195. var style = {
  196. transform: transform,
  197. transition: transition,
  198. transitionTimingFunction: prefixStyle('transitionTimingFunction'),
  199. transitionDuration: prefixStyle('transitionDuration'),
  200. transitionDelay: prefixStyle('transitionDelay'),
  201. transformOrigin: prefixStyle('transformOrigin'),
  202. transitionEnd: prefixStyle('transitionEnd')
  203. };
  204. var TOUCH_EVENT = 1;
  205. var MOUSE_EVENT = 2;
  206. var eventType = {
  207. touchstart: TOUCH_EVENT,
  208. touchmove: TOUCH_EVENT,
  209. touchend: TOUCH_EVENT,
  210. mousedown: MOUSE_EVENT,
  211. mousemove: MOUSE_EVENT,
  212. mouseup: MOUSE_EVENT
  213. };
  214. function getRect(el) {
  215. if (el instanceof window.SVGElement) {
  216. var rect = el.getBoundingClientRect();
  217. return {
  218. top: rect.top,
  219. left: rect.left,
  220. width: rect.width,
  221. height: rect.height
  222. };
  223. } else {
  224. return {
  225. top: el.offsetTop,
  226. left: el.offsetLeft,
  227. width: el.offsetWidth,
  228. height: el.offsetHeight
  229. };
  230. }
  231. }
  232. function preventDefaultException(el, exceptions) {
  233. for (var i in exceptions) {
  234. if (exceptions[i].test(el[i])) {
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. function tap(e, eventName) {
  241. var ev = document.createEvent('Event');
  242. ev.initEvent(eventName, true, true);
  243. ev.pageX = e.pageX;
  244. ev.pageY = e.pageY;
  245. e.target.dispatchEvent(ev);
  246. }
  247. function click(e) {
  248. var event = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'click';
  249. var eventSource = void 0;
  250. if (e.type === 'mouseup' || e.type === 'mousecancel') {
  251. eventSource = e;
  252. } else if (e.type === 'touchend' || e.type === 'touchcancel') {
  253. eventSource = e.changedTouches[0];
  254. }
  255. var posSrc = {};
  256. if (eventSource) {
  257. posSrc.screenX = eventSource.screenX || 0;
  258. posSrc.screenY = eventSource.screenY || 0;
  259. posSrc.clientX = eventSource.clientX || 0;
  260. posSrc.clientY = eventSource.clientY || 0;
  261. }
  262. var ev = void 0;
  263. var bubbles = true;
  264. var cancelable = true;
  265. if (typeof MouseEvent !== 'undefined') {
  266. try {
  267. ev = new MouseEvent(event, extend({
  268. bubbles: bubbles,
  269. cancelable: cancelable
  270. }, posSrc));
  271. } catch (e) {
  272. createEvent();
  273. }
  274. } else {
  275. createEvent();
  276. }
  277. function createEvent() {
  278. ev = document.createEvent('Event');
  279. ev.initEvent(event, bubbles, cancelable);
  280. extend(ev, posSrc);
  281. }
  282. // forwardedTouchEvent set to true in case of the conflict with fastclick
  283. ev.forwardedTouchEvent = true;
  284. ev._constructed = true;
  285. e.target.dispatchEvent(ev);
  286. }
  287. function dblclick(e) {
  288. click(e, 'dblclick');
  289. }
  290. function prepend(el, target) {
  291. if (target.firstChild) {
  292. before(el, target.firstChild);
  293. } else {
  294. target.appendChild(el);
  295. }
  296. }
  297. function before(el, target) {
  298. target.parentNode.insertBefore(el, target);
  299. }
  300. function removeChild(el, child) {
  301. el.removeChild(child);
  302. }
  303. var DEFAULT_OPTIONS = {
  304. startX: 0,
  305. startY: 0,
  306. scrollX: false,
  307. scrollY: true,
  308. freeScroll: false,
  309. directionLockThreshold: 5,
  310. eventPassthrough: '',
  311. click: false,
  312. tap: false,
  313. /**
  314. * support any side
  315. * bounce: {
  316. * top: true,
  317. * bottom: true,
  318. * left: true,
  319. * right: true
  320. * }
  321. */
  322. bounce: true,
  323. bounceTime: 800,
  324. momentum: true,
  325. momentumLimitTime: 300,
  326. momentumLimitDistance: 15,
  327. swipeTime: 2500,
  328. swipeBounceTime: 500,
  329. deceleration: 0.0015,
  330. flickLimitTime: 200,
  331. flickLimitDistance: 100,
  332. resizePolling: 60,
  333. probeType: 0,
  334. preventDefault: true,
  335. preventDefaultException: {
  336. tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT|AUDIO)$/
  337. },
  338. HWCompositing: true,
  339. useTransition: true,
  340. useTransform: true,
  341. bindToWrapper: false,
  342. disableMouse: hasTouch,
  343. disableTouch: !hasTouch,
  344. observeDOM: true,
  345. autoBlur: true,
  346. /**
  347. * for picker
  348. * wheel: {
  349. * selectedIndex: 0,
  350. * rotate: 25,
  351. * adjustTime: 400
  352. * wheelWrapperClass: 'wheel-scroll',
  353. * wheelItemClass: 'wheel-item'
  354. * }
  355. */
  356. wheel: false,
  357. /**
  358. * for slide
  359. * snap: {
  360. * loop: false,
  361. * el: domEl,
  362. * threshold: 0.1,
  363. * stepX: 100,
  364. * stepY: 100,
  365. * speed: 400,
  366. * easing: {
  367. * style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  368. * fn: function (t) {
  369. * return t * (2 - t)
  370. * }
  371. * }
  372. * listenFlick: true
  373. * }
  374. */
  375. snap: false,
  376. /**
  377. * for scrollbar
  378. * scrollbar: {
  379. * fade: true,
  380. * interactive: false
  381. * }
  382. */
  383. scrollbar: false,
  384. /**
  385. * for pull down and refresh
  386. * pullDownRefresh: {
  387. * threshold: 50,
  388. * stop: 20
  389. * }
  390. */
  391. pullDownRefresh: false,
  392. /**
  393. * for pull up and load
  394. * pullUpLoad: {
  395. * threshold: 50
  396. * }
  397. */
  398. pullUpLoad: false,
  399. /**
  400. * for mouse wheel
  401. * mouseWheel: {
  402. * speed: 20,
  403. * invert: false,
  404. * easeTime: 300
  405. * }
  406. */
  407. mouseWheel: false,
  408. stopPropagation: false,
  409. /**
  410. * for zoom
  411. * zoom: {
  412. * start: 1,
  413. * min: 1,
  414. * max: 4
  415. * }
  416. */
  417. zoom: false,
  418. /**
  419. * for infinity
  420. * infinity: {
  421. * render(item, div) {
  422. * },
  423. * createTombstone() {
  424. * },
  425. * fetch(count) {
  426. * }
  427. * }
  428. */
  429. infinity: false,
  430. /**
  431. * for double click
  432. * dblclick: {
  433. * delay: 300
  434. * }
  435. */
  436. dblclick: false
  437. };
  438. function initMixin(BScroll) {
  439. BScroll.prototype._init = function (options) {
  440. this._handleOptions(options);
  441. // init private custom events
  442. this._events = {};
  443. this.x = 0;
  444. this.y = 0;
  445. this.directionX = 0;
  446. this.directionY = 0;
  447. this.setScale(1);
  448. this._addDOMEvents();
  449. this._initExtFeatures();
  450. this._watchTransition();
  451. if (this.options.observeDOM) {
  452. this._initDOMObserver();
  453. }
  454. if (this.options.autoBlur) {
  455. this._handleAutoBlur();
  456. }
  457. this.refresh();
  458. if (!this.options.snap) {
  459. this.scrollTo(this.options.startX, this.options.startY);
  460. }
  461. this.enable();
  462. };
  463. BScroll.prototype.setScale = function (scale) {
  464. this.lastScale = isUndef(this.scale) ? scale : this.scale;
  465. this.scale = scale;
  466. };
  467. BScroll.prototype._handleOptions = function (options) {
  468. this.options = extend({}, DEFAULT_OPTIONS, options);
  469. this.translateZ = this.options.HWCompositing && hasPerspective ? ' translateZ(0)' : '';
  470. this.options.useTransition = this.options.useTransition && hasTransition;
  471. this.options.useTransform = this.options.useTransform && hasTransform;
  472. this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
  473. // If you want eventPassthrough I have to lock one of the axes
  474. this.options.scrollX = this.options.eventPassthrough === 'horizontal' ? false : this.options.scrollX;
  475. this.options.scrollY = this.options.eventPassthrough === 'vertical' ? false : this.options.scrollY;
  476. // With eventPassthrough we also need lockDirection mechanism
  477. this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
  478. this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
  479. if (this.options.tap === true) {
  480. this.options.tap = 'tap';
  481. }
  482. };
  483. BScroll.prototype._addDOMEvents = function () {
  484. var eventOperation = addEvent;
  485. this._handleDOMEvents(eventOperation);
  486. };
  487. BScroll.prototype._removeDOMEvents = function () {
  488. var eventOperation = removeEvent;
  489. this._handleDOMEvents(eventOperation);
  490. };
  491. BScroll.prototype._handleDOMEvents = function (eventOperation) {
  492. var target = this.options.bindToWrapper ? this.wrapper : window;
  493. eventOperation(window, 'orientationchange', this);
  494. eventOperation(window, 'resize', this);
  495. if (this.options.click) {
  496. eventOperation(this.wrapper, 'click', this, true);
  497. }
  498. if (!this.options.disableMouse) {
  499. eventOperation(this.wrapper, 'mousedown', this);
  500. eventOperation(target, 'mousemove', this);
  501. eventOperation(target, 'mousecancel', this);
  502. eventOperation(target, 'mouseup', this);
  503. }
  504. if (hasTouch && !this.options.disableTouch) {
  505. eventOperation(this.wrapper, 'touchstart', this);
  506. eventOperation(target, 'touchmove', this);
  507. eventOperation(target, 'touchcancel', this);
  508. eventOperation(target, 'touchend', this);
  509. }
  510. eventOperation(this.scroller, style.transitionEnd, this);
  511. };
  512. BScroll.prototype._initExtFeatures = function () {
  513. if (this.options.snap) {
  514. this._initSnap();
  515. }
  516. if (this.options.scrollbar) {
  517. this._initScrollbar();
  518. }
  519. if (this.options.pullUpLoad) {
  520. this._initPullUp();
  521. }
  522. if (this.options.pullDownRefresh) {
  523. this._initPullDown();
  524. }
  525. if (this.options.wheel) {
  526. this._initWheel();
  527. }
  528. if (this.options.mouseWheel) {
  529. this._initMouseWheel();
  530. }
  531. if (this.options.zoom) {
  532. this._initZoom();
  533. }
  534. if (this.options.infinity) {
  535. this._initInfinite();
  536. }
  537. };
  538. BScroll.prototype._watchTransition = function () {
  539. if (typeof Object.defineProperty !== 'function') {
  540. return;
  541. }
  542. var me = this;
  543. var isInTransition = false;
  544. var key = this.options.useTransition ? 'isInTransition' : 'isAnimating';
  545. Object.defineProperty(this, key, {
  546. get: function get() {
  547. return isInTransition;
  548. },
  549. set: function set(newVal) {
  550. isInTransition = newVal;
  551. // fix issue #359
  552. var el = me.scroller.children.length ? me.scroller.children : [me.scroller];
  553. var pointerEvents = isInTransition && !me.pulling ? 'none' : 'auto';
  554. for (var i = 0; i < el.length; i++) {
  555. el[i].style.pointerEvents = pointerEvents;
  556. }
  557. }
  558. });
  559. };
  560. BScroll.prototype._handleAutoBlur = function () {
  561. this.on('scrollStart', function () {
  562. var activeElement = document.activeElement;
  563. if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
  564. activeElement.blur();
  565. }
  566. });
  567. };
  568. BScroll.prototype._initDOMObserver = function () {
  569. var _this = this;
  570. if (typeof MutationObserver !== 'undefined') {
  571. var timer = void 0;
  572. var observer = new MutationObserver(function (mutations) {
  573. // don't do any refresh during the transition, or outside of the boundaries
  574. if (_this._shouldNotRefresh()) {
  575. return;
  576. }
  577. var immediateRefresh = false;
  578. var deferredRefresh = false;
  579. for (var i = 0; i < mutations.length; i++) {
  580. var mutation = mutations[i];
  581. if (mutation.type !== 'attributes') {
  582. immediateRefresh = true;
  583. break;
  584. } else {
  585. if (mutation.target !== _this.scroller) {
  586. deferredRefresh = true;
  587. break;
  588. }
  589. }
  590. }
  591. if (immediateRefresh) {
  592. _this.refresh();
  593. } else if (deferredRefresh) {
  594. // attributes changes too often
  595. clearTimeout(timer);
  596. timer = setTimeout(function () {
  597. if (!_this._shouldNotRefresh()) {
  598. _this.refresh();
  599. }
  600. }, 60);
  601. }
  602. });
  603. var config = {
  604. attributes: true,
  605. childList: true,
  606. subtree: true
  607. };
  608. observer.observe(this.scroller, config);
  609. this.on('destroy', function () {
  610. observer.disconnect();
  611. });
  612. } else {
  613. this._checkDOMUpdate();
  614. }
  615. };
  616. BScroll.prototype._shouldNotRefresh = function () {
  617. var outsideBoundaries = this.x > this.minScrollX || this.x < this.maxScrollX || this.y > this.minScrollY || this.y < this.maxScrollY;
  618. return this.isInTransition || this.stopFromTransition || outsideBoundaries;
  619. };
  620. BScroll.prototype._checkDOMUpdate = function () {
  621. var scrollerRect = getRect(this.scroller);
  622. var oldWidth = scrollerRect.width;
  623. var oldHeight = scrollerRect.height;
  624. function check() {
  625. if (this.destroyed) {
  626. return;
  627. }
  628. scrollerRect = getRect(this.scroller);
  629. var newWidth = scrollerRect.width;
  630. var newHeight = scrollerRect.height;
  631. if (oldWidth !== newWidth || oldHeight !== newHeight) {
  632. this.refresh();
  633. }
  634. oldWidth = newWidth;
  635. oldHeight = newHeight;
  636. next.call(this);
  637. }
  638. function next() {
  639. var _this2 = this;
  640. setTimeout(function () {
  641. check.call(_this2);
  642. }, 1000);
  643. }
  644. next.call(this);
  645. };
  646. BScroll.prototype.handleEvent = function (e) {
  647. switch (e.type) {
  648. case 'touchstart':
  649. case 'mousedown':
  650. this._start(e);
  651. if (this.options.zoom && e.touches && e.touches.length > 1) {
  652. this._zoomStart(e);
  653. }
  654. break;
  655. case 'touchmove':
  656. case 'mousemove':
  657. if (this.options.zoom && e.touches && e.touches.length > 1) {
  658. this._zoom(e);
  659. } else {
  660. this._move(e);
  661. }
  662. break;
  663. case 'touchend':
  664. case 'mouseup':
  665. case 'touchcancel':
  666. case 'mousecancel':
  667. if (this.scaled) {
  668. this._zoomEnd(e);
  669. } else {
  670. this._end(e);
  671. }
  672. break;
  673. case 'orientationchange':
  674. case 'resize':
  675. this._resize();
  676. break;
  677. case 'transitionend':
  678. case 'webkitTransitionEnd':
  679. case 'oTransitionEnd':
  680. case 'MSTransitionEnd':
  681. this._transitionEnd(e);
  682. break;
  683. case 'click':
  684. if (this.enabled && !e._constructed) {
  685. if (!preventDefaultException(e.target, this.options.preventDefaultException)) {
  686. e.preventDefault();
  687. e.stopPropagation();
  688. }
  689. }
  690. break;
  691. case 'wheel':
  692. case 'DOMMouseScroll':
  693. case 'mousewheel':
  694. this._onMouseWheel(e);
  695. break;
  696. }
  697. };
  698. BScroll.prototype.refresh = function () {
  699. var isWrapperStatic = window.getComputedStyle(this.wrapper, null).position === 'static';
  700. var wrapperRect = getRect(this.wrapper);
  701. this.wrapperWidth = wrapperRect.width;
  702. this.wrapperHeight = wrapperRect.height;
  703. var scrollerRect = getRect(this.scroller);
  704. this.scrollerWidth = Math.round(scrollerRect.width * this.scale);
  705. this.scrollerHeight = Math.round(scrollerRect.height * this.scale);
  706. this.relativeX = scrollerRect.left;
  707. this.relativeY = scrollerRect.top;
  708. if (isWrapperStatic) {
  709. this.relativeX -= wrapperRect.left;
  710. this.relativeY -= wrapperRect.top;
  711. }
  712. this.minScrollX = 0;
  713. this.minScrollY = 0;
  714. var wheel = this.options.wheel;
  715. if (wheel) {
  716. this.items = this.scroller.children;
  717. // check whether there are all disable items or not when refresh
  718. this._checkWheelAllDisabled();
  719. this.options.itemHeight = this.itemHeight = this.items.length ? this.scrollerHeight / this.items.length : 0;
  720. if (this.selectedIndex === undefined) {
  721. this.selectedIndex = wheel.selectedIndex || 0;
  722. }
  723. this.options.startY = -this.selectedIndex * this.itemHeight;
  724. this.maxScrollX = 0;
  725. this.maxScrollY = -this.itemHeight * (this.items.length - 1);
  726. } else {
  727. this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
  728. if (!this.options.infinity) {
  729. this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
  730. }
  731. if (this.maxScrollX < 0) {
  732. this.maxScrollX -= this.relativeX;
  733. this.minScrollX = -this.relativeX;
  734. } else if (this.scale > 1) {
  735. this.maxScrollX = this.maxScrollX / 2 - this.relativeX;
  736. this.minScrollX = this.maxScrollX;
  737. }
  738. if (this.maxScrollY < 0) {
  739. this.maxScrollY -= this.relativeY;
  740. this.minScrollY = -this.relativeY;
  741. } else if (this.scale > 1) {
  742. this.maxScrollY = this.maxScrollY / 2 - this.relativeY;
  743. this.minScrollY = this.maxScrollY;
  744. }
  745. }
  746. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < this.minScrollX;
  747. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < this.minScrollY;
  748. if (!this.hasHorizontalScroll) {
  749. this.maxScrollX = this.minScrollX;
  750. this.scrollerWidth = this.wrapperWidth;
  751. }
  752. if (!this.hasVerticalScroll) {
  753. this.maxScrollY = this.minScrollY;
  754. this.scrollerHeight = this.wrapperHeight;
  755. }
  756. this.endTime = 0;
  757. this.directionX = 0;
  758. this.directionY = 0;
  759. this.wrapperOffset = offset(this.wrapper);
  760. this.trigger('refresh');
  761. !this.scaled && this.resetPosition();
  762. };
  763. BScroll.prototype.enable = function () {
  764. this.enabled = true;
  765. };
  766. BScroll.prototype.disable = function () {
  767. this.enabled = false;
  768. };
  769. }
  770. var ease = {
  771. // easeOutQuint
  772. swipe: {
  773. style: 'cubic-bezier(0.23, 1, 0.32, 1)',
  774. fn: function fn(t) {
  775. return 1 + --t * t * t * t * t;
  776. }
  777. },
  778. // easeOutQuard
  779. swipeBounce: {
  780. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  781. fn: function fn(t) {
  782. return t * (2 - t);
  783. }
  784. },
  785. // easeOutQuart
  786. bounce: {
  787. style: 'cubic-bezier(0.165, 0.84, 0.44, 1)',
  788. fn: function fn(t) {
  789. return 1 - --t * t * t * t;
  790. }
  791. }
  792. };
  793. function momentum(current, start, time, lowerMargin, upperMargin, wrapperSize, options, scroll) {
  794. var distance = current - start;
  795. var speed = Math.abs(distance) / time;
  796. var deceleration = options.deceleration,
  797. itemHeight = options.itemHeight,
  798. swipeBounceTime = options.swipeBounceTime,
  799. wheel = options.wheel,
  800. swipeTime = options.swipeTime;
  801. var duration = swipeTime;
  802. var rate = wheel ? 4 : 15;
  803. var destination = current + speed / deceleration * (distance < 0 ? -1 : 1);
  804. if (wheel && itemHeight) {
  805. destination = scroll._findNearestValidWheel(destination).y;
  806. }
  807. if (destination < lowerMargin) {
  808. destination = wrapperSize ? Math.max(lowerMargin - wrapperSize / 4, lowerMargin - wrapperSize / rate * speed) : lowerMargin;
  809. duration = swipeBounceTime;
  810. } else if (destination > upperMargin) {
  811. destination = wrapperSize ? Math.min(upperMargin + wrapperSize / 4, upperMargin + wrapperSize / rate * speed) : upperMargin;
  812. duration = swipeBounceTime;
  813. }
  814. return {
  815. destination: Math.round(destination),
  816. duration: duration
  817. };
  818. }
  819. var DEFAULT_INTERVAL = 100 / 60;
  820. function noop() {}
  821. var requestAnimationFrame = function () {
  822. if (!inBrowser) {
  823. /* istanbul ignore if */
  824. return noop;
  825. }
  826. return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame ||
  827. // if all else fails, use setTimeout
  828. function (callback) {
  829. return window.setTimeout(callback, (callback.interval || DEFAULT_INTERVAL) / 2); // make interval as precise as possible.
  830. };
  831. }();
  832. var cancelAnimationFrame = function () {
  833. if (!inBrowser) {
  834. /* istanbul ignore if */
  835. return noop;
  836. }
  837. return window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || function (id) {
  838. window.clearTimeout(id);
  839. };
  840. }();
  841. var DIRECTION_UP = 1;
  842. var DIRECTION_DOWN = -1;
  843. var DIRECTION_LEFT = 1;
  844. var DIRECTION_RIGHT = -1;
  845. var PROBE_DEBOUNCE = 1;
  846. var PROBE_REALTIME = 3;
  847. function warn(msg) {
  848. console.error('[BScroll warn]: ' + msg);
  849. }
  850. function assert(condition, msg) {
  851. if (!condition) {
  852. throw new Error('[BScroll] ' + msg);
  853. }
  854. }
  855. function coreMixin(BScroll) {
  856. BScroll.prototype._start = function (e) {
  857. var _eventType = eventType[e.type];
  858. if (_eventType !== TOUCH_EVENT) {
  859. if (e.button !== 0) {
  860. return;
  861. }
  862. }
  863. if (!this.enabled || this.destroyed || this.initiated && this.initiated !== _eventType) {
  864. return;
  865. }
  866. this.initiated = _eventType;
  867. if (this.options.preventDefault && !preventDefaultException(e.target, this.options.preventDefaultException)) {
  868. e.preventDefault();
  869. }
  870. if (this.options.stopPropagation) {
  871. e.stopPropagation();
  872. }
  873. this.moved = false;
  874. this.distX = 0;
  875. this.distY = 0;
  876. this.directionX = 0;
  877. this.directionY = 0;
  878. this.movingDirectionX = 0;
  879. this.movingDirectionY = 0;
  880. this.directionLocked = 0;
  881. this._transitionTime();
  882. this.startTime = getNow();
  883. if (this.options.wheel) {
  884. this.target = e.target;
  885. }
  886. this.stop();
  887. var point = e.touches ? e.touches[0] : e;
  888. this.startX = this.x;
  889. this.startY = this.y;
  890. this.absStartX = this.x;
  891. this.absStartY = this.y;
  892. this.pointX = point.pageX;
  893. this.pointY = point.pageY;
  894. this.trigger('beforeScrollStart');
  895. };
  896. BScroll.prototype._move = function (e) {
  897. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  898. return;
  899. }
  900. if (this.options.preventDefault) {
  901. e.preventDefault();
  902. }
  903. if (this.options.stopPropagation) {
  904. e.stopPropagation();
  905. }
  906. var point = e.touches ? e.touches[0] : e;
  907. var deltaX = point.pageX - this.pointX;
  908. var deltaY = point.pageY - this.pointY;
  909. this.pointX = point.pageX;
  910. this.pointY = point.pageY;
  911. this.distX += deltaX;
  912. this.distY += deltaY;
  913. var absDistX = Math.abs(this.distX);
  914. var absDistY = Math.abs(this.distY);
  915. var timestamp = getNow();
  916. // We need to move at least momentumLimitDistance pixels for the scrolling to initiate
  917. if (timestamp - this.endTime > this.options.momentumLimitTime && !this.moved && absDistY < this.options.momentumLimitDistance && absDistX < this.options.momentumLimitDistance) {
  918. return;
  919. }
  920. // If you are scrolling in one direction lock the other
  921. if (!this.directionLocked && !this.options.freeScroll) {
  922. if (absDistX > absDistY + this.options.directionLockThreshold) {
  923. this.directionLocked = 'h'; // lock horizontally
  924. } else if (absDistY >= absDistX + this.options.directionLockThreshold) {
  925. this.directionLocked = 'v'; // lock vertically
  926. } else {
  927. this.directionLocked = 'n'; // no lock
  928. }
  929. }
  930. if (this.directionLocked === 'h') {
  931. if (this.options.eventPassthrough === 'vertical') {
  932. e.preventDefault();
  933. } else if (this.options.eventPassthrough === 'horizontal') {
  934. this.initiated = false;
  935. return;
  936. }
  937. deltaY = 0;
  938. } else if (this.directionLocked === 'v') {
  939. if (this.options.eventPassthrough === 'horizontal') {
  940. e.preventDefault();
  941. } else if (this.options.eventPassthrough === 'vertical') {
  942. this.initiated = false;
  943. return;
  944. }
  945. deltaX = 0;
  946. }
  947. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  948. deltaY = this.hasVerticalScroll ? deltaY : 0;
  949. this.movingDirectionX = deltaX > 0 ? DIRECTION_RIGHT : deltaX < 0 ? DIRECTION_LEFT : 0;
  950. this.movingDirectionY = deltaY > 0 ? DIRECTION_DOWN : deltaY < 0 ? DIRECTION_UP : 0;
  951. var newX = this.x + deltaX;
  952. var newY = this.y + deltaY;
  953. var top = false;
  954. var bottom = false;
  955. var left = false;
  956. var right = false;
  957. // Slow down or stop if outside of the boundaries
  958. var bounce = this.options.bounce;
  959. if (bounce !== false) {
  960. top = bounce.top === undefined ? true : bounce.top;
  961. bottom = bounce.bottom === undefined ? true : bounce.bottom;
  962. left = bounce.left === undefined ? true : bounce.left;
  963. right = bounce.right === undefined ? true : bounce.right;
  964. }
  965. if (newX > this.minScrollX || newX < this.maxScrollX) {
  966. if (newX > this.minScrollX && left || newX < this.maxScrollX && right) {
  967. newX = this.x + deltaX / 3;
  968. } else {
  969. newX = newX > this.minScrollX ? this.minScrollX : this.maxScrollX;
  970. }
  971. }
  972. if (newY > this.minScrollY || newY < this.maxScrollY) {
  973. if (newY > this.minScrollY && top || newY < this.maxScrollY && bottom) {
  974. newY = this.y + deltaY / 3;
  975. } else {
  976. newY = newY > this.minScrollY ? this.minScrollY : this.maxScrollY;
  977. }
  978. }
  979. if (!this.moved) {
  980. this.moved = true;
  981. this.trigger('scrollStart');
  982. }
  983. this._translate(newX, newY);
  984. if (timestamp - this.startTime > this.options.momentumLimitTime) {
  985. this.startTime = timestamp;
  986. this.startX = this.x;
  987. this.startY = this.y;
  988. if (this.options.probeType === PROBE_DEBOUNCE) {
  989. this.trigger('scroll', {
  990. x: this.x,
  991. y: this.y
  992. });
  993. }
  994. }
  995. if (this.options.probeType > PROBE_DEBOUNCE) {
  996. this.trigger('scroll', {
  997. x: this.x,
  998. y: this.y
  999. });
  1000. }
  1001. var scrollLeft = document.documentElement.scrollLeft || window.pageXOffset || document.body.scrollLeft;
  1002. var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
  1003. var pX = this.pointX - scrollLeft;
  1004. var pY = this.pointY - scrollTop;
  1005. if (pX > document.documentElement.clientWidth - this.options.momentumLimitDistance || pX < this.options.momentumLimitDistance || pY < this.options.momentumLimitDistance || pY > document.documentElement.clientHeight - this.options.momentumLimitDistance) {
  1006. this._end(e);
  1007. }
  1008. };
  1009. BScroll.prototype._end = function (e) {
  1010. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  1011. return;
  1012. }
  1013. this.initiated = false;
  1014. if (this.options.preventDefault && !preventDefaultException(e.target, this.options.preventDefaultException)) {
  1015. e.preventDefault();
  1016. }
  1017. if (this.options.stopPropagation) {
  1018. e.stopPropagation();
  1019. }
  1020. this.trigger('touchEnd', {
  1021. x: this.x,
  1022. y: this.y
  1023. });
  1024. this.isInTransition = false;
  1025. // ensures that the last position is rounded
  1026. var newX = Math.round(this.x);
  1027. var newY = Math.round(this.y);
  1028. var deltaX = newX - this.absStartX;
  1029. var deltaY = newY - this.absStartY;
  1030. this.directionX = deltaX > 0 ? DIRECTION_RIGHT : deltaX < 0 ? DIRECTION_LEFT : 0;
  1031. this.directionY = deltaY > 0 ? DIRECTION_DOWN : deltaY < 0 ? DIRECTION_UP : 0;
  1032. // if configure pull down refresh, check it first
  1033. if (this.options.pullDownRefresh && this._checkPullDown()) {
  1034. return;
  1035. }
  1036. // check if it is a click operation
  1037. if (this._checkClick(e)) {
  1038. this.trigger('scrollCancel');
  1039. return;
  1040. }
  1041. // reset if we are outside of the boundaries
  1042. if (this.resetPosition(this.options.bounceTime, ease.bounce)) {
  1043. return;
  1044. }
  1045. this._translate(newX, newY);
  1046. this.endTime = getNow();
  1047. var duration = this.endTime - this.startTime;
  1048. var absDistX = Math.abs(newX - this.startX);
  1049. var absDistY = Math.abs(newY - this.startY);
  1050. // flick
  1051. if (this._events.flick && duration < this.options.flickLimitTime && absDistX < this.options.flickLimitDistance && absDistY < this.options.flickLimitDistance) {
  1052. this.trigger('flick');
  1053. return;
  1054. }
  1055. var time = 0;
  1056. // start momentum animation if needed
  1057. if (this.options.momentum && duration < this.options.momentumLimitTime && (absDistY > this.options.momentumLimitDistance || absDistX > this.options.momentumLimitDistance)) {
  1058. var top = false;
  1059. var bottom = false;
  1060. var left = false;
  1061. var right = false;
  1062. var bounce = this.options.bounce;
  1063. if (bounce !== false) {
  1064. top = bounce.top === undefined ? true : bounce.top;
  1065. bottom = bounce.bottom === undefined ? true : bounce.bottom;
  1066. left = bounce.left === undefined ? true : bounce.left;
  1067. right = bounce.right === undefined ? true : bounce.right;
  1068. }
  1069. var wrapperWidth = this.directionX === DIRECTION_RIGHT && left || this.directionX === DIRECTION_LEFT && right ? this.wrapperWidth : 0;
  1070. var wrapperHeight = this.directionY === DIRECTION_DOWN && top || this.directionY === DIRECTION_UP && bottom ? this.wrapperHeight : 0;
  1071. var momentumX = this.hasHorizontalScroll ? momentum(this.x, this.startX, duration, this.maxScrollX, this.minScrollX, wrapperWidth, this.options, this) : { destination: newX, duration: 0 };
  1072. var momentumY = this.hasVerticalScroll ? momentum(this.y, this.startY, duration, this.maxScrollY, this.minScrollY, wrapperHeight, this.options, this) : { destination: newY, duration: 0 };
  1073. newX = momentumX.destination;
  1074. newY = momentumY.destination;
  1075. time = Math.max(momentumX.duration, momentumY.duration);
  1076. this.isInTransition = true;
  1077. } else {
  1078. if (this.options.wheel) {
  1079. newY = this._findNearestValidWheel(newY).y;
  1080. time = this.options.wheel.adjustTime || 400;
  1081. }
  1082. }
  1083. var easing = ease.swipe;
  1084. if (this.options.snap) {
  1085. var snap = this._nearestSnap(newX, newY);
  1086. this.currentPage = snap;
  1087. time = this.options.snapSpeed || Math.max(Math.max(Math.min(Math.abs(newX - snap.x), 1000), Math.min(Math.abs(newY - snap.y), 1000)), 300);
  1088. newX = snap.x;
  1089. newY = snap.y;
  1090. this.directionX = 0;
  1091. this.directionY = 0;
  1092. easing = this.options.snap.easing || ease.bounce;
  1093. }
  1094. if (newX !== this.x || newY !== this.y) {
  1095. // change easing function when scroller goes out of the boundaries
  1096. if (newX > this.minScrollX || newX < this.maxScrollX || newY > this.minScrollY || newY < this.maxScrollY) {
  1097. easing = ease.swipeBounce;
  1098. }
  1099. this.scrollTo(newX, newY, time, easing);
  1100. return;
  1101. }
  1102. if (this.options.wheel) {
  1103. this.selectedIndex = this._findNearestValidWheel(this.y).index;
  1104. }
  1105. this.trigger('scrollEnd', {
  1106. x: this.x,
  1107. y: this.y
  1108. });
  1109. };
  1110. BScroll.prototype._checkClick = function (e) {
  1111. // when in the process of pulling down, it should not prevent click
  1112. var preventClick = this.stopFromTransition && !this.pulling;
  1113. this.stopFromTransition = false;
  1114. // we scrolled less than 15 pixels
  1115. if (!this.moved) {
  1116. if (this.options.wheel) {
  1117. if (this.target && this.target.className === this.options.wheel.wheelWrapperClass) {
  1118. var index = this._findNearestValidWheel(this.y).index;
  1119. var _offset = Math.round((this.pointY + offsetToBody(this.wrapper).top - this.wrapperHeight / 2) / this.itemHeight);
  1120. this.target = this.items[index + _offset];
  1121. }
  1122. var top = offset(this.target).top;
  1123. var left = offset(this.target).left;
  1124. top -= this.wrapperOffset.top;
  1125. top -= Math.round(this.target.offsetHeight / 2 - this.wrapper.offsetHeight / 2) || 0;
  1126. left -= this.wrapperOffset.left;
  1127. left -= Math.round(this.target.offsetWidth / 2 - this.wrapper.offsetWidth / 2) || 0;
  1128. top = this._findNearestValidWheel(top).y;
  1129. this.scrollTo(left, top, this.options.wheel.adjustTime || 400, ease.swipe);
  1130. return true;
  1131. } else {
  1132. if (!preventClick) {
  1133. var _dblclick = this.options.dblclick;
  1134. var dblclickTrigged = false;
  1135. if (_dblclick && this.lastClickTime) {
  1136. var _dblclick$delay = _dblclick.delay,
  1137. delay = _dblclick$delay === undefined ? 300 : _dblclick$delay;
  1138. if (getNow() - this.lastClickTime < delay) {
  1139. dblclickTrigged = true;
  1140. dblclick(e);
  1141. }
  1142. }
  1143. if (this.options.tap) {
  1144. tap(e, this.options.tap);
  1145. }
  1146. if (this.options.click && !preventDefaultException(e.target, this.options.preventDefaultException)) {
  1147. click(e);
  1148. }
  1149. this.lastClickTime = dblclickTrigged ? null : getNow();
  1150. return true;
  1151. }
  1152. return false;
  1153. }
  1154. }
  1155. return false;
  1156. };
  1157. BScroll.prototype._resize = function () {
  1158. var _this = this;
  1159. if (!this.enabled) {
  1160. return;
  1161. }
  1162. // fix a scroll problem under Android condition
  1163. if (isAndroid) {
  1164. this.wrapper.scrollTop = 0;
  1165. }
  1166. clearTimeout(this.resizeTimeout);
  1167. this.resizeTimeout = setTimeout(function () {
  1168. _this.refresh();
  1169. }, this.options.resizePolling);
  1170. };
  1171. BScroll.prototype._startProbe = function () {
  1172. cancelAnimationFrame(this.probeTimer);
  1173. this.probeTimer = requestAnimationFrame(probe);
  1174. var me = this;
  1175. function probe() {
  1176. var pos = me.getComputedPosition();
  1177. me.trigger('scroll', pos);
  1178. if (!me.isInTransition) {
  1179. me.trigger('scrollEnd', pos);
  1180. return;
  1181. }
  1182. me.probeTimer = requestAnimationFrame(probe);
  1183. }
  1184. };
  1185. BScroll.prototype._transitionTime = function () {
  1186. var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1187. this.scrollerStyle[style.transitionDuration] = time + 'ms';
  1188. if (this.options.wheel) {
  1189. for (var i = 0; i < this.items.length; i++) {
  1190. this.items[i].style[style.transitionDuration] = time + 'ms';
  1191. }
  1192. }
  1193. if (this.indicators) {
  1194. for (var _i = 0; _i < this.indicators.length; _i++) {
  1195. this.indicators[_i].transitionTime(time);
  1196. }
  1197. }
  1198. };
  1199. BScroll.prototype._transitionTimingFunction = function (easing) {
  1200. this.scrollerStyle[style.transitionTimingFunction] = easing;
  1201. if (this.options.wheel) {
  1202. for (var i = 0; i < this.items.length; i++) {
  1203. this.items[i].style[style.transitionTimingFunction] = easing;
  1204. }
  1205. }
  1206. if (this.indicators) {
  1207. for (var _i2 = 0; _i2 < this.indicators.length; _i2++) {
  1208. this.indicators[_i2].transitionTimingFunction(easing);
  1209. }
  1210. }
  1211. };
  1212. BScroll.prototype._transitionEnd = function (e) {
  1213. if (e.target !== this.scroller || !this.isInTransition) {
  1214. return;
  1215. }
  1216. this._transitionTime();
  1217. var needReset = !this.pulling || this.movingDirectionY === DIRECTION_UP;
  1218. if (needReset && !this.resetPosition(this.options.bounceTime, ease.bounce)) {
  1219. this.isInTransition = false;
  1220. if (this.options.probeType !== PROBE_REALTIME) {
  1221. this.trigger('scrollEnd', {
  1222. x: this.x,
  1223. y: this.y
  1224. });
  1225. }
  1226. }
  1227. };
  1228. BScroll.prototype._translate = function (x, y, scale) {
  1229. assert(!isUndef(x) && !isUndef(y), 'Translate x or y is null or undefined.');
  1230. if (isUndef(scale)) {
  1231. scale = this.scale;
  1232. }
  1233. if (this.options.useTransform) {
  1234. this.scrollerStyle[style.transform] = 'translate(' + x + 'px,' + y + 'px) scale(' + scale + ')' + this.translateZ;
  1235. } else {
  1236. x = Math.round(x);
  1237. y = Math.round(y);
  1238. this.scrollerStyle.left = x + 'px';
  1239. this.scrollerStyle.top = y + 'px';
  1240. }
  1241. if (this.options.wheel) {
  1242. var _options$wheel$rotate = this.options.wheel.rotate,
  1243. rotate = _options$wheel$rotate === undefined ? 25 : _options$wheel$rotate;
  1244. for (var i = 0; i < this.items.length; i++) {
  1245. var deg = rotate * (y / this.itemHeight + i);
  1246. this.items[i].style[style.transform] = 'rotateX(' + deg + 'deg)';
  1247. }
  1248. }
  1249. this.x = x;
  1250. this.y = y;
  1251. this.setScale(scale);
  1252. if (this.indicators) {
  1253. for (var _i3 = 0; _i3 < this.indicators.length; _i3++) {
  1254. this.indicators[_i3].updatePosition();
  1255. }
  1256. }
  1257. };
  1258. BScroll.prototype._animate = function (destX, destY, duration, easingFn) {
  1259. var me = this;
  1260. var startX = this.x;
  1261. var startY = this.y;
  1262. var startScale = this.lastScale;
  1263. var destScale = this.scale;
  1264. var startTime = getNow();
  1265. var destTime = startTime + duration;
  1266. function step() {
  1267. var now = getNow();
  1268. if (now >= destTime) {
  1269. me.isAnimating = false;
  1270. me._translate(destX, destY, destScale);
  1271. me.trigger('scroll', {
  1272. x: me.x,
  1273. y: me.y
  1274. });
  1275. if (!me.pulling && !me.resetPosition(me.options.bounceTime)) {
  1276. me.trigger('scrollEnd', {
  1277. x: me.x,
  1278. y: me.y
  1279. });
  1280. }
  1281. return;
  1282. }
  1283. now = (now - startTime) / duration;
  1284. var easing = easingFn(now);
  1285. var newX = (destX - startX) * easing + startX;
  1286. var newY = (destY - startY) * easing + startY;
  1287. var newScale = (destScale - startScale) * easing + startScale;
  1288. me._translate(newX, newY, newScale);
  1289. if (me.isAnimating) {
  1290. me.animateTimer = requestAnimationFrame(step);
  1291. }
  1292. if (me.options.probeType === PROBE_REALTIME) {
  1293. me.trigger('scroll', {
  1294. x: me.x,
  1295. y: me.y
  1296. });
  1297. }
  1298. }
  1299. this.isAnimating = true;
  1300. cancelAnimationFrame(this.animateTimer);
  1301. step();
  1302. };
  1303. BScroll.prototype.scrollBy = function (x, y) {
  1304. var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  1305. var easing = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ease.bounce;
  1306. x = this.x + x;
  1307. y = this.y + y;
  1308. this.scrollTo(x, y, time, easing);
  1309. };
  1310. BScroll.prototype.scrollTo = function (x, y) {
  1311. var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  1312. var easing = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ease.bounce;
  1313. var isSilent = arguments[4];
  1314. if (this.options.wheel) {
  1315. y = this._findNearestValidWheel(y).y;
  1316. }
  1317. if (x === this.x && y === this.y) {
  1318. return;
  1319. }
  1320. this.isInTransition = this.options.useTransition && time > 0 && (this.x !== x || this.y !== y);
  1321. if (!time || this.options.useTransition) {
  1322. this._transitionTimingFunction(easing.style);
  1323. this._transitionTime(time);
  1324. this._translate(x, y);
  1325. if (time && this.options.probeType === PROBE_REALTIME) {
  1326. this._startProbe();
  1327. }
  1328. if (!time && !isSilent) {
  1329. // don't trigger resetPosition when zoom feature is open, fix #748
  1330. if (this.options.zoom) return;
  1331. this.trigger('scroll', {
  1332. x: x,
  1333. y: y
  1334. });
  1335. // force reflow to put everything in position
  1336. this._reflow = document.body.offsetHeight;
  1337. if (!this.resetPosition(this.options.bounceTime, ease.bounce)) {
  1338. this.trigger('scrollEnd', {
  1339. x: x,
  1340. y: y
  1341. });
  1342. }
  1343. }
  1344. if (this.options.wheel) {
  1345. this.selectedIndex = this._findNearestValidWheel(y).index;
  1346. }
  1347. } else {
  1348. this._animate(x, y, time, easing.fn);
  1349. }
  1350. };
  1351. BScroll.prototype.scrollToElement = function (el, time, offsetX, offsetY, easing) {
  1352. if (!el) {
  1353. return;
  1354. }
  1355. el = el.nodeType ? el : this.scroller.querySelector(el);
  1356. if (this.options.wheel && !el.classList.contains(this.options.wheel.wheelItemClass)) {
  1357. return;
  1358. }
  1359. var pos = offset(el);
  1360. pos.left -= this.wrapperOffset.left;
  1361. pos.top -= this.wrapperOffset.top;
  1362. // if offsetX/Y are true we center the element to the screen
  1363. if (offsetX === true) {
  1364. offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
  1365. }
  1366. if (offsetY === true) {
  1367. offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
  1368. }
  1369. pos.left -= offsetX || 0;
  1370. pos.top -= offsetY || 0;
  1371. pos.left = pos.left > this.minScrollX ? this.minScrollX : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
  1372. pos.top = pos.top > this.minScrollY ? this.minScrollY : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
  1373. if (this.options.wheel) {
  1374. pos.top = this._findNearestValidWheel(pos.top).y;
  1375. }
  1376. this.scrollTo(pos.left, pos.top, time, easing);
  1377. };
  1378. BScroll.prototype.resetPosition = function () {
  1379. var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1380. var easeing = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ease.bounce;
  1381. var x = this.x;
  1382. var roundX = Math.round(x);
  1383. if (!this.hasHorizontalScroll || roundX > this.minScrollX) {
  1384. x = this.minScrollX;
  1385. } else if (roundX < this.maxScrollX) {
  1386. x = this.maxScrollX;
  1387. }
  1388. var y = this.y;
  1389. var roundY = Math.round(y);
  1390. if (!this.hasVerticalScroll || roundY > this.minScrollY) {
  1391. y = this.minScrollY;
  1392. } else if (roundY < this.maxScrollY) {
  1393. y = this.maxScrollY;
  1394. }
  1395. if (x === this.x && y === this.y) {
  1396. return false;
  1397. }
  1398. this.scrollTo(x, y, time, easeing);
  1399. return true;
  1400. };
  1401. BScroll.prototype.getComputedPosition = function () {
  1402. var matrix = window.getComputedStyle(this.scroller, null);
  1403. var x = void 0;
  1404. var y = void 0;
  1405. if (this.options.useTransform) {
  1406. matrix = matrix[style.transform].split(')')[0].split(', ');
  1407. x = +(matrix[12] || matrix[4]);
  1408. y = +(matrix[13] || matrix[5]);
  1409. } else {
  1410. x = +matrix.left.replace(/[^-\d.]/g, '');
  1411. y = +matrix.top.replace(/[^-\d.]/g, '');
  1412. }
  1413. return {
  1414. x: x,
  1415. y: y
  1416. };
  1417. };
  1418. BScroll.prototype.stop = function () {
  1419. if (this.options.useTransition && this.isInTransition) {
  1420. this.isInTransition = false;
  1421. cancelAnimationFrame(this.probeTimer);
  1422. var pos = this.getComputedPosition();
  1423. this._translate(pos.x, pos.y);
  1424. if (this.options.wheel) {
  1425. this.target = this.items[this._findNearestValidWheel(pos.y).index];
  1426. } else {
  1427. this.trigger('scrollEnd', {
  1428. x: this.x,
  1429. y: this.y
  1430. });
  1431. }
  1432. this.stopFromTransition = true;
  1433. } else if (!this.options.useTransition && this.isAnimating) {
  1434. this.isAnimating = false;
  1435. cancelAnimationFrame(this.animateTimer);
  1436. this.trigger('scrollEnd', {
  1437. x: this.x,
  1438. y: this.y
  1439. });
  1440. this.stopFromTransition = true;
  1441. }
  1442. };
  1443. BScroll.prototype.destroy = function () {
  1444. this.destroyed = true;
  1445. this.trigger('destroy');
  1446. if (this.options.useTransition) {
  1447. cancelAnimationFrame(this.probeTimer);
  1448. } else {
  1449. cancelAnimationFrame(this.animateTimer);
  1450. }
  1451. this._removeDOMEvents();
  1452. // remove custom events
  1453. this._events = {};
  1454. };
  1455. }
  1456. function snapMixin(BScroll) {
  1457. BScroll.prototype._initSnap = function () {
  1458. var _this = this;
  1459. this.currentPage = {};
  1460. var snap = this.options.snap;
  1461. if (snap.loop) {
  1462. var children = this.scroller.children;
  1463. if (children.length > 1) {
  1464. prepend(children[children.length - 1].cloneNode(true), this.scroller);
  1465. this.scroller.appendChild(children[1].cloneNode(true));
  1466. } else {
  1467. // Loop does not make any sense if there is only one child.
  1468. snap.loop = false;
  1469. }
  1470. }
  1471. var el = snap.el;
  1472. if (typeof el === 'string') {
  1473. el = this.scroller.querySelectorAll(el);
  1474. }
  1475. this.on('refresh', function () {
  1476. _this.pages = [];
  1477. if (!_this.wrapperWidth || !_this.wrapperHeight || !_this.scrollerWidth || !_this.scrollerHeight) {
  1478. return;
  1479. }
  1480. var stepX = snap.stepX || _this.wrapperWidth;
  1481. var stepY = snap.stepY || _this.wrapperHeight;
  1482. var x = 0;
  1483. var y = void 0;
  1484. var cx = void 0;
  1485. var cy = void 0;
  1486. var i = 0;
  1487. var l = void 0;
  1488. var m = 0;
  1489. var n = void 0;
  1490. var rect = void 0;
  1491. if (!el) {
  1492. cx = Math.round(stepX / 2);
  1493. cy = Math.round(stepY / 2);
  1494. while (x > -_this.scrollerWidth) {
  1495. _this.pages[i] = [];
  1496. l = 0;
  1497. y = 0;
  1498. while (y > -_this.scrollerHeight) {
  1499. _this.pages[i][l] = {
  1500. x: Math.max(x, _this.maxScrollX),
  1501. y: Math.max(y, _this.maxScrollY),
  1502. width: stepX,
  1503. height: stepY,
  1504. cx: x - cx,
  1505. cy: y - cy
  1506. };
  1507. y -= stepY;
  1508. l++;
  1509. }
  1510. x -= stepX;
  1511. i++;
  1512. }
  1513. } else {
  1514. l = el.length;
  1515. n = -1;
  1516. for (; i < l; i++) {
  1517. rect = getRect(el[i]);
  1518. if (i === 0 || rect.left <= getRect(el[i - 1]).left) {
  1519. m = 0;
  1520. n++;
  1521. }
  1522. if (!_this.pages[m]) {
  1523. _this.pages[m] = [];
  1524. }
  1525. x = Math.max(-rect.left, _this.maxScrollX);
  1526. y = Math.max(-rect.top, _this.maxScrollY);
  1527. cx = x - Math.round(rect.width / 2);
  1528. cy = y - Math.round(rect.height / 2);
  1529. _this.pages[m][n] = {
  1530. x: x,
  1531. y: y,
  1532. width: rect.width,
  1533. height: rect.height,
  1534. cx: cx,
  1535. cy: cy
  1536. };
  1537. if (x > _this.maxScrollX) {
  1538. m++;
  1539. }
  1540. }
  1541. }
  1542. _this._checkSnapLoop();
  1543. var initPageX = snap._loopX ? 1 : 0;
  1544. var initPageY = snap._loopY ? 1 : 0;
  1545. _this._goToPage(_this.currentPage.pageX || initPageX, _this.currentPage.pageY || initPageY, 0, undefined, true);
  1546. // Update snap threshold if needed.
  1547. var snapThreshold = snap.threshold;
  1548. if (snapThreshold % 1 === 0) {
  1549. _this.snapThresholdX = snapThreshold;
  1550. _this.snapThresholdY = snapThreshold;
  1551. } else {
  1552. _this.snapThresholdX = Math.round(_this.pages[_this.currentPage.pageX][_this.currentPage.pageY].width * snapThreshold);
  1553. _this.snapThresholdY = Math.round(_this.pages[_this.currentPage.pageX][_this.currentPage.pageY].height * snapThreshold);
  1554. }
  1555. });
  1556. this.on('scrollEnd', function () {
  1557. if (snap.loop) {
  1558. if (snap._loopX) {
  1559. if (_this.currentPage.pageX === 0) {
  1560. _this._goToPage(_this.pages.length - 2, _this.currentPage.pageY, 0, undefined, true);
  1561. }
  1562. if (_this.currentPage.pageX === _this.pages.length - 1) {
  1563. _this._goToPage(1, _this.currentPage.pageY, 0, undefined, true);
  1564. }
  1565. } else {
  1566. if (_this.currentPage.pageY === 0) {
  1567. _this._goToPage(_this.currentPage.pageX, _this.pages[0].length - 2, 0, undefined, true);
  1568. }
  1569. if (_this.currentPage.pageY === _this.pages[0].length - 1) {
  1570. _this._goToPage(_this.currentPage.pageX, 1, 0, undefined, true);
  1571. }
  1572. }
  1573. }
  1574. });
  1575. if (snap.listenFlick !== false) {
  1576. this.on('flick', function () {
  1577. var time = snap.speed || Math.max(Math.max(Math.min(Math.abs(_this.x - _this.startX), 1000), Math.min(Math.abs(_this.y - _this.startY), 1000)), 300);
  1578. _this._goToPage(_this.currentPage.pageX + _this.directionX, _this.currentPage.pageY + _this.directionY, time);
  1579. });
  1580. }
  1581. this.on('destroy', function () {
  1582. if (snap.loop) {
  1583. var _children = _this.scroller.children;
  1584. if (_children.length > 2) {
  1585. removeChild(_this.scroller, _children[_children.length - 1]);
  1586. removeChild(_this.scroller, _children[0]);
  1587. }
  1588. }
  1589. });
  1590. };
  1591. BScroll.prototype._checkSnapLoop = function () {
  1592. var snap = this.options.snap;
  1593. if (!snap.loop || !this.pages || !this.pages.length) {
  1594. return;
  1595. }
  1596. if (this.pages.length > 1) {
  1597. snap._loopX = true;
  1598. }
  1599. if (this.pages[0] && this.pages[0].length > 1) {
  1600. snap._loopY = true;
  1601. }
  1602. if (snap._loopX && snap._loopY) {
  1603. warn('Loop does not support two direction at the same time.');
  1604. }
  1605. };
  1606. BScroll.prototype._nearestSnap = function (x, y) {
  1607. if (!this.pages.length) {
  1608. return { x: 0, y: 0, pageX: 0, pageY: 0 };
  1609. }
  1610. var i = 0;
  1611. // Check if we exceeded the snap threshold
  1612. if (Math.abs(x - this.absStartX) <= this.snapThresholdX && Math.abs(y - this.absStartY) <= this.snapThresholdY) {
  1613. return this.currentPage;
  1614. }
  1615. if (x > this.minScrollX) {
  1616. x = this.minScrollX;
  1617. } else if (x < this.maxScrollX) {
  1618. x = this.maxScrollX;
  1619. }
  1620. if (y > this.minScrollY) {
  1621. y = this.minScrollY;
  1622. } else if (y < this.maxScrollY) {
  1623. y = this.maxScrollY;
  1624. }
  1625. var l = this.pages.length;
  1626. for (; i < l; i++) {
  1627. if (x >= this.pages[i][0].cx) {
  1628. x = this.pages[i][0].x;
  1629. break;
  1630. }
  1631. }
  1632. l = this.pages[i].length;
  1633. var m = 0;
  1634. for (; m < l; m++) {
  1635. if (y >= this.pages[0][m].cy) {
  1636. y = this.pages[0][m].y;
  1637. break;
  1638. }
  1639. }
  1640. if (i === this.currentPage.pageX) {
  1641. i += this.directionX;
  1642. if (i < 0) {
  1643. i = 0;
  1644. } else if (i >= this.pages.length) {
  1645. i = this.pages.length - 1;
  1646. }
  1647. x = this.pages[i][0].x;
  1648. }
  1649. if (m === this.currentPage.pageY) {
  1650. m += this.directionY;
  1651. if (m < 0) {
  1652. m = 0;
  1653. } else if (m >= this.pages[0].length) {
  1654. m = this.pages[0].length - 1;
  1655. }
  1656. y = this.pages[0][m].y;
  1657. }
  1658. return {
  1659. x: x,
  1660. y: y,
  1661. pageX: i,
  1662. pageY: m
  1663. };
  1664. };
  1665. BScroll.prototype._goToPage = function (x) {
  1666. var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  1667. var time = arguments[2];
  1668. var easing = arguments[3];
  1669. var isSilent = arguments[4];
  1670. var snap = this.options.snap;
  1671. if (!snap || !this.pages || !this.pages.length) {
  1672. return;
  1673. }
  1674. easing = easing || snap.easing || ease.bounce;
  1675. if (x >= this.pages.length) {
  1676. x = this.pages.length - 1;
  1677. } else if (x < 0) {
  1678. x = 0;
  1679. }
  1680. if (!this.pages[x]) {
  1681. return;
  1682. }
  1683. if (y >= this.pages[x].length) {
  1684. y = this.pages[x].length - 1;
  1685. } else if (y < 0) {
  1686. y = 0;
  1687. }
  1688. var posX = this.pages[x][y].x;
  1689. var posY = this.pages[x][y].y;
  1690. time = time === undefined ? snap.speed || Math.max(Math.max(Math.min(Math.abs(posX - this.x), 1000), Math.min(Math.abs(posY - this.y), 1000)), 300) : time;
  1691. this.currentPage = {
  1692. x: posX,
  1693. y: posY,
  1694. pageX: x,
  1695. pageY: y
  1696. };
  1697. this.scrollTo(posX, posY, time, easing, isSilent);
  1698. };
  1699. BScroll.prototype.goToPage = function (x, y, time, easing) {
  1700. var snap = this.options.snap;
  1701. if (!snap || !this.pages || !this.pages.length) {
  1702. return;
  1703. }
  1704. if (snap.loop) {
  1705. var len = void 0;
  1706. if (snap._loopX) {
  1707. len = this.pages.length - 2;
  1708. if (x >= len) {
  1709. x = len - 1;
  1710. } else if (x < 0) {
  1711. x = 0;
  1712. }
  1713. x += 1;
  1714. } else {
  1715. len = this.pages[0].length - 2;
  1716. if (y >= len) {
  1717. y = len - 1;
  1718. } else if (y < 0) {
  1719. y = 0;
  1720. }
  1721. y += 1;
  1722. }
  1723. }
  1724. this._goToPage(x, y, time, easing);
  1725. };
  1726. BScroll.prototype.next = function (time, easing) {
  1727. var snap = this.options.snap;
  1728. if (!snap) {
  1729. return;
  1730. }
  1731. var x = this.currentPage.pageX;
  1732. var y = this.currentPage.pageY;
  1733. x++;
  1734. if (x >= this.pages.length && this.hasVerticalScroll) {
  1735. x = 0;
  1736. y++;
  1737. }
  1738. this._goToPage(x, y, time, easing);
  1739. };
  1740. BScroll.prototype.prev = function (time, easing) {
  1741. var snap = this.options.snap;
  1742. if (!snap) {
  1743. return;
  1744. }
  1745. var x = this.currentPage.pageX;
  1746. var y = this.currentPage.pageY;
  1747. x--;
  1748. if (x < 0 && this.hasVerticalScroll) {
  1749. x = 0;
  1750. y--;
  1751. }
  1752. this._goToPage(x, y, time, easing);
  1753. };
  1754. BScroll.prototype.getCurrentPage = function () {
  1755. var snap = this.options.snap;
  1756. if (!snap) {
  1757. return null;
  1758. }
  1759. if (snap.loop) {
  1760. var currentPage = void 0;
  1761. if (snap._loopX) {
  1762. currentPage = extend({}, this.currentPage, {
  1763. pageX: this.currentPage.pageX - 1
  1764. });
  1765. } else {
  1766. currentPage = extend({}, this.currentPage, {
  1767. pageY: this.currentPage.pageY - 1
  1768. });
  1769. }
  1770. return currentPage;
  1771. }
  1772. return this.currentPage;
  1773. };
  1774. }
  1775. function wheelMixin(BScroll) {
  1776. BScroll.prototype.wheelTo = function () {
  1777. var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1778. if (this.options.wheel) {
  1779. var y = -index * this.itemHeight;
  1780. this.scrollTo(0, y);
  1781. }
  1782. };
  1783. BScroll.prototype.getSelectedIndex = function () {
  1784. return this.options.wheel && this.selectedIndex;
  1785. };
  1786. BScroll.prototype._initWheel = function () {
  1787. var wheel = this.options.wheel;
  1788. if (!wheel.wheelWrapperClass) {
  1789. wheel.wheelWrapperClass = 'wheel-scroll';
  1790. }
  1791. if (!wheel.wheelItemClass) {
  1792. wheel.wheelItemClass = 'wheel-item';
  1793. }
  1794. if (!wheel.wheelDisabledItemClass) {
  1795. wheel.wheelDisabledItemClass = 'wheel-disabled-item';
  1796. }
  1797. if (wheel.selectedIndex === undefined) {
  1798. wheel.selectedIndex = 0;
  1799. }
  1800. };
  1801. BScroll.prototype._findNearestValidWheel = function (y) {
  1802. y = y > 0 ? 0 : y < this.maxScrollY ? this.maxScrollY : y;
  1803. var wheel = this.options.wheel;
  1804. var currentIndex = Math.abs(Math.round(-y / this.itemHeight));
  1805. var cacheIndex = currentIndex;
  1806. var items = this.items;
  1807. // Impersonation web native select
  1808. // first, check whether there is a enable item whose index is smaller than currentIndex
  1809. // then, check whether there is a enable item whose index is bigger than currentIndex
  1810. // otherwise, there are all disabled items, just keep currentIndex unchange
  1811. while (currentIndex >= 0) {
  1812. if (items[currentIndex].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
  1813. break;
  1814. }
  1815. currentIndex--;
  1816. }
  1817. if (currentIndex < 0) {
  1818. currentIndex = cacheIndex;
  1819. while (currentIndex <= items.length - 1) {
  1820. if (items[currentIndex].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
  1821. break;
  1822. }
  1823. currentIndex++;
  1824. }
  1825. }
  1826. // keep it unchange when all the items are disabled
  1827. if (currentIndex === items.length) {
  1828. currentIndex = cacheIndex;
  1829. }
  1830. // when all the items are disabled, this.selectedIndex should always be -1
  1831. return {
  1832. index: this.wheelItemsAllDisabled ? -1 : currentIndex,
  1833. y: -currentIndex * this.itemHeight
  1834. };
  1835. };
  1836. BScroll.prototype._checkWheelAllDisabled = function () {
  1837. var wheel = this.options.wheel;
  1838. var items = this.items;
  1839. this.wheelItemsAllDisabled = true;
  1840. for (var i = 0; i < items.length; i++) {
  1841. if (items[i].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
  1842. this.wheelItemsAllDisabled = false;
  1843. break;
  1844. }
  1845. }
  1846. };
  1847. }
  1848. var INDICATOR_MIN_LEN = 8;
  1849. function scrollbarMixin(BScroll) {
  1850. BScroll.prototype._initScrollbar = function () {
  1851. var _this = this;
  1852. var _options$scrollbar = this.options.scrollbar,
  1853. _options$scrollbar$fa = _options$scrollbar.fade,
  1854. fade = _options$scrollbar$fa === undefined ? true : _options$scrollbar$fa,
  1855. _options$scrollbar$in = _options$scrollbar.interactive,
  1856. interactive = _options$scrollbar$in === undefined ? false : _options$scrollbar$in;
  1857. this.indicators = [];
  1858. var indicator = void 0;
  1859. if (this.options.scrollX) {
  1860. indicator = {
  1861. el: createScrollbar('horizontal'),
  1862. direction: 'horizontal',
  1863. fade: fade,
  1864. interactive: interactive
  1865. };
  1866. this._insertScrollBar(indicator.el);
  1867. this.indicators.push(new Indicator(this, indicator));
  1868. }
  1869. if (this.options.scrollY) {
  1870. indicator = {
  1871. el: createScrollbar('vertical'),
  1872. direction: 'vertical',
  1873. fade: fade,
  1874. interactive: interactive
  1875. };
  1876. this._insertScrollBar(indicator.el);
  1877. this.indicators.push(new Indicator(this, indicator));
  1878. }
  1879. this.on('refresh', function () {
  1880. for (var i = 0; i < _this.indicators.length; i++) {
  1881. _this.indicators[i].refresh();
  1882. }
  1883. });
  1884. if (fade) {
  1885. this.on('scrollEnd', function () {
  1886. for (var i = 0; i < _this.indicators.length; i++) {
  1887. _this.indicators[i].fade();
  1888. }
  1889. });
  1890. this.on('scrollCancel', function () {
  1891. for (var i = 0; i < _this.indicators.length; i++) {
  1892. _this.indicators[i].fade();
  1893. }
  1894. });
  1895. this.on('scrollStart', function () {
  1896. for (var i = 0; i < _this.indicators.length; i++) {
  1897. _this.indicators[i].fade(true);
  1898. }
  1899. });
  1900. this.on('beforeScrollStart', function () {
  1901. for (var i = 0; i < _this.indicators.length; i++) {
  1902. _this.indicators[i].fade(true, true);
  1903. }
  1904. });
  1905. }
  1906. this.on('destroy', function () {
  1907. _this._removeScrollBars();
  1908. });
  1909. };
  1910. BScroll.prototype._insertScrollBar = function (scrollbar) {
  1911. this.wrapper.appendChild(scrollbar);
  1912. };
  1913. BScroll.prototype._removeScrollBars = function () {
  1914. for (var i = 0; i < this.indicators.length; i++) {
  1915. this.indicators[i].destroy();
  1916. }
  1917. };
  1918. }
  1919. function createScrollbar(direction) {
  1920. var scrollbar = document.createElement('div');
  1921. var indicator = document.createElement('div');
  1922. scrollbar.style.cssText = 'position:absolute;z-index:9999;pointerEvents:none';
  1923. indicator.style.cssText = '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;';
  1924. indicator.className = 'bscroll-indicator';
  1925. if (direction === 'horizontal') {
  1926. scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
  1927. indicator.style.height = '100%';
  1928. scrollbar.className = 'bscroll-horizontal-scrollbar';
  1929. } else {
  1930. scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
  1931. indicator.style.width = '100%';
  1932. scrollbar.className = 'bscroll-vertical-scrollbar';
  1933. }
  1934. scrollbar.style.cssText += ';overflow:hidden';
  1935. scrollbar.appendChild(indicator);
  1936. return scrollbar;
  1937. }
  1938. function Indicator(scroller, options) {
  1939. this.wrapper = options.el;
  1940. this.wrapperStyle = this.wrapper.style;
  1941. this.indicator = this.wrapper.children[0];
  1942. this.indicatorStyle = this.indicator.style;
  1943. this.scroller = scroller;
  1944. this.direction = options.direction;
  1945. if (options.fade) {
  1946. this.visible = 0;
  1947. this.wrapperStyle.opacity = '0';
  1948. } else {
  1949. this.visible = 1;
  1950. }
  1951. this.sizeRatioX = 1;
  1952. this.sizeRatioY = 1;
  1953. this.maxPosX = 0;
  1954. this.maxPosY = 0;
  1955. this.x = 0;
  1956. this.y = 0;
  1957. if (options.interactive) {
  1958. this._addDOMEvents();
  1959. }
  1960. }
  1961. Indicator.prototype.handleEvent = function (e) {
  1962. switch (e.type) {
  1963. case 'touchstart':
  1964. case 'mousedown':
  1965. this._start(e);
  1966. break;
  1967. case 'touchmove':
  1968. case 'mousemove':
  1969. this._move(e);
  1970. break;
  1971. case 'touchend':
  1972. case 'mouseup':
  1973. case 'touchcancel':
  1974. case 'mousecancel':
  1975. this._end(e);
  1976. break;
  1977. }
  1978. };
  1979. Indicator.prototype.refresh = function () {
  1980. if (this._shouldShow()) {
  1981. this.transitionTime();
  1982. this._calculate();
  1983. this.updatePosition();
  1984. }
  1985. };
  1986. Indicator.prototype.fade = function (visible, hold) {
  1987. var _this2 = this;
  1988. if (hold && !this.visible) {
  1989. return;
  1990. }
  1991. var time = visible ? 250 : 500;
  1992. visible = visible ? '1' : '0';
  1993. this.wrapperStyle[style.transitionDuration] = time + 'ms';
  1994. clearTimeout(this.fadeTimeout);
  1995. this.fadeTimeout = setTimeout(function () {
  1996. _this2.wrapperStyle.opacity = visible;
  1997. _this2.visible = +visible;
  1998. }, 0);
  1999. };
  2000. Indicator.prototype.updatePosition = function () {
  2001. if (this.direction === 'vertical') {
  2002. var y = Math.round(this.sizeRatioY * this.scroller.y);
  2003. if (y < 0) {
  2004. this.transitionTime(500);
  2005. var height = Math.max(this.indicatorHeight + y * 3, INDICATOR_MIN_LEN);
  2006. this.indicatorStyle.height = height + 'px';
  2007. y = 0;
  2008. } else if (y > this.maxPosY) {
  2009. this.transitionTime(500);
  2010. var _height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, INDICATOR_MIN_LEN);
  2011. this.indicatorStyle.height = _height + 'px';
  2012. y = this.maxPosY + this.indicatorHeight - _height;
  2013. } else {
  2014. this.indicatorStyle.height = this.indicatorHeight + 'px';
  2015. }
  2016. this.y = y;
  2017. if (this.scroller.options.useTransform) {
  2018. this.indicatorStyle[style.transform] = 'translateY(' + y + 'px)' + this.scroller.translateZ;
  2019. } else {
  2020. this.indicatorStyle.top = y + 'px';
  2021. }
  2022. } else {
  2023. var x = Math.round(this.sizeRatioX * this.scroller.x);
  2024. if (x < 0) {
  2025. this.transitionTime(500);
  2026. var width = Math.max(this.indicatorWidth + x * 3, INDICATOR_MIN_LEN);
  2027. this.indicatorStyle.width = width + 'px';
  2028. x = 0;
  2029. } else if (x > this.maxPosX) {
  2030. this.transitionTime(500);
  2031. var _width = Math.max(this.indicatorWidth - (x - this.maxPosX) * 3, INDICATOR_MIN_LEN);
  2032. this.indicatorStyle.width = _width + 'px';
  2033. x = this.maxPosX + this.indicatorWidth - _width;
  2034. } else {
  2035. this.indicatorStyle.width = this.indicatorWidth + 'px';
  2036. }
  2037. this.x = x;
  2038. if (this.scroller.options.useTransform) {
  2039. this.indicatorStyle[style.transform] = 'translateX(' + x + 'px)' + this.scroller.translateZ;
  2040. } else {
  2041. this.indicatorStyle.left = x + 'px';
  2042. }
  2043. }
  2044. };
  2045. Indicator.prototype.transitionTime = function () {
  2046. var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  2047. this.indicatorStyle[style.transitionDuration] = time + 'ms';
  2048. };
  2049. Indicator.prototype.transitionTimingFunction = function (easing) {
  2050. this.indicatorStyle[style.transitionTimingFunction] = easing;
  2051. };
  2052. Indicator.prototype.destroy = function () {
  2053. this._removeDOMEvents();
  2054. this.wrapper.parentNode.removeChild(this.wrapper);
  2055. };
  2056. Indicator.prototype._start = function (e) {
  2057. var point = e.touches ? e.touches[0] : e;
  2058. e.preventDefault();
  2059. e.stopPropagation();
  2060. this.transitionTime();
  2061. this.initiated = true;
  2062. this.moved = false;
  2063. this.lastPointX = point.pageX;
  2064. this.lastPointY = point.pageY;
  2065. this.startTime = getNow();
  2066. this._handleMoveEvents(addEvent);
  2067. this.scroller.trigger('beforeScrollStart');
  2068. };
  2069. Indicator.prototype._move = function (e) {
  2070. var point = e.touches ? e.touches[0] : e;
  2071. e.preventDefault();
  2072. e.stopPropagation();
  2073. if (!this.moved) {
  2074. this.scroller.trigger('scrollStart');
  2075. }
  2076. this.moved = true;
  2077. var deltaX = point.pageX - this.lastPointX;
  2078. this.lastPointX = point.pageX;
  2079. var deltaY = point.pageY - this.lastPointY;
  2080. this.lastPointY = point.pageY;
  2081. var newX = this.x + deltaX;
  2082. var newY = this.y + deltaY;
  2083. this._pos(newX, newY);
  2084. };
  2085. Indicator.prototype._end = function (e) {
  2086. if (!this.initiated) {
  2087. return;
  2088. }
  2089. this.initiated = false;
  2090. e.preventDefault();
  2091. e.stopPropagation();
  2092. this._handleMoveEvents(removeEvent);
  2093. var snapOption = this.scroller.options.snap;
  2094. if (snapOption) {
  2095. var speed = snapOption.speed,
  2096. _snapOption$easing = snapOption.easing,
  2097. easing = _snapOption$easing === undefined ? ease.bounce : _snapOption$easing;
  2098. var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
  2099. var time = speed || Math.max(Math.max(Math.min(Math.abs(this.scroller.x - snap.x), 1000), Math.min(Math.abs(this.scroller.y - snap.y), 1000)), 300);
  2100. if (this.scroller.x !== snap.x || this.scroller.y !== snap.y) {
  2101. this.scroller.directionX = 0;
  2102. this.scroller.directionY = 0;
  2103. this.scroller.currentPage = snap;
  2104. this.scroller.scrollTo(snap.x, snap.y, time, easing);
  2105. }
  2106. }
  2107. if (this.moved) {
  2108. this.scroller.trigger('scrollEnd', {
  2109. x: this.scroller.x,
  2110. y: this.scroller.y
  2111. });
  2112. }
  2113. };
  2114. Indicator.prototype._pos = function (x, y) {
  2115. if (x < 0) {
  2116. x = 0;
  2117. } else if (x > this.maxPosX) {
  2118. x = this.maxPosX;
  2119. }
  2120. if (y < 0) {
  2121. y = 0;
  2122. } else if (y > this.maxPosY) {
  2123. y = this.maxPosY;
  2124. }
  2125. x = Math.round(x / this.sizeRatioX);
  2126. y = Math.round(y / this.sizeRatioY);
  2127. this.scroller.scrollTo(x, y);
  2128. this.scroller.trigger('scroll', {
  2129. x: this.scroller.x,
  2130. y: this.scroller.y
  2131. });
  2132. };
  2133. Indicator.prototype._shouldShow = function () {
  2134. if (this.direction === 'vertical' && this.scroller.hasVerticalScroll || this.direction === 'horizontal' && this.scroller.hasHorizontalScroll) {
  2135. this.wrapper.style.display = '';
  2136. return true;
  2137. }
  2138. this.wrapper.style.display = 'none';
  2139. return false;
  2140. };
  2141. Indicator.prototype._calculate = function () {
  2142. if (this.direction === 'vertical') {
  2143. var wrapperHeight = this.wrapper.clientHeight;
  2144. this.indicatorHeight = Math.max(Math.round(wrapperHeight * wrapperHeight / (this.scroller.scrollerHeight || wrapperHeight || 1)), INDICATOR_MIN_LEN);
  2145. this.indicatorStyle.height = this.indicatorHeight + 'px';
  2146. this.maxPosY = wrapperHeight - this.indicatorHeight;
  2147. this.sizeRatioY = this.maxPosY / this.scroller.maxScrollY;
  2148. } else {
  2149. var wrapperWidth = this.wrapper.clientWidth;
  2150. this.indicatorWidth = Math.max(Math.round(wrapperWidth * wrapperWidth / (this.scroller.scrollerWidth || wrapperWidth || 1)), INDICATOR_MIN_LEN);
  2151. this.indicatorStyle.width = this.indicatorWidth + 'px';
  2152. this.maxPosX = wrapperWidth - this.indicatorWidth;
  2153. this.sizeRatioX = this.maxPosX / this.scroller.maxScrollX;
  2154. }
  2155. };
  2156. Indicator.prototype._addDOMEvents = function () {
  2157. var eventOperation = addEvent;
  2158. this._handleDOMEvents(eventOperation);
  2159. };
  2160. Indicator.prototype._removeDOMEvents = function () {
  2161. var eventOperation = removeEvent;
  2162. this._handleDOMEvents(eventOperation);
  2163. this._handleMoveEvents(eventOperation);
  2164. };
  2165. Indicator.prototype._handleMoveEvents = function (eventOperation) {
  2166. if (!this.scroller.options.disableTouch) {
  2167. eventOperation(window, 'touchmove', this);
  2168. }
  2169. if (!this.scroller.options.disableMouse) {
  2170. eventOperation(window, 'mousemove', this);
  2171. }
  2172. };
  2173. Indicator.prototype._handleDOMEvents = function (eventOperation) {
  2174. if (!this.scroller.options.disableTouch) {
  2175. eventOperation(this.indicator, 'touchstart', this);
  2176. eventOperation(window, 'touchend', this);
  2177. }
  2178. if (!this.scroller.options.disableMouse) {
  2179. eventOperation(this.indicator, 'mousedown', this);
  2180. eventOperation(window, 'mouseup', this);
  2181. }
  2182. };
  2183. function pullDownMixin(BScroll) {
  2184. BScroll.prototype._initPullDown = function () {
  2185. // must watch scroll in real time
  2186. this.options.probeType = PROBE_REALTIME;
  2187. };
  2188. BScroll.prototype._checkPullDown = function () {
  2189. var _options$pullDownRefr = this.options.pullDownRefresh,
  2190. _options$pullDownRefr2 = _options$pullDownRefr.threshold,
  2191. threshold = _options$pullDownRefr2 === undefined ? 90 : _options$pullDownRefr2,
  2192. _options$pullDownRefr3 = _options$pullDownRefr.stop,
  2193. stop = _options$pullDownRefr3 === undefined ? 40 : _options$pullDownRefr3;
  2194. // check if a real pull down action
  2195. if (this.directionY !== DIRECTION_DOWN || this.y < threshold) {
  2196. return false;
  2197. }
  2198. if (!this.pulling) {
  2199. this.pulling = true;
  2200. this.trigger('pullingDown');
  2201. }
  2202. this.scrollTo(this.x, stop, this.options.bounceTime, ease.bounce);
  2203. return this.pulling;
  2204. };
  2205. BScroll.prototype.finishPullDown = function () {
  2206. this.pulling = false;
  2207. this.resetPosition(this.options.bounceTime, ease.bounce);
  2208. };
  2209. BScroll.prototype.openPullDown = function () {
  2210. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  2211. this.options.pullDownRefresh = config;
  2212. this._initPullDown();
  2213. };
  2214. BScroll.prototype.closePullDown = function () {
  2215. this.options.pullDownRefresh = false;
  2216. };
  2217. BScroll.prototype.autoPullDownRefresh = function () {
  2218. var _options$pullDownRefr4 = this.options.pullDownRefresh,
  2219. _options$pullDownRefr5 = _options$pullDownRefr4.threshold,
  2220. threshold = _options$pullDownRefr5 === undefined ? 90 : _options$pullDownRefr5,
  2221. _options$pullDownRefr6 = _options$pullDownRefr4.stop,
  2222. stop = _options$pullDownRefr6 === undefined ? 40 : _options$pullDownRefr6;
  2223. if (this.pulling) {
  2224. return;
  2225. }
  2226. this.pulling = true;
  2227. this.scrollTo(this.x, threshold);
  2228. this.trigger('pullingDown');
  2229. this.scrollTo(this.x, stop, this.options.bounceTime, ease.bounce);
  2230. };
  2231. }
  2232. function pullUpMixin(BScroll) {
  2233. BScroll.prototype._initPullUp = function () {
  2234. // must watch scroll in real time
  2235. this.options.probeType = PROBE_REALTIME;
  2236. this.pullupWatching = false;
  2237. this._watchPullUp();
  2238. };
  2239. BScroll.prototype._watchPullUp = function () {
  2240. if (this.pullupWatching) {
  2241. return;
  2242. }
  2243. this.pullupWatching = true;
  2244. this.on('scroll', this._checkToEnd);
  2245. };
  2246. BScroll.prototype._checkToEnd = function (pos) {
  2247. var _this = this;
  2248. var _options$pullUpLoad$t = this.options.pullUpLoad.threshold,
  2249. threshold = _options$pullUpLoad$t === undefined ? 0 : _options$pullUpLoad$t;
  2250. if (this.movingDirectionY === DIRECTION_UP && pos.y <= this.maxScrollY + threshold) {
  2251. // reset pullupWatching status after scroll end.
  2252. this.once('scrollEnd', function () {
  2253. _this.pullupWatching = false;
  2254. });
  2255. this.trigger('pullingUp');
  2256. this.off('scroll', this._checkToEnd);
  2257. }
  2258. };
  2259. BScroll.prototype.finishPullUp = function () {
  2260. var _this2 = this;
  2261. if (this.pullupWatching) {
  2262. this.once('scrollEnd', function () {
  2263. _this2._watchPullUp();
  2264. });
  2265. } else {
  2266. this._watchPullUp();
  2267. }
  2268. };
  2269. BScroll.prototype.openPullUp = function () {
  2270. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  2271. this.options.pullUpLoad = config;
  2272. this._initPullUp();
  2273. };
  2274. BScroll.prototype.closePullUp = function () {
  2275. this.options.pullUpLoad = false;
  2276. if (!this.pullupWatching) {
  2277. return;
  2278. }
  2279. this.pullupWatching = false;
  2280. this.off('scroll', this._checkToEnd);
  2281. };
  2282. }
  2283. function mouseWheelMixin(BScroll) {
  2284. BScroll.prototype._initMouseWheel = function () {
  2285. var _this = this;
  2286. this._handleMouseWheelEvent(addEvent);
  2287. this.on('destroy', function () {
  2288. clearTimeout(_this.mouseWheelTimer);
  2289. clearTimeout(_this.mouseWheelEndTimer);
  2290. _this._handleMouseWheelEvent(removeEvent);
  2291. });
  2292. this.firstWheelOpreation = true;
  2293. };
  2294. BScroll.prototype._handleMouseWheelEvent = function (eventOperation) {
  2295. eventOperation(this.wrapper, 'wheel', this);
  2296. eventOperation(this.wrapper, 'mousewheel', this);
  2297. eventOperation(this.wrapper, 'DOMMouseScroll', this);
  2298. };
  2299. BScroll.prototype._onMouseWheel = function (e) {
  2300. var _this2 = this;
  2301. if (!this.enabled) {
  2302. return;
  2303. }
  2304. e.preventDefault();
  2305. if (this.options.stopPropagation) {
  2306. e.stopPropagation();
  2307. }
  2308. if (this.firstWheelOpreation) {
  2309. this.trigger('scrollStart');
  2310. }
  2311. this.firstWheelOpreation = false;
  2312. var _options$mouseWheel = this.options.mouseWheel,
  2313. _options$mouseWheel$s = _options$mouseWheel.speed,
  2314. speed = _options$mouseWheel$s === undefined ? 20 : _options$mouseWheel$s,
  2315. _options$mouseWheel$i = _options$mouseWheel.invert,
  2316. invert = _options$mouseWheel$i === undefined ? false : _options$mouseWheel$i,
  2317. _options$mouseWheel$e = _options$mouseWheel.easeTime,
  2318. easeTime = _options$mouseWheel$e === undefined ? 300 : _options$mouseWheel$e;
  2319. clearTimeout(this.mouseWheelTimer);
  2320. this.mouseWheelTimer = setTimeout(function () {
  2321. if (!_this2.options.snap && !easeTime) {
  2322. _this2.trigger('scrollEnd', {
  2323. x: _this2.x,
  2324. y: _this2.y
  2325. });
  2326. }
  2327. _this2.firstWheelOpreation = true;
  2328. }, 400);
  2329. var wheelDeltaX = void 0;
  2330. var wheelDeltaY = void 0;
  2331. switch (true) {
  2332. case 'deltaX' in e:
  2333. if (e.deltaMode === 1) {
  2334. wheelDeltaX = -e.deltaX * speed;
  2335. wheelDeltaY = -e.deltaY * speed;
  2336. } else {
  2337. wheelDeltaX = -e.deltaX;
  2338. wheelDeltaY = -e.deltaY;
  2339. }
  2340. break;
  2341. case 'wheelDeltaX' in e:
  2342. wheelDeltaX = e.wheelDeltaX / 120 * speed;
  2343. wheelDeltaY = e.wheelDeltaY / 120 * speed;
  2344. break;
  2345. case 'wheelDelta' in e:
  2346. wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * speed;
  2347. break;
  2348. case 'detail' in e:
  2349. wheelDeltaX = wheelDeltaY = -e.detail / 3 * speed;
  2350. break;
  2351. default:
  2352. return;
  2353. }
  2354. var direction = invert ? -1 : 1;
  2355. wheelDeltaX *= direction;
  2356. wheelDeltaY *= direction;
  2357. if (!this.hasVerticalScroll) {
  2358. wheelDeltaX = wheelDeltaY;
  2359. wheelDeltaY = 0;
  2360. }
  2361. var newX = void 0;
  2362. var newY = void 0;
  2363. if (this.options.snap) {
  2364. newX = this.currentPage.pageX;
  2365. newY = this.currentPage.pageY;
  2366. if (wheelDeltaX > 0) {
  2367. newX--;
  2368. } else if (wheelDeltaX < 0) {
  2369. newX++;
  2370. }
  2371. if (wheelDeltaY > 0) {
  2372. newY--;
  2373. } else if (wheelDeltaY < 0) {
  2374. newY++;
  2375. }
  2376. this._goToPage(newX, newY);
  2377. return;
  2378. }
  2379. newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
  2380. newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
  2381. this.movingDirectionX = this.directionX = wheelDeltaX > 0 ? -1 : wheelDeltaX < 0 ? 1 : 0;
  2382. this.movingDirectionY = this.directionY = wheelDeltaY > 0 ? -1 : wheelDeltaY < 0 ? 1 : 0;
  2383. if (newX > this.minScrollX) {
  2384. newX = this.minScrollX;
  2385. } else if (newX < this.maxScrollX) {
  2386. newX = this.maxScrollX;
  2387. }
  2388. if (newY > this.minScrollY) {
  2389. newY = this.minScrollY;
  2390. } else if (newY < this.maxScrollY) {
  2391. newY = this.maxScrollY;
  2392. }
  2393. var needTriggerEnd = this.y === newY;
  2394. this.scrollTo(newX, newY, easeTime, ease.swipe);
  2395. this.trigger('scroll', {
  2396. x: this.x,
  2397. y: this.y
  2398. });
  2399. clearTimeout(this.mouseWheelEndTimer);
  2400. if (needTriggerEnd) {
  2401. this.mouseWheelEndTimer = setTimeout(function () {
  2402. _this2.trigger('scrollEnd', {
  2403. x: _this2.x,
  2404. y: _this2.y
  2405. });
  2406. }, easeTime);
  2407. }
  2408. };
  2409. }
  2410. function zoomMixin(BScroll) {
  2411. BScroll.prototype._initZoom = function () {
  2412. var _options$zoom = this.options.zoom,
  2413. _options$zoom$start = _options$zoom.start,
  2414. start = _options$zoom$start === undefined ? 1 : _options$zoom$start,
  2415. _options$zoom$min = _options$zoom.min,
  2416. min = _options$zoom$min === undefined ? 1 : _options$zoom$min,
  2417. _options$zoom$max = _options$zoom.max,
  2418. max = _options$zoom$max === undefined ? 4 : _options$zoom$max;
  2419. this.scale = Math.min(Math.max(start, min), max);
  2420. this.setScale(this.scale);
  2421. this.scrollerStyle[style.transformOrigin] = '0 0';
  2422. };
  2423. BScroll.prototype._zoomTo = function (scale, originX, originY, startScale) {
  2424. this.scaled = true;
  2425. var lastScale = scale / (startScale || this.scale);
  2426. this.setScale(scale);
  2427. this.refresh();
  2428. var newX = Math.round(this.startX - (originX - this.relativeX) * (lastScale - 1));
  2429. var newY = Math.round(this.startY - (originY - this.relativeY) * (lastScale - 1));
  2430. if (newX > this.minScrollX) {
  2431. newX = this.minScrollX;
  2432. } else if (newX < this.maxScrollX) {
  2433. newX = this.maxScrollX;
  2434. }
  2435. if (newY > this.minScrollY) {
  2436. newY = this.minScrollY;
  2437. } else if (newY < this.maxScrollY) {
  2438. newY = this.maxScrollY;
  2439. }
  2440. if (this.x !== newX || this.y !== newY) {
  2441. this.scrollTo(newX, newY, this.options.bounceTime);
  2442. }
  2443. this.scaled = false;
  2444. };
  2445. BScroll.prototype.zoomTo = function (scale, x, y) {
  2446. var _offsetToBody = offsetToBody(this.wrapper),
  2447. left = _offsetToBody.left,
  2448. top = _offsetToBody.top;
  2449. var originX = x + left - this.x;
  2450. var originY = y + top - this.y;
  2451. this._zoomTo(scale, originX, originY);
  2452. };
  2453. BScroll.prototype._zoomStart = function (e) {
  2454. var firstFinger = e.touches[0];
  2455. var secondFinger = e.touches[1];
  2456. var deltaX = Math.abs(firstFinger.pageX - secondFinger.pageX);
  2457. var deltaY = Math.abs(firstFinger.pageY - secondFinger.pageY);
  2458. this.startDistance = getDistance(deltaX, deltaY);
  2459. this.startScale = this.scale;
  2460. var _offsetToBody2 = offsetToBody(this.wrapper),
  2461. left = _offsetToBody2.left,
  2462. top = _offsetToBody2.top;
  2463. this.originX = Math.abs(firstFinger.pageX + secondFinger.pageX) / 2 + left - this.x;
  2464. this.originY = Math.abs(firstFinger.pageY + secondFinger.pageY) / 2 + top - this.y;
  2465. this.trigger('zoomStart');
  2466. };
  2467. BScroll.prototype._zoom = function (e) {
  2468. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  2469. return;
  2470. }
  2471. if (this.options.preventDefault) {
  2472. e.preventDefault();
  2473. }
  2474. if (this.options.stopPropagation) {
  2475. e.stopPropagation();
  2476. }
  2477. var firstFinger = e.touches[0];
  2478. var secondFinger = e.touches[1];
  2479. var deltaX = Math.abs(firstFinger.pageX - secondFinger.pageX);
  2480. var deltaY = Math.abs(firstFinger.pageY - secondFinger.pageY);
  2481. var distance = getDistance(deltaX, deltaY);
  2482. var scale = distance / this.startDistance * this.startScale;
  2483. this.scaled = true;
  2484. var _options$zoom2 = this.options.zoom,
  2485. _options$zoom2$min = _options$zoom2.min,
  2486. min = _options$zoom2$min === undefined ? 1 : _options$zoom2$min,
  2487. _options$zoom2$max = _options$zoom2.max,
  2488. max = _options$zoom2$max === undefined ? 4 : _options$zoom2$max;
  2489. if (scale < min) {
  2490. scale = 0.5 * min * Math.pow(2.0, scale / min);
  2491. } else if (scale > max) {
  2492. scale = 2.0 * max * Math.pow(0.5, max / scale);
  2493. }
  2494. var lastScale = scale / this.startScale;
  2495. var x = this.startX - (this.originX - this.relativeX) * (lastScale - 1);
  2496. var y = this.startY - (this.originY - this.relativeY) * (lastScale - 1);
  2497. this.setScale(scale);
  2498. this.scrollTo(x, y, 0);
  2499. };
  2500. BScroll.prototype._zoomEnd = function (e) {
  2501. if (!this.enabled || this.destroyed || eventType[e.type] !== this.initiated) {
  2502. return;
  2503. }
  2504. if (this.options.preventDefault) {
  2505. e.preventDefault();
  2506. }
  2507. if (this.options.stopPropagation) {
  2508. e.stopPropagation();
  2509. }
  2510. this.isInTransition = false;
  2511. this.isAnimating = false;
  2512. this.initiated = 0;
  2513. var _options$zoom3 = this.options.zoom,
  2514. _options$zoom3$min = _options$zoom3.min,
  2515. min = _options$zoom3$min === undefined ? 1 : _options$zoom3$min,
  2516. _options$zoom3$max = _options$zoom3.max,
  2517. max = _options$zoom3$max === undefined ? 4 : _options$zoom3$max;
  2518. var scale = this.scale > max ? max : this.scale < min ? min : this.scale;
  2519. this._zoomTo(scale, this.originX, this.originY, this.startScale);
  2520. this.trigger('zoomEnd');
  2521. };
  2522. }
  2523. // import { ease } from '../util/ease'
  2524. // Number of items to instantiate beyond current view in the scroll direction.
  2525. var RUNWAY_ITEMS = 30;
  2526. // Number of items to instantiate beyond current view in the opposite direction.
  2527. var RUNWAY_ITEMS_OPPOSITE = 10;
  2528. // The animation interval (in ms) for fading in content from tombstones.
  2529. var ANIMATION_DURATION_MS = 200;
  2530. // The number of pixels of default additional length to allow scrolling to.
  2531. var DEFAULT_SCROLL_RUNWAY = 2000;
  2532. function infiniteMixin(BScroll) {
  2533. BScroll.prototype._initInfinite = function () {
  2534. this.options.probeType = 3;
  2535. this.maxScrollY = -DEFAULT_SCROLL_RUNWAY;
  2536. this.infiniteScroller = new InfiniteScroller(this, this.options.infinity);
  2537. };
  2538. }
  2539. function isTombstoneNode(node) {
  2540. if (node && node.classList) {
  2541. return node.classList.contains('tombstone');
  2542. }
  2543. }
  2544. function InfiniteScroller(scroller, options) {
  2545. var _this = this;
  2546. this.options = options;
  2547. assert(typeof this.options.createTombstone === 'function', 'Infinite scroll need createTombstone Function to create tombstone');
  2548. assert(typeof this.options.fetch === 'function', 'Infinite scroll need fetch Function to fetch new data.');
  2549. assert(typeof this.options.render === 'function', 'Infinite scroll need render Function to render each item.');
  2550. this.firstAttachedItem = 0;
  2551. this.lastAttachedItem = 0;
  2552. this.anchorScrollTop = 0;
  2553. this.anchorItem = {
  2554. index: 0,
  2555. offset: 0
  2556. };
  2557. this.tombstoneHeight = 0;
  2558. this.tombstoneWidth = 0;
  2559. this.tombstones = [];
  2560. this.tombstonesAnimationHandlers = [];
  2561. this.items = [];
  2562. this.loadedItems = 0;
  2563. this.requestInProgress = false;
  2564. this.hasMore = true;
  2565. this.scroller = scroller;
  2566. this.wrapperEl = this.scroller.wrapper;
  2567. this.scrollerEl = this.scroller.scroller;
  2568. this.scroller.on('resize', function () {
  2569. _this.onResize();
  2570. });
  2571. this.scroller.on('destroy', function () {
  2572. _this.destroy();
  2573. });
  2574. // wait scroll core init
  2575. this._onResizeHandler = setTimeout(function () {
  2576. _this.onResize();
  2577. // must wait tombstoneHeight has size
  2578. _this.scroller.on('scroll', function () {
  2579. _this.onScroll();
  2580. });
  2581. });
  2582. }
  2583. InfiniteScroller.prototype.destroy = function () {
  2584. var _this2 = this;
  2585. // In extreme scene, destroy is triggered before _onResizeHandler
  2586. clearTimeout(this._onResizeHandler);
  2587. this.tombstonesAnimationHandlers.forEach(function (handler) {
  2588. clearTimeout(handler);
  2589. });
  2590. this.tombstonesAnimationHandlers = null;
  2591. this.items.forEach(function (item) {
  2592. if (item.node) {
  2593. _this2.scrollerEl.removeChild(item.node);
  2594. item.node = null;
  2595. }
  2596. });
  2597. this.scroller.infiniteScroller = null;
  2598. this.scroller = null;
  2599. this.wrapperEl = null;
  2600. this.scrollerEl = null;
  2601. this.items = null;
  2602. this.tombstones = null;
  2603. };
  2604. InfiniteScroller.prototype.onScroll = function () {
  2605. var scrollTop = -this.scroller.y;
  2606. var delta = scrollTop - this.anchorScrollTop;
  2607. if (scrollTop === 0) {
  2608. this.anchorItem = {
  2609. index: 0,
  2610. offset: 0
  2611. };
  2612. } else {
  2613. this.anchorItem = this._calculateAnchoredItem(this.anchorItem, delta);
  2614. }
  2615. this.anchorScrollTop = scrollTop;
  2616. var lastScreenItem = this._calculateAnchoredItem(this.anchorItem, this.scroller.wrapperHeight);
  2617. var start = this.anchorItem.index;
  2618. var end = lastScreenItem.index;
  2619. if (delta < 0) {
  2620. start -= RUNWAY_ITEMS;
  2621. end += RUNWAY_ITEMS_OPPOSITE;
  2622. } else {
  2623. start -= RUNWAY_ITEMS_OPPOSITE;
  2624. end += RUNWAY_ITEMS;
  2625. }
  2626. this.fill(start, end);
  2627. this.maybeRequestContent();
  2628. };
  2629. InfiniteScroller.prototype.onResize = function () {
  2630. var tombstone = this.options.createTombstone();
  2631. tombstone.style.position = 'absolute';
  2632. this.scrollerEl.appendChild(tombstone);
  2633. tombstone.style.display = '';
  2634. this.tombstoneHeight = tombstone.offsetHeight;
  2635. this.tombstoneWidth = tombstone.offsetWidth;
  2636. this.scrollerEl.removeChild(tombstone);
  2637. for (var i = 0; i < this.items.length; i++) {
  2638. this.items[i].height = this.items[i].width = 0;
  2639. }
  2640. this.onScroll();
  2641. };
  2642. InfiniteScroller.prototype.fill = function (start, end) {
  2643. this.firstAttachedItem = Math.max(0, start);
  2644. if (!this.hasMore) {
  2645. end = Math.min(end, this.items.length);
  2646. }
  2647. this.lastAttachedItem = end;
  2648. this.attachContent();
  2649. };
  2650. InfiniteScroller.prototype.maybeRequestContent = function () {
  2651. var _this3 = this;
  2652. if (this.requestInProgress || !this.hasMore) {
  2653. return;
  2654. }
  2655. var itemsNeeded = this.lastAttachedItem - this.loadedItems;
  2656. if (itemsNeeded <= 0) {
  2657. return;
  2658. }
  2659. this.requestInProgress = true;
  2660. this.options.fetch(itemsNeeded).then(function (items) {
  2661. _this3.requestInProgress = false;
  2662. if (items) {
  2663. _this3.addContent(items);
  2664. } else {
  2665. _this3.hasMore = false;
  2666. var tombstoneLen = _this3._removeTombstones();
  2667. var curPos = 0;
  2668. if (_this3.anchorItem.index <= _this3.items.length) {
  2669. curPos = _this3._fixScrollPosition();
  2670. _this3._setupAnimations({}, curPos);
  2671. _this3.scroller.resetPosition(_this3.scroller.options.bounceTime);
  2672. } else {
  2673. _this3.anchorItem.index -= tombstoneLen;
  2674. curPos = _this3._fixScrollPosition();
  2675. _this3._setupAnimations({}, curPos);
  2676. _this3.scroller.stop();
  2677. _this3.scroller.resetPosition();
  2678. _this3.onScroll();
  2679. }
  2680. }
  2681. });
  2682. };
  2683. InfiniteScroller.prototype.addContent = function (items) {
  2684. for (var i = 0; i < items.length; i++) {
  2685. if (this.items.length <= this.loadedItems) {
  2686. this._addItem();
  2687. }
  2688. this.items[this.loadedItems++].data = items[i];
  2689. }
  2690. this.attachContent();
  2691. this.maybeRequestContent();
  2692. };
  2693. InfiniteScroller.prototype.attachContent = function () {
  2694. var unusedNodes = this._collectUnusedNodes();
  2695. var tombstoneAnimations = this._createDOMNodes(unusedNodes);
  2696. this._cleanupUnusedNodes(unusedNodes);
  2697. this._cacheNodeSize();
  2698. var curPos = this._fixScrollPosition();
  2699. this._setupAnimations(tombstoneAnimations, curPos);
  2700. };
  2701. InfiniteScroller.prototype.resetMore = function () {
  2702. this.hasMore = true;
  2703. };
  2704. InfiniteScroller.prototype._removeTombstones = function () {
  2705. var markIndex = void 0;
  2706. var tombstoneLen = 0;
  2707. var itemLen = this.items.length;
  2708. for (var i = 0; i < itemLen; i++) {
  2709. var currentNode = this.items[i].node;
  2710. var currentData = this.items[i].data;
  2711. if ((!currentNode || isTombstoneNode(currentNode)) && !currentData) {
  2712. // 0 should be excluded
  2713. if (markIndex === void 0) {
  2714. markIndex = i;
  2715. }
  2716. if (currentNode) {
  2717. this.scrollerEl.removeChild(currentNode);
  2718. }
  2719. }
  2720. }
  2721. tombstoneLen = itemLen - markIndex;
  2722. this.items.splice(markIndex);
  2723. this.lastAttachedItem = Math.min(this.lastAttachedItem, this.items.length);
  2724. return tombstoneLen;
  2725. };
  2726. InfiniteScroller.prototype._collectUnusedNodes = function () {
  2727. var unusedNodes = [];
  2728. for (var i = 0; i < this.items.length; i++) {
  2729. // Skip the items which should be visible.
  2730. if (i === this.firstAttachedItem) {
  2731. i = this.lastAttachedItem - 1;
  2732. continue;
  2733. }
  2734. var currentNode = this.items[i].node;
  2735. if (currentNode) {
  2736. if (isTombstoneNode(currentNode)) {
  2737. // Cache tombstones for reuse
  2738. this.tombstones.push(currentNode);
  2739. this.tombstones[this.tombstones.length - 1].style.display = 'none';
  2740. } else {
  2741. unusedNodes.push(currentNode);
  2742. }
  2743. }
  2744. this.items[i].node = null;
  2745. }
  2746. return unusedNodes;
  2747. };
  2748. InfiniteScroller.prototype._createDOMNodes = function (unusedNodes) {
  2749. var tombstoneAnimations = {};
  2750. for (var i = this.firstAttachedItem; i < this.lastAttachedItem; i++) {
  2751. while (this.items.length <= i) {
  2752. this._addItem();
  2753. }
  2754. var currentNode = this.items[i].node;
  2755. var currentData = this.items[i].data;
  2756. if (currentNode) {
  2757. if (isTombstoneNode(currentNode) && currentData) {
  2758. currentNode.style.zIndex = 1;
  2759. tombstoneAnimations[i] = [currentNode, this.items[i].top - this.anchorScrollTop];
  2760. this.items[i].node = null;
  2761. } else {
  2762. continue;
  2763. }
  2764. }
  2765. var node = currentData ? this.options.render(currentData, unusedNodes.pop()) : this._getTombStone();
  2766. node.style.position = 'absolute';
  2767. this.items[i].top = -1;
  2768. this.scrollerEl.appendChild(node);
  2769. this.items[i].node = node;
  2770. }
  2771. return tombstoneAnimations;
  2772. };
  2773. InfiniteScroller.prototype._cleanupUnusedNodes = function (unusedNodes) {
  2774. while (unusedNodes.length) {
  2775. this.scrollerEl.removeChild(unusedNodes.pop());
  2776. }
  2777. };
  2778. InfiniteScroller.prototype._cacheNodeSize = function () {
  2779. for (var i = this.firstAttachedItem; i < this.lastAttachedItem; i++) {
  2780. var item = this.items[i];
  2781. // Only cache the height if we have the real contents, not a placeholder.
  2782. if (item.data && !item.height) {
  2783. var isTombstone = isTombstoneNode(item.node);
  2784. item.height = isTombstone ? this.tombstoneHeight : item.node.offsetHeight;
  2785. item.width = isTombstone ? this.tombstoneWidth : item.node.offsetWidth;
  2786. }
  2787. }
  2788. };
  2789. InfiniteScroller.prototype._fixScrollPosition = function () {
  2790. this.anchorScrollTop = 0;
  2791. for (var _i = 0; _i < this.anchorItem.index; _i++) {
  2792. this.anchorScrollTop += this.items[_i].height || this.tombstoneHeight;
  2793. }
  2794. this.anchorScrollTop += this.anchorItem.offset;
  2795. // Position all nodes.
  2796. var curPos = this.anchorScrollTop - this.anchorItem.offset;
  2797. var i = this.anchorItem.index;
  2798. while (i > this.firstAttachedItem) {
  2799. curPos -= this.items[i - 1].height || this.tombstoneHeight;
  2800. i--;
  2801. }
  2802. return curPos;
  2803. };
  2804. InfiniteScroller.prototype._setupAnimations = function (tombstoneAnimations, curPos) {
  2805. var _this4 = this;
  2806. for (var i in tombstoneAnimations) {
  2807. var animation = tombstoneAnimations[i];
  2808. this.items[i].node.style[style.transform] = 'translateY(' + (this.anchorScrollTop + animation[1]) + 'px) scale(' + this.tombstoneWidth / this.items[i].width + ', ' + this.tombstoneHeight / this.items[i].height + ')';
  2809. // Call offsetTop on the nodes to be animated to force them to apply current transforms.
  2810. /* eslint-disable no-unused-expressions */
  2811. this.items[i].node.offsetTop;
  2812. animation[0].offsetTop;
  2813. this.items[i].node.style[style.transition] = cssVendor + 'transform ' + ANIMATION_DURATION_MS + 'ms';
  2814. }
  2815. for (var _i2 = this.firstAttachedItem; _i2 < this.lastAttachedItem; _i2++) {
  2816. var _animation = tombstoneAnimations[_i2];
  2817. if (_animation) {
  2818. var tombstoneNode = _animation[0];
  2819. tombstoneNode.style[style.transition] = cssVendor + 'transform ' + ANIMATION_DURATION_MS + 'ms, opacity ' + ANIMATION_DURATION_MS + 'ms';
  2820. tombstoneNode.style[style.transform] = 'translateY(' + curPos + 'px) scale(' + this.items[_i2].width / this.tombstoneWidth + ', ' + this.items[_i2].height / this.tombstoneHeight + ')';
  2821. tombstoneNode.style.opacity = 0;
  2822. }
  2823. if (curPos !== this.items[_i2].top) {
  2824. if (!_animation) {
  2825. this.items[_i2].node.style[style.transition] = '';
  2826. }
  2827. this.items[_i2].node.style[style.transform] = 'translateY(' + curPos + 'px)';
  2828. }
  2829. this.items[_i2].top = curPos;
  2830. curPos += this.items[_i2].height || this.tombstoneHeight;
  2831. }
  2832. this.scroller.maxScrollY = -(curPos - this.scroller.wrapperHeight + (this.hasMore ? DEFAULT_SCROLL_RUNWAY : 0));
  2833. var tombstoneAnimationsHandler = setTimeout(function () {
  2834. for (var _i3 in tombstoneAnimations) {
  2835. var _animation2 = tombstoneAnimations[_i3];
  2836. _animation2[0].style.display = 'none';
  2837. // Tombstone can be recycled now.
  2838. _this4.tombstones.push(_animation2[0]);
  2839. }
  2840. }, ANIMATION_DURATION_MS);
  2841. this.tombstonesAnimationHandlers.push(tombstoneAnimationsHandler);
  2842. };
  2843. InfiniteScroller.prototype._getTombStone = function () {
  2844. var tombstone = this.tombstones.pop();
  2845. if (tombstone) {
  2846. tombstone.style.display = '';
  2847. tombstone.style.opacity = 1;
  2848. tombstone.style[style.transform] = '';
  2849. tombstone.style[style.transition] = '';
  2850. return tombstone;
  2851. }
  2852. return this.options.createTombstone();
  2853. };
  2854. InfiniteScroller.prototype._addItem = function () {
  2855. this.items.push({
  2856. data: null,
  2857. node: null,
  2858. height: 0,
  2859. width: 0,
  2860. top: 0
  2861. });
  2862. };
  2863. InfiniteScroller.prototype._calculateAnchoredItem = function (initialAnchor, delta) {
  2864. if (delta === 0) {
  2865. return initialAnchor;
  2866. }
  2867. var i = initialAnchor.index;
  2868. var tombstones = 0;
  2869. delta += initialAnchor.offset;
  2870. if (delta < 0) {
  2871. while (delta < 0 && i > 0 && this.items[i - 1].height) {
  2872. delta += this.items[i - 1].height;
  2873. i--;
  2874. }
  2875. tombstones = Math.max(-i, Math.ceil(Math.min(delta, 0) / this.tombstoneHeight));
  2876. } else {
  2877. while (delta > 0 && i < this.items.length && this.items[i].height && this.items[i].height < delta) {
  2878. delta -= this.items[i].height;
  2879. i++;
  2880. }
  2881. if (i >= this.items.length || !this.items[i].height) {
  2882. tombstones = Math.floor(Math.max(delta, 0) / this.tombstoneHeight);
  2883. }
  2884. }
  2885. i += tombstones;
  2886. delta -= tombstones * this.tombstoneHeight;
  2887. return {
  2888. index: i,
  2889. offset: delta
  2890. };
  2891. };
  2892. function BScroll(el, options) {
  2893. this.wrapper = typeof el === 'string' ? document.querySelector(el) : el;
  2894. if (!this.wrapper) {
  2895. warn('Can not resolve the wrapper DOM.');
  2896. }
  2897. this.scroller = this.wrapper.children[0];
  2898. if (!this.scroller) {
  2899. warn('The wrapper need at least one child element to be scroller.');
  2900. }
  2901. // cache style for better performance
  2902. this.scrollerStyle = this.scroller.style;
  2903. this._init(options);
  2904. }
  2905. initMixin(BScroll);
  2906. coreMixin(BScroll);
  2907. eventMixin(BScroll);
  2908. snapMixin(BScroll);
  2909. wheelMixin(BScroll);
  2910. scrollbarMixin(BScroll);
  2911. pullDownMixin(BScroll);
  2912. pullUpMixin(BScroll);
  2913. mouseWheelMixin(BScroll);
  2914. zoomMixin(BScroll);
  2915. infiniteMixin(BScroll);
  2916. BScroll.Version = '1.15.2';
  2917. export default BScroll;