Skip to content

Commit 2f88270

Browse files
committed
fix(Swiper): 修复轮播图最后一张无法拖拽至第一张
1 parent 60e6917 commit 2f88270

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/core/src/Swiper/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,31 @@ const Swiper = (porps: SwiperProps) => {
8989

9090
// 开始拖拽终止定时器
9191
const onScrollBeginDrag = () => {
92-
if (autoplay) clearInterval(timer.current as unknown as number);
92+
if (autoplay) {
93+
clearInterval(timer.current as unknown as number);
94+
// setCurIndex(0);
95+
}
9396
};
9497

9598
// 停止拖拽开启定时器
9699
const onScrollEndDrag = () => {
97100
if (autoplay) autoPlay();
98101
};
99102

100-
// 拖拽成功
101103
const onMomentumScrollEnd = (e: NativeSyntheticEvent<NativeScrollEvent>) => {
102104
e.persist();
103105
let offSetX = e.nativeEvent.contentOffset.x;
104106
let mentWidth = e.nativeEvent.layoutMeasurement.width;
105107
let page = offSetX / mentWidth;
106-
setCurIndex(page);
108+
109+
if (page === dataSource.length) {
110+
setCurIndex(0);
111+
scrollToRef.current.scrollTo({ x: 0, y: 0, animated: false });
112+
} else {
113+
setCurIndex(page);
114+
}
107115
};
116+
108117
// 点击原点跳转
109118
const onClickDot = (index: number) => {
110119
setCurIndex(index);
@@ -123,7 +132,7 @@ const Swiper = (porps: SwiperProps) => {
123132
onMomentumScrollEnd={onMomentumScrollEnd}
124133
onContentSizeChange={onContentSizeChange}
125134
>
126-
{dataSource.map((item: dataSourceType, index: number) => {
135+
{[...dataSource, dataSource[0]].map((item: dataSourceType, index: number) => {
127136
return (
128137
<View key={index} style={{ width, height }}>
129138
<View style={{ padding: 12 }}>

0 commit comments

Comments
 (0)