Skip to content

Commit

Permalink
fix(core): fix(core): lazyPreloadPrevNext not working with grid
Browse files Browse the repository at this point in the history
fixes #6725
  • Loading branch information
nolimits4web committed Jun 7, 2023
1 parent 69acab4 commit 883f006
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/modules/grid/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default function Grid({ swiper, extendParams }) {
const initSlides = (slidesLength) => {
const { slidesPerView } = swiper.params;
const { rows, fill } = swiper.params.grid;
slidesPerRow = slidesNumberEvenToRows / rows;
numFullColumns = Math.floor(slidesLength / rows);
if (Math.floor(slidesLength / rows) === slidesLength / rows) {
slidesNumberEvenToRows = slidesLength;
Expand All @@ -33,6 +32,7 @@ export default function Grid({ swiper, extendParams }) {
if (slidesPerView !== 'auto' && fill === 'row') {
slidesNumberEvenToRows = Math.max(slidesNumberEvenToRows, slidesPerView * rows);
}
slidesPerRow = slidesNumberEvenToRows / rows;
};

const updateSlide = (i, slide, slidesLength, getDirectionLabel) => {
Expand Down Expand Up @@ -72,6 +72,8 @@ export default function Grid({ swiper, extendParams }) {
row = Math.floor(i / slidesPerRow);
column = i - row * slidesPerRow;
}
slide.row = row;
slide.column = column;
slide.style[getDirectionLabel('margin-top')] =
row !== 0 ? spaceBetween && `${spaceBetween}px` : '';
};
Expand Down
13 changes: 13 additions & 0 deletions src/shared/process-lazy-preloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ export const preload = (swiper) => {
? swiper.slidesPerViewDynamic()
: Math.ceil(swiper.params.slidesPerView);
const activeIndex = swiper.activeIndex;
if (swiper.params.grid && swiper.params.grid.rows > 1) {
const activeColumn = activeIndex;
const preloadColumns = [activeColumn - amount];
preloadColumns.push(
...Array.from({ length: amount }).map((_, i) => {
return activeColumn + slidesPerView + i;
}),
);
swiper.slides.forEach((slideEl, i) => {
if (preloadColumns.includes(slideEl.column)) unlazy(swiper, i);
});
return;
}
const slideIndexLastInView = activeIndex + slidesPerView - 1;
if (swiper.params.rewind || swiper.params.loop) {
for (let i = activeIndex - amount; i <= slideIndexLastInView + amount; i += 1) {
Expand Down

0 comments on commit 883f006

Please sign in to comment.