loopFix.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. export default function loopFix() {
  2. const swiper = this;
  3. swiper.emit('beforeLoopFix');
  4. const {
  5. activeIndex,
  6. slides,
  7. loopedSlides,
  8. allowSlidePrev,
  9. allowSlideNext,
  10. snapGrid,
  11. rtlTranslate: rtl
  12. } = swiper;
  13. let newIndex;
  14. swiper.allowSlidePrev = true;
  15. swiper.allowSlideNext = true;
  16. const snapTranslate = -snapGrid[activeIndex];
  17. const diff = snapTranslate - swiper.getTranslate();
  18. if (activeIndex < loopedSlides) {
  19. newIndex = slides.length - loopedSlides * 3 + activeIndex;
  20. newIndex += loopedSlides;
  21. const slideChanged = swiper.slideTo(newIndex, 0, false, true);
  22. if (slideChanged && diff !== 0) {
  23. swiper.setTranslate((rtl ? -swiper.translate : swiper.translate) - diff);
  24. }
  25. } else if (activeIndex >= slides.length - loopedSlides) {
  26. newIndex = -slides.length + activeIndex + loopedSlides;
  27. newIndex += loopedSlides;
  28. const slideChanged = swiper.slideTo(newIndex, 0, false, true);
  29. if (slideChanged && diff !== 0) {
  30. swiper.setTranslate((rtl ? -swiper.translate : swiper.translate) - diff);
  31. }
  32. }
  33. swiper.allowSlidePrev = allowSlidePrev;
  34. swiper.allowSlideNext = allowSlideNext;
  35. swiper.emit('loopFix');
  36. }