slidePrev.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. export default function slidePrev(speed = this.params.speed, runCallbacks = true, internal) {
  2. const swiper = this;
  3. const {
  4. params,
  5. animating,
  6. snapGrid,
  7. slidesGrid,
  8. rtlTranslate,
  9. enabled
  10. } = swiper;
  11. if (!enabled) return swiper;
  12. if (params.loop) {
  13. if (animating && params.loopPreventsSlide) return false;
  14. swiper.loopFix();
  15. }
  16. const translate = rtlTranslate ? swiper.translate : -swiper.translate;
  17. function normalize(val) {
  18. if (val < 0) return -Math.floor(Math.abs(val));
  19. return Math.floor(val);
  20. }
  21. const normalizedTranslate = normalize(translate);
  22. const normalizedSnapGrid = snapGrid.map(val => normalize(val));
  23. let prevSnap = snapGrid[normalizedSnapGrid.indexOf(normalizedTranslate) - 1];
  24. if (typeof prevSnap === 'undefined' && params.cssMode) {
  25. let prevSnapIndex;
  26. snapGrid.forEach((snap, snapIndex) => {
  27. if (normalizedTranslate >= snap) {
  28. prevSnapIndex = snapIndex;
  29. }
  30. });
  31. if (typeof prevSnapIndex !== 'undefined') {
  32. prevSnap = snapGrid[prevSnapIndex > 0 ? prevSnapIndex - 1 : prevSnapIndex];
  33. }
  34. }
  35. let prevIndex = 0;
  36. if (typeof prevSnap !== 'undefined') {
  37. prevIndex = slidesGrid.indexOf(prevSnap);
  38. if (prevIndex < 0) prevIndex = swiper.activeIndex - 1;
  39. if (params.slidesPerView === 'auto' && params.slidesPerGroup === 1 && params.slidesPerGroupAuto) {
  40. prevIndex = prevIndex - swiper.slidesPerViewDynamic('previous', true) + 1;
  41. prevIndex = Math.max(prevIndex, 0);
  42. }
  43. }
  44. if (params.rewind && swiper.isBeginning) {
  45. return swiper.slideTo(swiper.slides.length - 1, speed, runCallbacks, internal);
  46. }
  47. setTimeout(() => {
  48. swiper.slideTo(prevIndex, speed, runCallbacks, internal)
  49. }, 30)
  50. return true;
  51. }