Skip to content

Commit 207b804

Browse files
committed
Merge branch 'mouse-scroll-support' of github.com:ismail9k/vue3-carousel into mouse-scroll-support
2 parents 6a8cd87 + 2bc2791 commit 207b804

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/components/Carousel/Carousel.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,50 @@ export const Carousel = defineComponent({
366366
document.removeEventListener('keydown', handleArrowKeys)
367367
}
368368

369+
const handleScroll = throttle((event: Event): void => {
370+
if (!config.mouseScroll || isSliding.value) {
371+
return
372+
}
373+
374+
// Prevent default scrolling behavior when wheel navigation is enabled
375+
event.preventDefault()
376+
377+
const wheelEvent = event as WheelEvent
378+
379+
// Add sensitivity threshold to prevent small movements from triggering navigation
380+
const threshold = config.mouseScrollThreshold ?? 30 // Default to 30 if undefined
381+
382+
// Determine scroll direction
383+
const deltaY = Math.abs(wheelEvent.deltaY) > threshold ? wheelEvent.deltaY : 0
384+
const deltaX = Math.abs(wheelEvent.deltaX) > threshold ? wheelEvent.deltaX : 0
385+
386+
// If neither delta exceeds the threshold, don't navigate
387+
if (deltaY === 0 && deltaX === 0) {
388+
return
389+
}
390+
391+
const isScrollingDown = deltaY > 0
392+
const isScrollingRight = deltaX > 0
393+
394+
// Determine direction based on carousel orientation and scroll direction
395+
if (
396+
(isVertical.value && isScrollingDown) ||
397+
(!isVertical.value && isScrollingRight)
398+
) {
399+
if (isReversed.value) {
400+
prev()
401+
} else {
402+
next()
403+
}
404+
} else {
405+
if (isReversed.value) {
406+
next()
407+
} else {
408+
prev()
409+
}
410+
}
411+
}, 150)
412+
369413
/**
370414
* Autoplay
371415
*/
@@ -406,6 +450,7 @@ export const Carousel = defineComponent({
406450
isReversed: isReversed.value,
407451
dragged: { x: deltaX, y: deltaY },
408452
effectiveSlideSize: effectiveSlideSize.value,
453+
threshold: config.mouseDragThreshold as number,
409454
})
410455

411456
activeSlideIndex.value = config.wrapAround

0 commit comments

Comments
 (0)