react-id-swiper icon indicating copy to clipboard operation
react-id-swiper copied to clipboard

Previous slide images not loading (Centered slides + lazy loading + loop)

Open ntucakovic opened this issue 6 years ago • 8 comments

When lazy loading is enabled with centered slides, slides appearing left of the active slide will not be loaded. Also when moving to previous slides, again images will not be loaded.

Demo: https://codesandbox.io/s/reactidswiper-center-slides-lazy-loading-wqpue Fix: https://codesandbox.io/s/reactidswiper-center-slides-lazy-loading-fixed-s7kh9

/**
 * When we set `centeredSlides` to `true`, slides before the active one aren't initially loaded.
 */
export function useLazyCenteredSlides(swiperInstance: SwiperInstance): void {
  const loadPreviousSlides = useCallback(() => {
    const slidesPerView = swiperInstance.params.slidesPerView;
    const activeIndex = swiperInstance.activeIndex;

    const loadPreviousTotal = Math.ceil(
      (slidesPerView !== "auto" ? slidesPerView : 2) / 2
    );
    for (let i = 0; i <= loadPreviousTotal; i++) {
      swiperInstance.lazy.loadInSlide(activeIndex - i);
    }
  }, [swiperInstance]);

  useEffect(() => {
    if (swiperInstance !== null) {
      loadPreviousSlides();
      swiperInstance.on("slideChange", loadPreviousSlides);
    }
  }, [swiperInstance, loadPreviousSlides]);
}

As you can see, it's not ideal. I don't have proper support in case Swiper is initialized with slidesPerView auto. So just work on top of that if you need to support that situation. I don't have that scenario in my swiper instances.

I'm not sure whether I should lift this issue to swiper repo.

ntucakovic avatar Aug 29 '19 09:08 ntucakovic

I had very similar issue. Looped swiper with 5 images in a view. Images got their src lazy and once I scoll back there are empty places instead allready loaded images.

akopichin avatar Sep 16 '19 09:09 akopichin

+1 also having this same issue.

braxex avatar Jan 06 '20 18:01 braxex

+1 having same issue

operkot avatar Feb 10 '20 19:02 operkot

interesting could this also be same reason issue #394 fails

theskillwithin avatar May 23 '20 23:05 theskillwithin

1+

juliocarneiro avatar Jan 13 '21 13:01 juliocarneiro

btw the swiper library now supports react out of the box. this repo is just a wrapper around it

https://swiperjs.com/

theskillwithin avatar Jan 13 '21 14:01 theskillwithin

1+

lmoroz avatar Feb 18 '22 06:02 lmoroz

It still happens even when not using Lazy load, only loop and centeredSlides. The las ones (previous) are not loading images but the text does load.

mariaferraro avatar Mar 23 '23 16:03 mariaferraro