swiper icon indicating copy to clipboard operation
swiper copied to clipboard

LazyLoading not working combined with Grid

Open ezetojo opened this issue 3 years ago • 2 comments

Check that this is really a bug

  • [X] I confirm

Reproduction link

https://codesandbox.io/s/swiper-lazy-load-images-forked-ppqb7?file=/index.html

Bug description

LoadPrevNextAmount on lazy options not working when using grid (didn't test it alone)

Expected Behavior

Use loadPrevNextAmount to calculate how many pictures must be loaded for slide

Actual Behavior

Always is loaded 1 picture

Swiper version

7.2

Platform/Target and Browser Versions

Windows 10 Chrome 95

Validations

  • [X] Follow our Code of Conduct
  • [X] Read the docs.
  • [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
  • [X] Make sure this is a Swiper issue and not a framework-specific issue

Would you like to open a PR for this bug?

  • [ ] I'm willing to open a PR

ezetojo avatar Nov 12 '21 20:11 ezetojo

In https://github.com/nolimits4web/swiper/blob/master/src/modules/lazy/lazy.js#L167

Something like:

        const amount = params.loadPrevNextAmount;
        const rows = swiperParams.grid ? swiperParams.grid.rows : 1;
        const spv = slidesPerView * rows;

        const maxIndex = Math.min(activeIndex + spv + Math.max(amount, spv), slides.length);
        const minIndex = Math.max(activeIndex - Math.max(spv, amount), 0);
        // Next Slides
        for (let i = activeIndex + spv; i < maxIndex; i += 1) {
          if (slideExist(i)) loadInSlide(i);
        }

?

dantio avatar Nov 22 '21 15:11 dantio

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 12 '22 14:06 stale[bot]

I am encountering the exact same issue, has anyone found a workaround or a fix ?

Pandaii avatar Sep 26 '22 15:09 Pandaii

Based on Dantio's answer, the solution that I found is the following: https://github.com/nolimits4web/swiper/blob/master/src/modules/lazy/lazy.js#L167

if (params.loadPrevNext) {
    if (slidesPerView > 1 || params.loadPrevNextAmount && params.loadPrevNextAmount > 1) {
      const amount = params.loadPrevNextAmount;
      const rows = swiperParams.grid ? swiperParams.grid.rows : 1;
      const spv = slidesPerView * rows;

      const maxIndex = Math.min((activeIndex+1) * spv + Math.max(amount, spv), slides.length);
      const minIndex = Math.max(activeIndex*spv - Math.max(spv, amount), 0);    
      
      // Next Slides
      for (let i = (activeIndex+1)*spv; i < maxIndex; i += 1) {
        if (slideExist(i)) loadInSlide(i);
      }

      // Prev Slides
      for (let i = minIndex; i < activeIndex*spv; i += 1) {
        if (slideExist(i)) loadInSlide(i);
      }

Pandaii avatar Sep 26 '22 21:09 Pandaii

Swiper v9 doesn't lazy module anymore. If you have similar issues in Swiper 9, open a new issue with a CodeSandbox showing the issue.

nolimits4web avatar Feb 01 '23 11:02 nolimits4web