From 955b5ede0d975231feab55258823a9538752c0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=9D=B1=E6=BE=94?= Date: Sat, 20 Nov 2021 11:35:50 +0800 Subject: [PATCH] fix: sliding error with no loop Fixes #24 --- src/Carousel.tsx | 17 +++++++++++++++-- src/utils/log.ts | 7 +++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/utils/log.ts diff --git a/src/Carousel.tsx b/src/Carousel.tsx index a914c0f1..65177cbd 100644 --- a/src/Carousel.tsx +++ b/src/Carousel.tsx @@ -248,8 +248,12 @@ function Carousel( const offsetX = useDerivedValue(() => { const x = handlerOffsetX.value % computedAnimResult.TOTAL_WIDTH; + + if (!loop) { + return handlerOffsetX.value; + } return isNaN(x) ? 0 : x; - }, [computedAnimResult]); + }, [computedAnimResult, loop]); useAnimatedReaction( () => offsetX.value, @@ -323,14 +327,23 @@ function Carousel( } const page = Math.round(handlerOffsetX.value / width); + const velocityPage = Math.round( (handlerOffsetX.value + e.velocityX) / width ); - const pageWithVelocity = Math.min( + + let pageWithVelocity = Math.min( page + 1, Math.max(page - 1, velocityPage) ); + if (!loop) { + pageWithVelocity = Math.max( + -(data.length - 1), + Math.min(0, pageWithVelocity) + ); + } + if (loop) { handlerOffsetX.value = _withAnimationCallback( pageWithVelocity * width diff --git a/src/utils/log.ts b/src/utils/log.ts new file mode 100644 index 00000000..23aea112 --- /dev/null +++ b/src/utils/log.ts @@ -0,0 +1,7 @@ +/** + * In worklet + * e.g. runOnJS(lop)(...); + */ +export function log(msg: any) { + console.log(msg); +}