free-mode.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import {
  2. now
  3. } from '../../shared/utils.js';
  4. export default function freeMode({
  5. swiper,
  6. extendParams,
  7. emit,
  8. once
  9. }) {
  10. extendParams({
  11. freeMode: {
  12. enabled: false,
  13. momentum: true,
  14. momentumRatio: 1,
  15. momentumBounce: true,
  16. momentumBounceRatio: 1,
  17. momentumVelocityRatio: 1,
  18. sticky: false,
  19. minimumVelocity: 0.02
  20. }
  21. });
  22. function onTouchMove() {
  23. const {
  24. touchEventsData: data,
  25. touches
  26. } = swiper; // Velocity
  27. if (data.velocities.length === 0) {
  28. data.velocities.push({
  29. position: touches[swiper.isHorizontal() ? 'startX' : 'startY'],
  30. time: data.touchStartTime
  31. });
  32. }
  33. data.velocities.push({
  34. position: touches[swiper.isHorizontal() ? 'currentX' : 'currentY'],
  35. time: now()
  36. });
  37. }
  38. function onTouchEnd({
  39. currentPos
  40. }) {
  41. const {
  42. params,
  43. $wrapperEl,
  44. rtlTranslate: rtl,
  45. snapGrid,
  46. touchEventsData: data
  47. } = swiper; // Time diff
  48. const touchEndTime = now();
  49. const timeDiff = touchEndTime - data.touchStartTime;
  50. if (currentPos < -swiper.minTranslate()) {
  51. swiper.slideTo(swiper.activeIndex);
  52. return;
  53. }
  54. if (currentPos > -swiper.maxTranslate()) {
  55. if (swiper.slides.length < snapGrid.length) {
  56. swiper.slideTo(snapGrid.length - 1);
  57. } else {
  58. swiper.slideTo(swiper.slides.length - 1);
  59. }
  60. return;
  61. }
  62. if (params.freeMode.momentum) {
  63. if (data.velocities.length > 1) {
  64. const lastMoveEvent = data.velocities.pop();
  65. const velocityEvent = data.velocities.pop();
  66. const distance = lastMoveEvent.position - velocityEvent.position;
  67. const time = lastMoveEvent.time - velocityEvent.time;
  68. swiper.velocity = distance / time;
  69. swiper.velocity /= 2;
  70. if (Math.abs(swiper.velocity) < params.freeMode.minimumVelocity) {
  71. swiper.velocity = 0;
  72. } // this implies that the user stopped moving a finger then released.
  73. // There would be no events with distance zero, so the last event is stale.
  74. if (time > 150 || now() - lastMoveEvent.time > 300) {
  75. swiper.velocity = 0;
  76. }
  77. } else {
  78. swiper.velocity = 0;
  79. }
  80. swiper.velocity *= params.freeMode.momentumVelocityRatio;
  81. data.velocities.length = 0;
  82. let momentumDuration = 1000 * params.freeMode.momentumRatio;
  83. const momentumDistance = swiper.velocity * momentumDuration;
  84. let newPosition = swiper.translate + momentumDistance;
  85. if (rtl) newPosition = -newPosition;
  86. let doBounce = false;
  87. let afterBouncePosition;
  88. const bounceAmount = Math.abs(swiper.velocity) * 20 * params.freeMode.momentumBounceRatio;
  89. let needsLoopFix;
  90. if (newPosition < swiper.maxTranslate()) {
  91. if (params.freeMode.momentumBounce) {
  92. if (newPosition + swiper.maxTranslate() < -bounceAmount) {
  93. newPosition = swiper.maxTranslate() - bounceAmount;
  94. }
  95. afterBouncePosition = swiper.maxTranslate();
  96. doBounce = true;
  97. data.allowMomentumBounce = true;
  98. } else {
  99. newPosition = swiper.maxTranslate();
  100. }
  101. if (params.loop && params.centeredSlides) needsLoopFix = true;
  102. } else if (newPosition > swiper.minTranslate()) {
  103. if (params.freeMode.momentumBounce) {
  104. if (newPosition - swiper.minTranslate() > bounceAmount) {
  105. newPosition = swiper.minTranslate() + bounceAmount;
  106. }
  107. afterBouncePosition = swiper.minTranslate();
  108. doBounce = true;
  109. data.allowMomentumBounce = true;
  110. } else {
  111. newPosition = swiper.minTranslate();
  112. }
  113. if (params.loop && params.centeredSlides) needsLoopFix = true;
  114. } else if (params.freeMode.sticky) {
  115. let nextSlide;
  116. for (let j = 0; j < snapGrid.length; j += 1) {
  117. if (snapGrid[j] > -newPosition) {
  118. nextSlide = j;
  119. break;
  120. }
  121. }
  122. if (Math.abs(snapGrid[nextSlide] - newPosition) < Math.abs(snapGrid[nextSlide - 1] - newPosition) ||
  123. swiper.swipeDirection === 'next') {
  124. newPosition = snapGrid[nextSlide];
  125. } else {
  126. newPosition = snapGrid[nextSlide - 1];
  127. }
  128. newPosition = -newPosition;
  129. }
  130. if (needsLoopFix) {
  131. once('transitionEnd', () => {
  132. swiper.loopFix();
  133. });
  134. } // Fix duration
  135. if (swiper.velocity !== 0) {
  136. if (rtl) {
  137. momentumDuration = Math.abs((-newPosition - swiper.translate) / swiper.velocity);
  138. } else {
  139. momentumDuration = Math.abs((newPosition - swiper.translate) / swiper.velocity);
  140. }
  141. if (params.freeMode.sticky) {
  142. const moveDistance = Math.abs((rtl ? -newPosition : newPosition) - swiper.translate);
  143. const currentSlideSize = swiper.slidesSizesGrid[swiper.activeIndex];
  144. if (moveDistance < currentSlideSize) {
  145. momentumDuration = params.speed;
  146. } else if (moveDistance < 2 * currentSlideSize) {
  147. momentumDuration = params.speed * 1.5;
  148. } else {
  149. momentumDuration = params.speed * 2.5;
  150. }
  151. }
  152. } else if (params.freeMode.sticky) {
  153. swiper.slideToClosest();
  154. return;
  155. }
  156. if (params.freeMode.momentumBounce && doBounce) {
  157. swiper.updateProgress(afterBouncePosition);
  158. swiper.setTransition(momentumDuration);
  159. swiper.setTranslate(newPosition);
  160. swiper.transitionStart(true, swiper.swipeDirection);
  161. swiper.animating = true;
  162. $wrapperEl.transitionEnd(() => {
  163. if (!swiper || swiper.destroyed || !data.allowMomentumBounce) return;
  164. emit('momentumBounce');
  165. swiper.setTransition(params.speed);
  166. setTimeout(() => {
  167. swiper.setTranslate(afterBouncePosition);
  168. $wrapperEl.transitionEnd(() => {
  169. if (!swiper || swiper.destroyed) return;
  170. swiper.transitionEnd();
  171. }, momentumDuration);
  172. }, 0);
  173. }, momentumDuration);
  174. } else if (swiper.velocity) {
  175. emit('_freeModeNoMomentumRelease');
  176. swiper.updateProgress(newPosition);
  177. swiper.setTransition(momentumDuration);
  178. swiper.setTranslate(newPosition);
  179. swiper.transitionStart(true, swiper.swipeDirection);
  180. if (!swiper.animating) {
  181. swiper.animating = true;
  182. $wrapperEl.transitionEnd(() => {
  183. if (!swiper || swiper.destroyed) return;
  184. swiper.transitionEnd();
  185. }, momentumDuration);
  186. }
  187. } else {
  188. swiper.updateProgress(newPosition);
  189. }
  190. swiper.updateActiveIndex();
  191. swiper.updateSlidesClasses();
  192. } else if (params.freeMode.sticky) {
  193. swiper.slideToClosest();
  194. return;
  195. } else if (params.freeMode) {
  196. emit('_freeModeNoMomentumRelease');
  197. }
  198. if (!params.freeMode.momentum || timeDiff >= params.longSwipesMs) {
  199. swiper.updateProgress();
  200. swiper.updateActiveIndex();
  201. swiper.updateSlidesClasses();
  202. }
  203. }
  204. Object.assign(swiper, {
  205. freeMode: {
  206. onTouchMove,
  207. onTouchEnd
  208. }
  209. });
  210. }