Skip to content

Commit 62c616a

Browse files
committed
fix(swiper):优化闪烁跳动指定位置
1 parent a5c0eca commit 62c616a

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

packages/core/src/ImageViewer/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ export interface ImageViewerProps extends ViewProps {
2020
height?: number;
2121
/** 图像源(远程URL或本地文件资源 */
2222
src?: string | ImageViewerDataSourceProps[];
23+
/** 默认显示第几张图片 */
24+
defaultIndex?: number;
2325
}
2426

2527
function ImageViewer(props: ImageViewerProps) {
26-
const { width = 150, height = 150, src = defaultImage, ...others } = props;
28+
const { width = 150, height = 150, src = defaultImage, defaultIndex = 0, ...others } = props;
2729
const [visible, setVisible] = useState(false);
2830
const [index, setIndex] = useState<number>(0);
2931
const fadeAnim = useRef(new Animated.Value(0)).current;
@@ -42,7 +44,7 @@ function ImageViewer(props: ImageViewerProps) {
4244

4345
const imgUrl = useMemo(() => {
4446
if (Array.isArray(src)) {
45-
return src[0].url;
47+
return src[defaultIndex].url;
4648
}
4749
return src;
4850
}, [src]);

packages/core/src/Swiper/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ const Swiper = (porps: SwiperProps) => {
5252
let timer = useRef<NodeJS.Timeout | undefined>();
5353
const scrollToRef: any = useRef();
5454

55-
useEffect(() => {
55+
// 设置初始变量
56+
const onContentSizeChange = () => {
5657
if (scrollToRef && scrollToRef.current && index !== 0) {
57-
setTimeout(() => {
58-
onClickDot(index);
59-
scrollToRef.current.scrollTo({ x: width * index, y: 0, animated: true });
60-
}, 0);
58+
setCurIndex(index);
59+
scrollToRef.current.scrollTo({ x: width * index, y: 0, animated: false });
6160
}
62-
}, [scrollToRef, index]);
61+
};
6362

6463
// 自动播放
6564
const autoPlay = useCallback(() => {
@@ -122,6 +121,7 @@ const Swiper = (porps: SwiperProps) => {
122121
onScrollBeginDrag={onScrollBeginDrag}
123122
onScrollEndDrag={onScrollEndDrag}
124123
onMomentumScrollEnd={onMomentumScrollEnd}
124+
onContentSizeChange={onContentSizeChange}
125125
>
126126
{dataSource.map((item: dataSourceType, index: number) => {
127127
return (

0 commit comments

Comments
 (0)