mui.class.scroll.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. (function($, window, document, undefined) {
  2. var CLASS_SCROLL = $.className('scroll');
  3. var CLASS_SCROLLBAR = $.className('scrollbar');
  4. var CLASS_INDICATOR = $.className('scrollbar-indicator');
  5. var CLASS_SCROLLBAR_VERTICAL = CLASS_SCROLLBAR + '-vertical';
  6. var CLASS_SCROLLBAR_HORIZONTAL = CLASS_SCROLLBAR + '-horizontal';
  7. var CLASS_ACTIVE = $.className('active');
  8. var ease = {
  9. quadratic: {
  10. style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
  11. fn: function(k) {
  12. return k * (2 - k);
  13. }
  14. },
  15. circular: {
  16. style: 'cubic-bezier(0.1, 0.57, 0.1, 1)',
  17. fn: function(k) {
  18. return Math.sqrt(1 - (--k * k));
  19. }
  20. },
  21. outCirc: {
  22. style: 'cubic-bezier(0.075, 0.82, 0.165, 1)'
  23. },
  24. outCubic: {
  25. style: 'cubic-bezier(0.165, 0.84, 0.44, 1)'
  26. }
  27. }
  28. var Scroll = $.Class.extend({
  29. init: function(element, options) {
  30. this.wrapper = this.element = element;
  31. this.scroller = this.wrapper.children[0];
  32. this.scrollerStyle = this.scroller && this.scroller.style;
  33. this.stopped = false;
  34. this.options = $.extend(true, {
  35. scrollY: true, //是否竖向滚动
  36. scrollX: false, //是否横向滚动
  37. startX: 0, //初始化时滚动至x
  38. startY: 0, //初始化时滚动至y
  39. indicators: true, //是否显示滚动条
  40. stopPropagation: false,
  41. hardwareAccelerated: true,
  42. fixedBadAndorid: false,
  43. preventDefaultException: {
  44. tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT|VIDEO)$/
  45. },
  46. momentum: true,
  47. snapX: 0.5, //横向切换距离(以当前容器宽度为基准)
  48. snap: false, //图片轮播,拖拽式选项卡
  49. bounce: true, //是否启用回弹
  50. bounceTime: 500, //回弹动画时间
  51. bounceEasing: ease.outCirc, //回弹动画曲线
  52. scrollTime: 500,
  53. scrollEasing: ease.outCubic, //轮播动画曲线
  54. directionLockThreshold: 5,
  55. parallaxElement: false, //视差元素
  56. parallaxRatio: 0.5
  57. }, options);
  58. this.x = 0;
  59. this.y = 0;
  60. this.translateZ = this.options.hardwareAccelerated ? ' translateZ(0)' : '';
  61. this._init();
  62. if (this.scroller) {
  63. this.refresh();
  64. // if (this.options.startX !== 0 || this.options.startY !== 0) { //需要判断吗?后续根据实际情况再看看
  65. this.scrollTo(this.options.startX, this.options.startY);
  66. // }
  67. }
  68. },
  69. _init: function() {
  70. this._initParallax();
  71. this._initIndicators();
  72. this._initEvent();
  73. },
  74. _initParallax: function() {
  75. if (this.options.parallaxElement) {
  76. this.parallaxElement = document.querySelector(this.options.parallaxElement);
  77. this.parallaxStyle = this.parallaxElement.style;
  78. this.parallaxHeight = this.parallaxElement.offsetHeight;
  79. this.parallaxImgStyle = this.parallaxElement.querySelector('img').style;
  80. }
  81. },
  82. _initIndicators: function() {
  83. var self = this;
  84. self.indicators = [];
  85. if (!this.options.indicators) {
  86. return;
  87. }
  88. var indicators = [],
  89. indicator;
  90. // Vertical scrollbar
  91. if (self.options.scrollY) {
  92. indicator = {
  93. el: this._createScrollBar(CLASS_SCROLLBAR_VERTICAL),
  94. listenX: false
  95. };
  96. this.wrapper.appendChild(indicator.el);
  97. indicators.push(indicator);
  98. }
  99. // Horizontal scrollbar
  100. if (this.options.scrollX) {
  101. indicator = {
  102. el: this._createScrollBar(CLASS_SCROLLBAR_HORIZONTAL),
  103. listenY: false
  104. };
  105. this.wrapper.appendChild(indicator.el);
  106. indicators.push(indicator);
  107. }
  108. for (var i = indicators.length; i--;) {
  109. this.indicators.push(new Indicator(this, indicators[i]));
  110. }
  111. },
  112. _initSnap: function() {
  113. this.currentPage = {};
  114. this.pages = [];
  115. var snaps = this.snaps;
  116. var length = snaps.length;
  117. var m = 0;
  118. var n = -1;
  119. var x = 0;
  120. var leftX = 0;
  121. var rightX = 0;
  122. var snapX = 0;
  123. for (var i = 0; i < length; i++) {
  124. var snap = snaps[i];
  125. var offsetLeft = snap.offsetLeft;
  126. var offsetWidth = snap.offsetWidth;
  127. if (i === 0 || offsetLeft <= snaps[i - 1].offsetLeft) {
  128. m = 0;
  129. n++;
  130. }
  131. if (!this.pages[m]) {
  132. this.pages[m] = [];
  133. }
  134. x = this._getSnapX(offsetLeft);
  135. snapX = Math.round((offsetWidth) * this.options.snapX);
  136. leftX = x - snapX;
  137. rightX = x - offsetWidth + snapX;
  138. this.pages[m][n] = {
  139. x: x,
  140. leftX: leftX,
  141. rightX: rightX,
  142. pageX: m,
  143. element: snap
  144. }
  145. if (snap.classList.contains(CLASS_ACTIVE)) {
  146. this.currentPage = this.pages[m][0];
  147. }
  148. if (x >= this.maxScrollX) {
  149. m++;
  150. }
  151. }
  152. this.options.startX = this.currentPage.x || 0;
  153. },
  154. _getSnapX: function(offsetLeft) {
  155. return Math.max(Math.min(0, -offsetLeft + (this.wrapperWidth / 2)), this.maxScrollX);
  156. },
  157. _gotoPage: function(index) {
  158. this.currentPage = this.pages[Math.min(index, this.pages.length - 1)][0];
  159. for (var i = 0, len = this.snaps.length; i < len; i++) {
  160. if (i === index) {
  161. this.snaps[i].classList.add(CLASS_ACTIVE);
  162. } else {
  163. this.snaps[i].classList.remove(CLASS_ACTIVE);
  164. }
  165. }
  166. this.scrollTo(this.currentPage.x, 0, this.options.scrollTime);
  167. },
  168. _nearestSnap: function(x) {
  169. if (!this.pages.length) {
  170. return {
  171. x: 0,
  172. pageX: 0
  173. };
  174. }
  175. var i = 0;
  176. var length = this.pages.length;
  177. if (x > 0) {
  178. x = 0;
  179. } else if (x < this.maxScrollX) {
  180. x = this.maxScrollX;
  181. }
  182. for (; i < length; i++) {
  183. var nearestX = this.direction === 'left' ? this.pages[i][0].leftX : this.pages[i][0].rightX;
  184. if (x >= nearestX) {
  185. return this.pages[i][0];
  186. }
  187. }
  188. return {
  189. x: 0,
  190. pageX: 0
  191. };
  192. },
  193. _initEvent: function(detach) {
  194. var action = detach ? 'removeEventListener' : 'addEventListener';
  195. window[action]('orientationchange', this);
  196. window[action]('resize', this);
  197. this.scroller[action]('webkitTransitionEnd', this);
  198. this.wrapper[action]($.EVENT_START, this);
  199. this.wrapper[action]($.EVENT_CANCEL, this);
  200. this.wrapper[action]($.EVENT_END, this);
  201. this.wrapper[action]('drag', this);
  202. this.wrapper[action]('dragend', this);
  203. this.wrapper[action]('flick', this);
  204. this.wrapper[action]('scrollend', this);
  205. if (this.options.scrollX) {
  206. this.wrapper[action]('swiperight', this);
  207. }
  208. var segmentedControl = this.wrapper.querySelector($.classSelector('.segmented-control'));
  209. if (segmentedControl) { //靠,这个bug排查了一下午,阻止hash跳转,一旦hash跳转会导致可拖拽选项卡的tab不见
  210. mui(segmentedControl)[detach ? 'off' : 'on']('click', 'a', $.preventDefault);
  211. }
  212. this.wrapper[action]('scrollstart', this);
  213. this.wrapper[action]('refresh', this);
  214. },
  215. _handleIndicatorScrollend: function() {
  216. this.indicators.map(function(indicator) {
  217. indicator.fade();
  218. });
  219. },
  220. _handleIndicatorScrollstart: function() {
  221. this.indicators.map(function(indicator) {
  222. indicator.fade(1);
  223. });
  224. },
  225. _handleIndicatorRefresh: function() {
  226. this.indicators.map(function(indicator) {
  227. indicator.refresh();
  228. });
  229. },
  230. handleEvent: function(e) {
  231. if (this.stopped) {
  232. this.resetPosition();
  233. return;
  234. }
  235. switch (e.type) {
  236. case $.EVENT_START:
  237. this._start(e);
  238. break;
  239. case 'drag':
  240. this.options.stopPropagation && e.stopPropagation();
  241. this._drag(e);
  242. break;
  243. case 'dragend':
  244. case 'flick':
  245. this.options.stopPropagation && e.stopPropagation();
  246. this._flick(e);
  247. break;
  248. case $.EVENT_CANCEL:
  249. case $.EVENT_END:
  250. this._end(e);
  251. break;
  252. case 'webkitTransitionEnd':
  253. this.transitionTimer && this.transitionTimer.cancel();
  254. this._transitionEnd(e);
  255. break;
  256. case 'scrollstart':
  257. this._handleIndicatorScrollstart(e);
  258. break;
  259. case 'scrollend':
  260. this._handleIndicatorScrollend(e);
  261. this._scrollend(e);
  262. e.stopPropagation();
  263. break;
  264. case 'orientationchange':
  265. case 'resize':
  266. this._resize();
  267. break;
  268. case 'swiperight':
  269. e.stopPropagation();
  270. break;
  271. case 'refresh':
  272. this._handleIndicatorRefresh(e);
  273. break;
  274. }
  275. },
  276. _start: function(e) {
  277. this.moved = this.needReset = false;
  278. this._transitionTime();
  279. if (this.isInTransition) {
  280. this.needReset = true;
  281. this.isInTransition = false;
  282. var pos = $.parseTranslateMatrix($.getStyles(this.scroller, 'webkitTransform'));
  283. this.setTranslate(Math.round(pos.x), Math.round(pos.y));
  284. // this.resetPosition(); //reset
  285. $.trigger(this.scroller, 'scrollend', this);
  286. // e.stopPropagation();
  287. e.preventDefault();
  288. }
  289. this.reLayout();
  290. $.trigger(this.scroller, 'beforescrollstart', this);
  291. },
  292. _getDirectionByAngle: function(angle) {
  293. if (angle < -80 && angle > -100) {
  294. return 'up';
  295. } else if (angle >= 80 && angle < 100) {
  296. return 'down';
  297. } else if (angle >= 170 || angle <= -170) {
  298. return 'left';
  299. } else if (angle >= -35 && angle <= 10) {
  300. return 'right';
  301. }
  302. return null;
  303. },
  304. _drag: function(e) {
  305. // if (this.needReset) {
  306. // e.stopPropagation(); //disable parent drag(nested scroller)
  307. // return;
  308. // }
  309. var detail = e.detail;
  310. if (this.options.scrollY || detail.direction === 'up' || detail.direction === 'down') { //如果是竖向滚动或手势方向是上或下
  311. //ios8 hack
  312. if ($.os.ios && parseFloat($.os.version) >= 8) { //多webview时,离开当前webview会导致后续touch事件不触发
  313. var clientY = detail.gesture.touches[0].clientY;
  314. //下拉刷新 or 上拉加载
  315. if ((clientY + 10) > window.innerHeight || clientY < 10) {
  316. this.resetPosition(this.options.bounceTime);
  317. return;
  318. }
  319. }
  320. }
  321. var isPreventDefault = isReturn = false;
  322. var direction = this._getDirectionByAngle(detail.angle);
  323. if (detail.direction === 'left' || detail.direction === 'right') {
  324. if (this.options.scrollX) {
  325. isPreventDefault = true;
  326. if (!this.moved) { //识别角度(该角度导致轮播不灵敏)
  327. // if (direction !== 'left' && direction !== 'right') {
  328. // isReturn = true;
  329. // } else {
  330. $.gestures.session.lockDirection = true; //锁定方向
  331. $.gestures.session.startDirection = detail.direction;
  332. // }
  333. }
  334. } else if (this.options.scrollY && !this.moved) {
  335. isReturn = true;
  336. }
  337. } else if (detail.direction === 'up' || detail.direction === 'down') {
  338. if (this.options.scrollY) {
  339. isPreventDefault = true;
  340. // if (!this.moved) { //识别角度,竖向滚动似乎没必要进行小角度验证
  341. // if (direction !== 'up' && direction !== 'down') {
  342. // isReturn = true;
  343. // }
  344. // }
  345. if (!this.moved) {
  346. $.gestures.session.lockDirection = true; //锁定方向
  347. $.gestures.session.startDirection = detail.direction;
  348. }
  349. } else if (this.options.scrollX && !this.moved) {
  350. isReturn = true;
  351. }
  352. } else {
  353. isReturn = true;
  354. }
  355. if (this.moved || isPreventDefault) {
  356. e.stopPropagation(); //阻止冒泡(scroll类嵌套)
  357. detail.gesture && detail.gesture.preventDefault();
  358. }
  359. if (isReturn) { //禁止非法方向滚动
  360. return;
  361. }
  362. if (!this.moved) {
  363. $.trigger(this.scroller, 'scrollstart', this);
  364. } else {
  365. e.stopPropagation(); //move期间阻止冒泡(scroll嵌套)
  366. }
  367. var deltaX = 0;
  368. var deltaY = 0;
  369. if (!this.moved) { //start
  370. deltaX = detail.deltaX;
  371. deltaY = detail.deltaY;
  372. } else { //move
  373. deltaX = detail.deltaX - $.gestures.session.prevTouch.deltaX;
  374. deltaY = detail.deltaY - $.gestures.session.prevTouch.deltaY;
  375. }
  376. var absDeltaX = Math.abs(detail.deltaX);
  377. var absDeltaY = Math.abs(detail.deltaY);
  378. if (absDeltaX > absDeltaY + this.options.directionLockThreshold) {
  379. deltaY = 0;
  380. } else if (absDeltaY >= absDeltaX + this.options.directionLockThreshold) {
  381. deltaX = 0;
  382. }
  383. deltaX = this.hasHorizontalScroll ? deltaX : 0;
  384. deltaY = this.hasVerticalScroll ? deltaY : 0;
  385. var newX = this.x + deltaX;
  386. var newY = this.y + deltaY;
  387. // Slow down if outside of the boundaries
  388. if (newX > 0 || newX < this.maxScrollX) {
  389. newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
  390. }
  391. if (newY > 0 || newY < this.maxScrollY) {
  392. newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
  393. }
  394. if (!this.requestAnimationFrame) {
  395. this._updateTranslate();
  396. }
  397. this.direction = detail.deltaX > 0 ? 'right' : 'left';
  398. this.moved = true;
  399. this.x = newX;
  400. this.y = newY;
  401. $.trigger(this.scroller, 'scroll', this);
  402. },
  403. _flick: function(e) {
  404. // if (!this.moved || this.needReset) {
  405. // return;
  406. // }
  407. if (!this.moved) {
  408. return;
  409. }
  410. e.stopPropagation();
  411. var detail = e.detail;
  412. this._clearRequestAnimationFrame();
  413. if (e.type === 'dragend' && detail.flick) { //dragend
  414. return;
  415. }
  416. var newX = Math.round(this.x);
  417. var newY = Math.round(this.y);
  418. this.isInTransition = false;
  419. // reset if we are outside of the boundaries
  420. if (this.resetPosition(this.options.bounceTime)) {
  421. return;
  422. }
  423. this.scrollTo(newX, newY); // ensures that the last position is rounded
  424. if (e.type === 'dragend') { //dragend
  425. $.trigger(this.scroller, 'scrollend', this);
  426. return;
  427. }
  428. var time = 0;
  429. var easing = '';
  430. // start momentum animation if needed
  431. if (this.options.momentum && detail.flickTime < 300) {
  432. momentumX = this.hasHorizontalScroll ? this._momentum(this.x, detail.flickDistanceX, detail.flickTime, this.maxScrollX, this.options.bounce ? this.wrapperWidth : 0, this.options.deceleration) : {
  433. destination: newX,
  434. duration: 0
  435. };
  436. momentumY = this.hasVerticalScroll ? this._momentum(this.y, detail.flickDistanceY, detail.flickTime, this.maxScrollY, this.options.bounce ? this.wrapperHeight : 0, this.options.deceleration) : {
  437. destination: newY,
  438. duration: 0
  439. };
  440. newX = momentumX.destination;
  441. newY = momentumY.destination;
  442. time = Math.max(momentumX.duration, momentumY.duration);
  443. this.isInTransition = true;
  444. }
  445. if (newX != this.x || newY != this.y) {
  446. if (newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY) {
  447. easing = ease.quadratic;
  448. }
  449. this.scrollTo(newX, newY, time, easing);
  450. return;
  451. }
  452. $.trigger(this.scroller, 'scrollend', this);
  453. // e.stopPropagation();
  454. },
  455. _end: function(e) {
  456. this.needReset = false;
  457. if ((!this.moved && this.needReset) || e.type === $.EVENT_CANCEL) {
  458. this.resetPosition();
  459. }
  460. },
  461. _transitionEnd: function(e) {
  462. if (e.target != this.scroller || !this.isInTransition) {
  463. return;
  464. }
  465. this._transitionTime();
  466. if (!this.resetPosition(this.options.bounceTime)) {
  467. this.isInTransition = false;
  468. $.trigger(this.scroller, 'scrollend', this);
  469. }
  470. },
  471. _scrollend: function(e) {
  472. if ((this.y === 0 && this.maxScrollY === 0) || (Math.abs(this.y) > 0 && this.y <= this.maxScrollY)) {
  473. $.trigger(this.scroller, 'scrollbottom', this);
  474. }
  475. },
  476. _resize: function() {
  477. var that = this;
  478. clearTimeout(that.resizeTimeout);
  479. that.resizeTimeout = setTimeout(function() {
  480. that.refresh();
  481. }, that.options.resizePolling);
  482. },
  483. _transitionTime: function(time) {
  484. time = time || 0;
  485. this.scrollerStyle['webkitTransitionDuration'] = time + 'ms';
  486. if (this.parallaxElement && this.options.scrollY) { //目前仅支持竖向视差效果
  487. this.parallaxStyle['webkitTransitionDuration'] = time + 'ms';
  488. }
  489. if (this.options.fixedBadAndorid && !time && $.os.isBadAndroid) {
  490. this.scrollerStyle['webkitTransitionDuration'] = '0.001s';
  491. if (this.parallaxElement && this.options.scrollY) { //目前仅支持竖向视差效果
  492. this.parallaxStyle['webkitTransitionDuration'] = '0.001s';
  493. }
  494. }
  495. if (this.indicators) {
  496. for (var i = this.indicators.length; i--;) {
  497. this.indicators[i].transitionTime(time);
  498. }
  499. }
  500. if (time) { //自定义timer,保证webkitTransitionEnd始终触发
  501. this.transitionTimer && this.transitionTimer.cancel();
  502. this.transitionTimer = $.later(function() {
  503. $.trigger(this.scroller, 'webkitTransitionEnd');
  504. }, time + 100, this);
  505. }
  506. },
  507. _transitionTimingFunction: function(easing) {
  508. this.scrollerStyle['webkitTransitionTimingFunction'] = easing;
  509. if (this.parallaxElement && this.options.scrollY) { //目前仅支持竖向视差效果
  510. this.parallaxStyle['webkitTransitionDuration'] = easing;
  511. }
  512. if (this.indicators) {
  513. for (var i = this.indicators.length; i--;) {
  514. this.indicators[i].transitionTimingFunction(easing);
  515. }
  516. }
  517. },
  518. _translate: function(x, y) {
  519. this.x = x;
  520. this.y = y;
  521. },
  522. _clearRequestAnimationFrame: function() {
  523. if (this.requestAnimationFrame) {
  524. cancelAnimationFrame(this.requestAnimationFrame);
  525. this.requestAnimationFrame = null;
  526. }
  527. },
  528. _updateTranslate: function() {
  529. var self = this;
  530. if (self.x !== self.lastX || self.y !== self.lastY) {
  531. self.setTranslate(self.x, self.y);
  532. }
  533. self.requestAnimationFrame = requestAnimationFrame(function() {
  534. self._updateTranslate();
  535. });
  536. },
  537. _createScrollBar: function(clazz) {
  538. var scrollbar = document.createElement('div');
  539. var indicator = document.createElement('div');
  540. scrollbar.className = CLASS_SCROLLBAR + ' ' + clazz;
  541. indicator.className = CLASS_INDICATOR;
  542. scrollbar.appendChild(indicator);
  543. if (clazz === CLASS_SCROLLBAR_VERTICAL) {
  544. this.scrollbarY = scrollbar;
  545. this.scrollbarIndicatorY = indicator;
  546. } else if (clazz === CLASS_SCROLLBAR_HORIZONTAL) {
  547. this.scrollbarX = scrollbar;
  548. this.scrollbarIndicatorX = indicator;
  549. }
  550. this.wrapper.appendChild(scrollbar);
  551. return scrollbar;
  552. },
  553. _preventDefaultException: function(el, exceptions) {
  554. for (var i in exceptions) {
  555. if (exceptions[i].test(el[i])) {
  556. return true;
  557. }
  558. }
  559. return false;
  560. },
  561. _reLayout: function() {
  562. if (!this.hasHorizontalScroll) {
  563. this.maxScrollX = 0;
  564. this.scrollerWidth = this.wrapperWidth;
  565. }
  566. if (!this.hasVerticalScroll) {
  567. this.maxScrollY = 0;
  568. this.scrollerHeight = this.wrapperHeight;
  569. }
  570. this.indicators.map(function(indicator) {
  571. indicator.refresh();
  572. });
  573. //以防slider类嵌套使用
  574. if (this.options.snap && typeof this.options.snap === 'string') {
  575. var items = this.scroller.querySelectorAll(this.options.snap);
  576. this.itemLength = 0;
  577. this.snaps = [];
  578. for (var i = 0, len = items.length; i < len; i++) {
  579. var item = items[i];
  580. if (item.parentNode === this.scroller) {
  581. this.itemLength++;
  582. this.snaps.push(item);
  583. }
  584. }
  585. this._initSnap(); //需要每次都_initSnap么。其实init的时候执行一次,后续resize的时候执行一次就行了吧.先这么做吧,如果影响性能,再调整
  586. }
  587. },
  588. _momentum: function(current, distance, time, lowerMargin, wrapperSize, deceleration) {
  589. var speed = parseFloat(Math.abs(distance) / time),
  590. destination,
  591. duration;
  592. deceleration = deceleration === undefined ? 0.0006 : deceleration;
  593. destination = current + (speed * speed) / (2 * deceleration) * (distance < 0 ? -1 : 1);
  594. duration = speed / deceleration;
  595. if (destination < lowerMargin) {
  596. destination = wrapperSize ? lowerMargin - (wrapperSize / 2.5 * (speed / 8)) : lowerMargin;
  597. distance = Math.abs(destination - current);
  598. duration = distance / speed;
  599. } else if (destination > 0) {
  600. destination = wrapperSize ? wrapperSize / 2.5 * (speed / 8) : 0;
  601. distance = Math.abs(current) + destination;
  602. duration = distance / speed;
  603. }
  604. return {
  605. destination: Math.round(destination),
  606. duration: duration
  607. };
  608. },
  609. _getTranslateStr: function(x, y) {
  610. if (this.options.hardwareAccelerated) {
  611. return 'translate3d(' + x + 'px,' + y + 'px,0px) ' + this.translateZ;
  612. }
  613. return 'translate(' + x + 'px,' + y + 'px) ';
  614. },
  615. //API
  616. setStopped: function(stopped) {
  617. // this.stopped = !!stopped;
  618. // fixed ios双webview模式下拉刷新
  619. if(stopped) {
  620. this.disablePullupToRefresh();
  621. this.disablePulldownToRefresh();
  622. } else {
  623. this.enablePullupToRefresh();
  624. this.enablePulldownToRefresh();
  625. }
  626. },
  627. setTranslate: function(x, y) {
  628. this.x = x;
  629. this.y = y;
  630. this.scrollerStyle['webkitTransform'] = this._getTranslateStr(x, y);
  631. if (this.parallaxElement && this.options.scrollY) { //目前仅支持竖向视差效果
  632. var parallaxY = y * this.options.parallaxRatio;
  633. var scale = 1 + parallaxY / ((this.parallaxHeight - parallaxY) / 2);
  634. if (scale > 1) {
  635. this.parallaxImgStyle['opacity'] = 1 - parallaxY / 100 * this.options.parallaxRatio;
  636. this.parallaxStyle['webkitTransform'] = this._getTranslateStr(0, -parallaxY) + ' scale(' + scale + ',' + scale + ')';
  637. } else {
  638. this.parallaxImgStyle['opacity'] = 1;
  639. this.parallaxStyle['webkitTransform'] = this._getTranslateStr(0, -1) + ' scale(1,1)';
  640. }
  641. }
  642. if (this.indicators) {
  643. for (var i = this.indicators.length; i--;) {
  644. this.indicators[i].updatePosition();
  645. }
  646. }
  647. this.lastX = this.x;
  648. this.lastY = this.y;
  649. $.trigger(this.scroller, 'scroll', this);
  650. },
  651. reLayout: function() {
  652. this.wrapper.offsetHeight;
  653. var paddingLeft = parseFloat($.getStyles(this.wrapper, 'padding-left')) || 0;
  654. var paddingRight = parseFloat($.getStyles(this.wrapper, 'padding-right')) || 0;
  655. var paddingTop = parseFloat($.getStyles(this.wrapper, 'padding-top')) || 0;
  656. var paddingBottom = parseFloat($.getStyles(this.wrapper, 'padding-bottom')) || 0;
  657. var clientWidth = this.wrapper.clientWidth;
  658. var clientHeight = this.wrapper.clientHeight;
  659. this.scrollerWidth = this.scroller.offsetWidth;
  660. this.scrollerHeight = this.scroller.offsetHeight;
  661. this.wrapperWidth = clientWidth - paddingLeft - paddingRight;
  662. this.wrapperHeight = clientHeight - paddingTop - paddingBottom;
  663. this.maxScrollX = Math.min(this.wrapperWidth - this.scrollerWidth, 0);
  664. this.maxScrollY = Math.min(this.wrapperHeight - this.scrollerHeight, 0);
  665. this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
  666. this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
  667. this._reLayout();
  668. },
  669. resetPosition: function(time) {
  670. var x = this.x,
  671. y = this.y;
  672. time = time || 0;
  673. if (!this.hasHorizontalScroll || this.x > 0) {
  674. x = 0;
  675. } else if (this.x < this.maxScrollX) {
  676. x = this.maxScrollX;
  677. }
  678. if (!this.hasVerticalScroll || this.y > 0) {
  679. y = 0;
  680. } else if (this.y < this.maxScrollY) {
  681. y = this.maxScrollY;
  682. }
  683. if (x == this.x && y == this.y) {
  684. return false;
  685. }
  686. this.scrollTo(x, y, time, this.options.scrollEasing);
  687. return true;
  688. },
  689. _reInit: function() {
  690. var groups = this.wrapper.querySelectorAll('.' + CLASS_SCROLL);
  691. for (var i = 0, len = groups.length; i < len; i++) {
  692. if (groups[i].parentNode === this.wrapper) {
  693. this.scroller = groups[i];
  694. break;
  695. }
  696. }
  697. this.scrollerStyle = this.scroller && this.scroller.style;
  698. },
  699. refresh: function() {
  700. this._reInit();
  701. this.reLayout();
  702. $.trigger(this.scroller, 'refresh', this);
  703. this.resetPosition();
  704. },
  705. scrollTo: function(x, y, time, easing) {
  706. var easing = easing || ease.circular;
  707. // this.isInTransition = time > 0 && (this.lastX != x || this.lastY != y);
  708. //暂不严格判断x,y,否则会导致部分版本上不正常触发轮播
  709. this.isInTransition = time > 0;
  710. if (this.isInTransition) {
  711. this._clearRequestAnimationFrame();
  712. this._transitionTimingFunction(easing.style);
  713. this._transitionTime(time);
  714. this.setTranslate(x, y);
  715. } else {
  716. this.setTranslate(x, y);
  717. }
  718. },
  719. scrollToBottom: function(time, easing) {
  720. time = time || this.options.scrollTime;
  721. this.scrollTo(0, this.maxScrollY, time, easing);
  722. },
  723. gotoPage: function(index) {
  724. this._gotoPage(index);
  725. },
  726. destroy: function() {
  727. this._initEvent(true); //detach
  728. delete $.data[this.wrapper.getAttribute('data-scroll')];
  729. this.wrapper.setAttribute('data-scroll', '');
  730. }
  731. });
  732. //Indicator
  733. var Indicator = function(scroller, options) {
  734. this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
  735. this.wrapperStyle = this.wrapper.style;
  736. this.indicator = this.wrapper.children[0];
  737. this.indicatorStyle = this.indicator.style;
  738. this.scroller = scroller;
  739. this.options = $.extend({
  740. listenX: true,
  741. listenY: true,
  742. fade: false,
  743. speedRatioX: 0,
  744. speedRatioY: 0
  745. }, options);
  746. this.sizeRatioX = 1;
  747. this.sizeRatioY = 1;
  748. this.maxPosX = 0;
  749. this.maxPosY = 0;
  750. if (this.options.fade) {
  751. this.wrapperStyle['webkitTransform'] = this.scroller.translateZ;
  752. this.wrapperStyle['webkitTransitionDuration'] = this.options.fixedBadAndorid && $.os.isBadAndroid ? '0.001s' : '0ms';
  753. this.wrapperStyle.opacity = '0';
  754. }
  755. }
  756. Indicator.prototype = {
  757. handleEvent: function(e) {
  758. },
  759. transitionTime: function(time) {
  760. time = time || 0;
  761. this.indicatorStyle['webkitTransitionDuration'] = time + 'ms';
  762. if (this.scroller.options.fixedBadAndorid && !time && $.os.isBadAndroid) {
  763. this.indicatorStyle['webkitTransitionDuration'] = '0.001s';
  764. }
  765. },
  766. transitionTimingFunction: function(easing) {
  767. this.indicatorStyle['webkitTransitionTimingFunction'] = easing;
  768. },
  769. refresh: function() {
  770. this.transitionTime();
  771. if (this.options.listenX && !this.options.listenY) {
  772. this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
  773. } else if (this.options.listenY && !this.options.listenX) {
  774. this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
  775. } else {
  776. this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
  777. }
  778. this.wrapper.offsetHeight; // force refresh
  779. if (this.options.listenX) {
  780. this.wrapperWidth = this.wrapper.clientWidth;
  781. this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
  782. this.indicatorStyle.width = this.indicatorWidth + 'px';
  783. this.maxPosX = this.wrapperWidth - this.indicatorWidth;
  784. this.minBoundaryX = 0;
  785. this.maxBoundaryX = this.maxPosX;
  786. this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
  787. }
  788. if (this.options.listenY) {
  789. this.wrapperHeight = this.wrapper.clientHeight;
  790. this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
  791. this.indicatorStyle.height = this.indicatorHeight + 'px';
  792. this.maxPosY = this.wrapperHeight - this.indicatorHeight;
  793. this.minBoundaryY = 0;
  794. this.maxBoundaryY = this.maxPosY;
  795. this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
  796. }
  797. this.updatePosition();
  798. },
  799. updatePosition: function() {
  800. var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
  801. y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
  802. if (x < this.minBoundaryX) {
  803. this.width = Math.max(this.indicatorWidth + x, 8);
  804. this.indicatorStyle.width = this.width + 'px';
  805. x = this.minBoundaryX;
  806. } else if (x > this.maxBoundaryX) {
  807. this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
  808. this.indicatorStyle.width = this.width + 'px';
  809. x = this.maxPosX + this.indicatorWidth - this.width;
  810. } else if (this.width != this.indicatorWidth) {
  811. this.width = this.indicatorWidth;
  812. this.indicatorStyle.width = this.width + 'px';
  813. }
  814. if (y < this.minBoundaryY) {
  815. this.height = Math.max(this.indicatorHeight + y * 3, 8);
  816. this.indicatorStyle.height = this.height + 'px';
  817. y = this.minBoundaryY;
  818. } else if (y > this.maxBoundaryY) {
  819. this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
  820. this.indicatorStyle.height = this.height + 'px';
  821. y = this.maxPosY + this.indicatorHeight - this.height;
  822. } else if (this.height != this.indicatorHeight) {
  823. this.height = this.indicatorHeight;
  824. this.indicatorStyle.height = this.height + 'px';
  825. }
  826. this.x = x;
  827. this.y = y;
  828. this.indicatorStyle['webkitTransform'] = this.scroller._getTranslateStr(x, y);
  829. },
  830. fade: function(val, hold) {
  831. if (hold && !this.visible) {
  832. return;
  833. }
  834. clearTimeout(this.fadeTimeout);
  835. this.fadeTimeout = null;
  836. var time = val ? 250 : 500,
  837. delay = val ? 0 : 300;
  838. val = val ? '1' : '0';
  839. this.wrapperStyle['webkitTransitionDuration'] = time + 'ms';
  840. this.fadeTimeout = setTimeout((function(val) {
  841. this.wrapperStyle.opacity = val;
  842. this.visible = +val;
  843. }).bind(this, val), delay);
  844. }
  845. };
  846. $.Scroll = Scroll;
  847. $.fn.scroll = function(options) {
  848. var scrollApis = [];
  849. this.each(function() {
  850. var scrollApi = null;
  851. var self = this;
  852. var id = self.getAttribute('data-scroll');
  853. if (!id) {
  854. id = ++$.uuid;
  855. var _options = $.extend({}, options);
  856. if (self.classList.contains($.className('segmented-control'))) {
  857. _options = $.extend(_options, {
  858. scrollY: false,
  859. scrollX: true,
  860. indicators: false,
  861. snap: $.classSelector('.control-item')
  862. });
  863. }
  864. $.data[id] = scrollApi = new Scroll(self, _options);
  865. self.setAttribute('data-scroll', id);
  866. } else {
  867. scrollApi = $.data[id];
  868. }
  869. scrollApis.push(scrollApi);
  870. });
  871. return scrollApis.length === 1 ? scrollApis[0] : scrollApis;
  872. };
  873. })(mui, window, document);