@@ -7,6 +7,7 @@ export interface CarouselProps {
7
7
className ?: string ;
8
8
startIndex ?: number ;
9
9
draggable ?: boolean ;
10
+ clickDragThreshold ?: number ;
10
11
duration ?: number ;
11
12
easing ?: string ;
12
13
threshold ?: number ;
@@ -54,6 +55,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
54
55
duration = 300 ,
55
56
easing = 'ease' ,
56
57
threshold = 20 ,
58
+ clickDragThreshold = 10 ,
57
59
loop = true ,
58
60
rtl = false ,
59
61
autoPlay = props . autoplay || false ,
@@ -88,6 +90,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
88
90
) ;
89
91
90
92
const [ activeIndex , setActiveIndex ] = useState ( getIndex ( startIndex ) ) ;
93
+ const [ isDragging , setDragging ] = useState ( false ) ;
91
94
92
95
const enableTransition = useCallback ( ( ) => {
93
96
innerRef . current . style . transition = `transform ${ duration } ms ${ easing } ` ;
@@ -296,6 +299,10 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
296
299
const nextOffset = nextIndex * state . wrapWidth ;
297
300
const dragOffset = state . endX - state . startX ;
298
301
302
+ if ( ! isDragging && Math . abs ( dragOffset ) > clickDragThreshold ) {
303
+ setDragging ( true ) ;
304
+ }
305
+
299
306
// 阻尼
300
307
// if ((activeIndex === 0 && dragOffset > 0) || (activeIndex === count - 1 && dragOffset < 0)) {
301
308
// dragOffset *= 0.35;
@@ -310,6 +317,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
310
317
e . stopPropagation ( ) ;
311
318
const state = stateRef . current ;
312
319
state . pressDown = false ;
320
+ setDragging ( false ) ;
313
321
enableTransition ( ) ;
314
322
if ( state . endX ) {
315
323
updateAfterDrag ( ) ;
@@ -430,6 +438,7 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
430
438
{
431
439
'Carousel--draggable' : draggable ,
432
440
'Carousel--rtl' : rtl ,
441
+ 'Carousel--dragging' : isDragging ,
433
442
} ,
434
443
className ,
435
444
) }
0 commit comments