translateTo.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {
  2. animateCSSModeScroll
  3. } from '../../shared/utils.js';
  4. export default function translateTo(translate = 0, speed = this.params.speed, runCallbacks = true, translateBounds =
  5. true, internal) {
  6. const swiper = this;
  7. let timer;
  8. const {
  9. params,
  10. wrapperEl
  11. } = swiper;
  12. if (swiper.animating && params.preventInteractionOnTransition) {
  13. return false;
  14. }
  15. const minTranslate = swiper.minTranslate();
  16. const maxTranslate = swiper.maxTranslate();
  17. let newTranslate;
  18. if (translateBounds && translate > minTranslate) newTranslate = minTranslate;
  19. else if (translateBounds && translate < maxTranslate) newTranslate = maxTranslate;
  20. else newTranslate = translate; // Update progress
  21. swiper.updateProgress(newTranslate);
  22. if (params.cssMode) {
  23. const isH = swiper.isHorizontal();
  24. if (speed === 0) {
  25. wrapperEl[isH ? 'scrollLeft' : 'scrollTop'] = -newTranslate;
  26. } else {
  27. if (!swiper.support.smoothScroll) {
  28. animateCSSModeScroll({
  29. swiper,
  30. targetPosition: -newTranslate,
  31. side: isH ? 'left' : 'top'
  32. });
  33. return true;
  34. }
  35. wrapperEl.scrollTo({
  36. [isH ? 'left' : 'top']: -newTranslate,
  37. behavior: 'smooth'
  38. });
  39. }
  40. return true;
  41. }
  42. if (speed === 0) {
  43. swiper.setTransition(0);
  44. swiper.setTranslate(newTranslate);
  45. if (runCallbacks) {
  46. swiper.emit('beforeTransitionStart', speed, internal);
  47. swiper.emit('transitionEnd');
  48. }
  49. } else {
  50. swiper.setTransition(speed);
  51. swiper.setTranslate(newTranslate);
  52. if (runCallbacks) {
  53. swiper.emit('beforeTransitionStart', speed, internal);
  54. swiper.emit('transitionStart');
  55. }
  56. if (!swiper.animating) {
  57. swiper.animating = true;
  58. if (!swiper.onTranslateToWrapperTransitionEnd) {
  59. swiper.onTranslateToWrapperTransitionEnd = function transitionEnd(e) {
  60. if (!swiper || swiper.destroyed) return;
  61. if (e.target !== this) return;
  62. clearTimeout(timer)
  63. swiper.onTranslateToWrapperTransitionEnd = null;
  64. delete swiper.onTranslateToWrapperTransitionEnd;
  65. if (runCallbacks) {
  66. swiper.emit('transitionEnd');
  67. }
  68. };
  69. }
  70. timer = setTimeout(() => {
  71. swiper.onTranslateToWrapperTransitionEnd();
  72. }, speed)
  73. }
  74. }
  75. return true;
  76. }