free-mode.js 8.1 KB

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