Skip to content

Commit 94fefd4

Browse files
committed
Merge branch 'pr/6544'
2 parents 1cb3233 + 7794f9c commit 94fefd4

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/core/core.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ class Swiper {
586586
// Init Flag
587587
swiper.initialized = true;
588588

589+
preload(swiper);
590+
589591
// Emit
590592
swiper.emit('init');
591593
swiper.emit('afterInit');

src/core/update/updateActiveIndex.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import preload from '../../shared/process-lazy-preloader.js';
1+
import { preload } from '../../shared/process-lazy-preloader.js';
22

33
export function getActiveIndexByTranslate(swiper) {
44
const { slidesGrid, params } = swiper;
@@ -87,8 +87,9 @@ export default function updateActiveIndex(newActiveIndex) {
8787
activeIndex,
8888
});
8989

90-
preload(swiper);
91-
90+
if (swiper.initialized) {
91+
preload(swiper);
92+
}
9293
swiper.emit('activeIndexChange');
9394
swiper.emit('snapIndexChange');
9495
if (previousRealIndex !== realIndex) {

src/shared/process-lazy-preloader.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export const processLazyPreloader = (swiper, imageEl) => {
99
};
1010

1111
const unlazy = (swiper, index) => {
12-
const imageEl = swiper.slides[index].querySelector('loading="lazy"');
12+
if (!swiper.slides[index]) return;
13+
const imageEl = swiper.slides[index].querySelector('[loading="lazy"]');
1314
if (imageEl) imageEl.removeAttribute('loading');
1415
};
1516

@@ -19,15 +20,24 @@ export const preload = (swiper) => {
1920
const len = swiper.slides.length;
2021
if (!len || !amount || amount < 0) return;
2122
amount = Math.min(amount, len);
22-
const active = swiper.activeSlide;
23+
const slidesPerView =
24+
swiper.params.slidesPerView === 'auto'
25+
? swiper.slidesPerViewDynamic()
26+
: Math.ceil(swiper.params.slidesPerView);
27+
const activeIndex = swiper.activeIndex;
28+
const slideIndexLastInView = activeIndex + slidesPerView - 1;
2329
if (swiper.params.rewind) {
24-
for (let i = active - amount; i <= active + amount; i += 1) {
25-
const reali = ((i % len) + len) % len;
26-
if (reali !== active) unlazy(swiper, reali);
30+
for (let i = activeIndex - amount; i <= slideIndexLastInView + amount; i += 1) {
31+
const realIndex = ((i % len) + len) % len;
32+
if (realIndex !== activeIndex && realIndex > slideIndexLastInView) unlazy(swiper, realIndex);
2733
}
2834
} else {
29-
for (let i = Math.max(active - amount, 0); i <= Math.min(active + amount, len - 1); i += 1) {
30-
if (i !== active) unlazy(swiper, i);
35+
for (
36+
let i = Math.max(slideIndexLastInView - amount, 0);
37+
i <= Math.min(slideIndexLastInView + amount, len - 1);
38+
i += 1
39+
) {
40+
if (i !== activeIndex && i > slideIndexLastInView) unlazy(swiper, i);
3141
}
3242
}
3343
};

0 commit comments

Comments
 (0)