Skip to content

Commit b9ae2e5

Browse files
committed
feat: enhance dragging behavior in Carousel component to prevent excessive sliding
1 parent f6ca695 commit b9ae2e5

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/components/Carousel/Carousel.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,21 +391,9 @@ export const Carousel = defineComponent({
391391
const currentX = 'touches' in event ? event.touches[0].clientX : event.clientX
392392
const currentY = 'touches' in event ? event.touches[0].clientY : event.clientY
393393

394-
const tmpDraggedX = currentX - startPosition.x
395-
const tmpDraggedY = currentY - startPosition.y
396-
397-
if (!config.wrapAround && config.preventExcessiveDragging) {
398-
const isAtMinIndex = activeSlideIndex.value === minSlideIndex.value;
399-
const isAtMaxIndex = activeSlideIndex.value === maxSlideIndex.value;
400-
401-
if ((Math.abs(tmpDraggedX) > Math.abs(tmpDraggedY) ?
402-
(tmpDraggedX > 0 && isAtMinIndex) || (tmpDraggedX < 0 && isAtMaxIndex) :
403-
(tmpDraggedY > 0 && isAtMinIndex) || (tmpDraggedY < 0 && isAtMaxIndex))) return;
404-
}
405-
406394
// Calculate deltas for X and Y axes
407-
dragged.x = tmpDraggedX
408-
dragged.y = tmpDraggedY
395+
dragged.x = currentX - startPosition.x
396+
dragged.y = currentY - startPosition.y
409397

410398
const draggedSlides = getDraggedSlidesCount({
411399
isVertical: isVertical.value,
@@ -701,7 +689,18 @@ export const Carousel = defineComponent({
701689
// Include user drag interaction offset
702690
const dragOffset = isVertical.value ? dragged.y : dragged.x
703691

704-
const totalOffset = scrolledOffset + dragOffset
692+
let totalOffset = scrolledOffset + dragOffset
693+
694+
if (!config.wrapAround && config.preventExcessiveDragging) {
695+
const maxSlidingValue =
696+
(slidesCount.value - config.itemsToShow) * effectiveSlideSize.value
697+
698+
totalOffset = getNumberInRange({
699+
val: totalOffset,
700+
min: -1 * maxSlidingValue,
701+
max: 0,
702+
})
703+
}
705704

706705
return `translate${translateAxis}(${totalOffset}px)`
707706
})

0 commit comments

Comments
 (0)