updateSlidesProgress.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. export default function updateSlidesProgress(translate = this && this.translate || 0) {
  2. const swiper = this;
  3. const params = swiper.params;
  4. const {
  5. slides,
  6. rtlTranslate: rtl,
  7. snapGrid
  8. } = swiper;
  9. if (slides.length === 0) return;
  10. if (typeof slides[0].swiperSlideOffset === 'undefined' || typeof slides[slides.length - 1].swiperSlideOffset ===
  11. 'undefined') swiper
  12. .updateSlidesOffset();
  13. let offsetCenter = -translate;
  14. if (rtl) offsetCenter = translate; // Visible Slides
  15. swiper.visibleSlidesIndexes = [];
  16. swiper.visibleSlides = [];
  17. // slides.forEach((item)=>)
  18. for (let i = 0; i < slides.length; i += 1) {
  19. const slide = slides[i];
  20. let slideOffset = slide.swiperSlideOffset;
  21. if (params.cssMode && params.centeredSlides) {
  22. slideOffset -= slides[0].swiperSlideOffset;
  23. }
  24. const slideProgress = (offsetCenter + (params.centeredSlides ? swiper.minTranslate() : 0) - slideOffset) / (
  25. slide.swiperSlideSize + params.spaceBetween);
  26. const originalSlideProgress = (offsetCenter - snapGrid[0] + (params.centeredSlides ? swiper.minTranslate() :
  27. 0) - slideOffset) / (slide.swiperSlideSize + params.spaceBetween);
  28. const slideBefore = -(offsetCenter - slideOffset);
  29. const slideAfter = slideBefore + swiper.slidesSizesGrid[i];
  30. const isVisible = slideBefore >= 0 && slideBefore < swiper.size - 1 || slideAfter > 1 && slideAfter <= swiper
  31. .size || slideBefore <= 0 && slideAfter >= swiper.size;
  32. if (isVisible) {
  33. swiper.visibleSlides.push(slide);
  34. swiper.visibleSlidesIndexes.push(i);
  35. slides[i].addClass(params.slideVisibleClass);
  36. }
  37. slide.progress = rtl ? -slideProgress : slideProgress;
  38. slide.originalProgress = rtl ? -originalSlideProgress : originalSlideProgress;
  39. }
  40. }