-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
/
Copy pathvirtual.mjs
42 lines (38 loc) · 1.09 KB
/
virtual.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React from 'react';
function renderVirtual(swiper, slides, virtualData) {
if (!virtualData) return null;
const getSlideIndex = (index) => {
let slideIndex = index;
if (index < 0) {
slideIndex = slides.length + index;
} else if (slideIndex >= slides.length) {
// eslint-disable-next-line
slideIndex = slideIndex - slides.length;
}
return slideIndex;
};
const style = swiper.isHorizontal()
? {
[swiper.rtlTranslate ? 'right' : 'left']: `${virtualData.offset}px`,
}
: {
top: `${virtualData.offset}px`,
};
const { from, to } = virtualData;
const loopFrom = swiper.params.loop ? -slides.length : 0;
const loopTo = swiper.params.loop ? slides.length * 2 : slides.length;
const slidesToRender = [];
for (let i = loopFrom; i < loopTo; i += 1) {
if (i >= from && i <= to) {
slidesToRender.push(slides[getSlideIndex(i)]);
}
}
return slidesToRender.map((child, index) => {
return React.cloneElement(child, {
swiper,
style,
key: `slide-${index}`,
});
});
}
export { renderVirtual };