free-mode.js 7.9 KB

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