Skip to content

Commit 2013db9

Browse files
committed
feat: add throttleTime option to mouseWheel configuration and improve wheel event handling in Carousel
1 parent 6dbabe4 commit 2013db9

File tree

6 files changed

+51
-16
lines changed

6 files changed

+51
-16
lines changed

docs/config.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ Both `mouseDrag` and `touchDrag` properties accept either a boolean value or a `
5858

5959
The `mouseWheel` property accepts either a boolean value or a `WheelConfig` object with the following properties:
6060

61-
| Property | Type | Default | Description |
62-
|-------------|----------|---------|--------------------------------------------------------------------------------------------|
63-
| `threshold` | `number` | 10 | Controls the wheel movement threshold required to trigger a slide transition. Higher values require more scrolling to trigger a slide change. |
61+
| Property | Type | Default | Description |
62+
|---------------|----------|---------|--------------------------------------------------------------------------------------------|
63+
| `threshold` | `number` | 10 | Controls the wheel movement threshold required to trigger a slide transition. Higher values require more scrolling to trigger a slide change. |
64+
| `throttleTime` | `number` | 100 | Controls how quickly wheel events are processed in milliseconds. Lower values make scrolling more responsive but may cause performance issues. By default, uses either this value or 1/3 of the transition time, whichever is smaller. |
6465

6566
### Example
6667

6768
```vue
6869
<template>
6970
<Carousel
70-
:mouseWheel="{ threshold: 20 }"
71+
:mouseWheel="{ threshold: 20, throttleTime: 50 }"
7172
>
7273
<!-- Slides -->
7374
</Carousel>

playground/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const defaultConfig = {
4141
autoplay: null,
4242
wrapAround: true,
4343
height: '200',
44-
mouseScroll: true,
44+
mouseWheel: true,
4545
dir: 'left-to-right',
4646
breakpointMode: 'carousel',
4747
gap: 10,

src/components/Carousel/Carousel.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
CarouselConfig,
2323
createSlideRegistry,
2424
DEFAULT_CONFIG,
25+
DEFAULT_DRAG_THRESHOLD,
2526
DIR_MAP,
2627
injectCarousel,
2728
NonNormalizedDir,
@@ -418,11 +419,11 @@ export const Carousel = defineComponent({
418419
}) => {
419420
const threshold = isTouch
420421
? typeof config.touchDrag === 'object'
421-
? (config.touchDrag?.threshold ?? 0.3)
422-
: 0.3
422+
? (config.touchDrag?.threshold ?? DEFAULT_DRAG_THRESHOLD)
423+
: DEFAULT_DRAG_THRESHOLD
423424
: typeof config.mouseDrag === 'object'
424-
? (config.mouseDrag?.threshold ?? 0.3)
425-
: 0.3
425+
? (config.mouseDrag?.threshold ?? DEFAULT_DRAG_THRESHOLD)
426+
: DEFAULT_DRAG_THRESHOLD
426427

427428
const draggedSlides = getDraggedSlidesCount({
428429
isVertical: isVertical.value,

src/composables/useWheel.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { ComputedRef, Ref, computed } from 'vue'
22

33
import { CarouselConfig } from '@/shared'
4+
import {
5+
DEFAULT_MOUSE_WHEEL_THRESHOLD,
6+
DEFAULT_WHEEL_THROTTLE_TIME,
7+
} from '@/shared/constants'
48
import { throttle } from '@/utils'
59

610
interface UseWheelOptions {
@@ -28,6 +32,20 @@ export function useWheel(options: UseWheelOptions) {
2832
return typeof isSliding === 'boolean' ? isSliding : isSliding.value
2933
})
3034

35+
// Use a shorter throttle duration for wheel events to improve responsiveness
36+
// Use either a custom wheel throttle time or 1/3 of the transition time
37+
const wheelThrottleTime = computed(() => {
38+
// If mouseWheel is an object with a throttleTime property, use that
39+
if (typeof config.mouseWheel === 'object' && config.mouseWheel.throttleTime) {
40+
return config.mouseWheel.throttleTime
41+
}
42+
// Otherwise use a fraction of the transition time for better responsiveness
43+
return Math.min(
44+
DEFAULT_WHEEL_THROTTLE_TIME,
45+
config.transition ? Math.floor(config.transition / 3) : DEFAULT_WHEEL_THROTTLE_TIME
46+
)
47+
})
48+
3149
const handleScroll = throttle((event: Event): void => {
3250
if (!config.mouseWheel || sliding.value) {
3351
return
@@ -40,7 +58,9 @@ export function useWheel(options: UseWheelOptions) {
4058

4159
// Add sensitivity threshold to prevent small movements from triggering navigation
4260
const threshold =
43-
typeof config.mouseWheel === 'object' ? (config.mouseWheel.threshold ?? 10) : 10
61+
typeof config.mouseWheel === 'object'
62+
? (config.mouseWheel.threshold ?? DEFAULT_MOUSE_WHEEL_THRESHOLD)
63+
: DEFAULT_MOUSE_WHEEL_THRESHOLD
4464

4565
// Determine scroll direction
4666
const deltaY = Math.abs(wheelEvent.deltaY) > threshold ? wheelEvent.deltaY : 0
@@ -51,24 +71,33 @@ export function useWheel(options: UseWheelOptions) {
5171
return
5272
}
5373

54-
const isScrollingDown = deltaY > 0
55-
const isScrollingRight = deltaX > 0
74+
// Determine primary delta based on carousel orientation
75+
const primaryDelta = vertical.value ? deltaY : deltaX
76+
77+
// If primaryDelta is 0, use the other delta as fallback
78+
const effectiveDelta =
79+
primaryDelta !== 0 ? primaryDelta : vertical.value ? deltaX : deltaY
80+
81+
// Positive delta means scrolling down/right
82+
const isScrollingForward = effectiveDelta > 0
5683

57-
// Determine direction based on carousel orientation and scroll direction
58-
if ((vertical.value && isScrollingDown) || (!vertical.value && isScrollingRight)) {
84+
// Apply navigation based on scroll direction and carousel configuration
85+
if (isScrollingForward) {
86+
// Scrolling down/right
5987
if (reversed.value) {
6088
prev()
6189
} else {
6290
next()
6391
}
6492
} else {
93+
// Scrolling up/left
6594
if (reversed.value) {
6695
next()
6796
} else {
6897
prev()
6998
}
7099
}
71-
}, 16)
100+
}, wheelThrottleTime.value) // Use the computed throttle time
72101

73102
return {
74103
handleScroll,

src/shared/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export const SNAP_ALIGN_OPTIONS = [
4545
'center-odd',
4646
] as const
4747

48+
export const DEFAULT_MOUSE_WHEEL_THRESHOLD = 10
49+
export const DEFAULT_WHEEL_THROTTLE_TIME = 100
50+
export const DEFAULT_DRAG_THRESHOLD = 0.3
51+
4852
export const DEFAULT_CONFIG: CarouselConfig = {
4953
autoplay: 0,
5054
breakpointMode: BREAKPOINT_MODE_OPTIONS[0],
@@ -60,7 +64,6 @@ export const DEFAULT_CONFIG: CarouselConfig = {
6064
modelValue: 0,
6165
mouseDrag: true,
6266
mouseWheel: false,
63-
mouseScrollThreshold: 10,
6467
pauseAutoplayOnHover: false,
6568
preventExcessiveDragging: false,
6669
slideEffect: SLIDE_EFFECTS[0],

src/shared/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type DragConfig = {
3333
}
3434
export type WheelConfig = {
3535
threshold?: number
36+
throttleTime?: number
3637
}
3738

3839
export type CarouselConfig = {

0 commit comments

Comments
 (0)