Skip to content

Commit 598d104

Browse files
committed
Refactor onMouseDown, cleanup and add slideMultiple option.
1 parent 204f4bc commit 598d104

File tree

1 file changed

+71
-44
lines changed

1 file changed

+71
-44
lines changed

src/components/VueperSlides.vue

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ export default {
142142
slideImageInside: {
143143
type: Boolean,
144144
default: false
145+
},
146+
slideMultiple: {
147+
type: [Boolean, Number],
148+
default: false
145149
}
146150
},
147151
data: () => ({
@@ -377,7 +381,7 @@ export default {
377381
378382
this.mouseDown = true
379383
380-
if (this.draggingDistance) {
384+
if (this.conf.draggingDistance) {
381385
// Store drag start in var for distance calculation in onMouseUp().
382386
this.touch.dragStartX = 'ontouchstart' in window ? e.touches[0].clientX : e.clientX
383387
} else {
@@ -401,7 +405,7 @@ export default {
401405
this.cloneSlides()
402406
}
403407
404-
if (this.draggingDistance) {
408+
if (this.conf.draggingDistance) {
405409
this.touch.dragAmount = this.getDragAmount(e)
406410
let dragAmountPercentage = this.touch.dragAmount / this.container.clientWidth
407411
@@ -414,45 +418,48 @@ export default {
414418
},
415419
416420
onMouseUp (e) {
417-
if (this.mouseDown || this.touch.dragging) {
418-
this.mouseDown = false
419-
this.touch.dragging = false
421+
this.mouseDown = false
420422
421-
let slideOnDragEnd
422-
if (this.draggingDistance) {
423-
let dragAmount = this.touch.dragAmount
424-
let dragAmountPercentage = dragAmount / this.container.clientWidth
423+
// If no mouse move there is nothing to do so don't go further.
424+
if (!this.touch.dragging) return this.cancelSlideChange()
425425
426-
slideOnDragEnd = this.slides.current
427-
if (Math.abs(dragAmount) >= this.draggingDistance) {
428-
slideOnDragEnd += dragAmount > 0 ? -1 : 1
429-
}
430-
} else {
431-
// When the drag is realeased, calculate if the drag ends before or after the 50%-slideshow-width threshold.
432-
// Then finish the sliding toward that slide.
433-
slideOnDragEnd = - (Math.round(this.transition.currentTranslation / 100) + (this.clones.length ? 1 : 0))
434-
}
426+
this.touch.dragging = false
427+
let itemsToSlide = this.conf.slideMultiple || 1
428+
let dragAmount = - this.touch.dragAmount
429+
let realCurrentSlideIndex = this.slides.current + !!this.clones.length * 1// Takes clones in account if any.
430+
let dragAmountRef50percent = - (this.transition.currentTranslation + realCurrentSlideIndex * 100)
431+
let forwards = (dragAmount || dragAmountRef50percent) > 0
435432
436-
let { nextSlide } = this.getSlideInRange(slideOnDragEnd)
433+
let reasonsToCancelSliding = [
434+
// Dragging distance conf is set & drag amount is lesser than dragging distance conf.
435+
Math.abs(dragAmount) < this.conf.draggingDistance,
437436
438-
// If drag is not allowed (`arrowNextDisabled` = true) and dragging beyond last slide,
439-
// cancel sliding and snap back to last slide.
440-
if (this.arrowNextDisabled && this.conf.autoplay && nextSlide === 0) {
441-
nextSlide = this.slides.count - 1
442-
}
437+
// Dragging distance conf is not set & dragging is lesser than 50%.
438+
!this.conf.draggingDistance && Math.abs(dragAmountRef50percent) < 50,
443439
444-
// Only call `goToSlide` if the drag ends on a slide that is different than the currentSlide.
445-
if (nextSlide !== this.slides.current) {
446-
this.goToSlide(slideOnDragEnd)
447-
} else {
448-
// Apply transition to snap back to current slide.
449-
this.transition.currentTranslation = - (this.slides.current + (this.clones.length ? 1 : 0)) * 100
450-
}
440+
// arrowNext is disabled and dragging beyond last slide.
441+
this.arrowPrevDisabled && !this.slides.current && !forwards,
451442
452-
this.touch.dragStartX = null
453-
this.touch.dragAmount = null
454-
this.enableScroll()
443+
// arrowPrev is disabled and dragging beyond first slide.
444+
this.arrowNextDisabled && this.slides.current === this.slides.count - 1 && forwards
445+
]
446+
447+
// If no reason to cancel sliding.
448+
if (reasonsToCancelSliding.indexOf(true) === -1) {
449+
let targetSlide = this.slides.current + itemsToSlide * (forwards ? 1 : -1)
450+
this.goToSlide(targetSlide)
455451
}
452+
453+
else this.cancelSlideChange()
454+
455+
this.touch.dragStartX = null
456+
this.touch.dragAmount = null
457+
// this.enableScroll()
458+
},
459+
460+
// Dragging did not pass conditions to change slide, snap back to current slide.
461+
cancelSlideChange () {
462+
this.transition.currentTranslation = - (this.slides.current + (this.clones.length ? 1 : 0)) * 100
456463
},
457464
458465
getDragPercentage (e) {
@@ -496,11 +503,13 @@ export default {
496503
},
497504
498505
previous () {
499-
this.goToSlide(this.slides.current - 1)
506+
let itemsToSlide = this.conf.slideMultiple || 1
507+
this.goToSlide(this.slides.current - itemsToSlide)
500508
},
501509
502510
next () {
503-
this.goToSlide(this.slides.current + 1)
511+
let itemsToSlide = this.conf.slideMultiple || 1
512+
this.goToSlide(this.slides.current + itemsToSlide)
504513
},
505514
506515
refreshParallax () {
@@ -510,16 +519,33 @@ export default {
510519
}, 100)
511520
},
512521
513-
getSlideInRange (index) {
522+
getSlideInRange (index, autoPlaying) {
523+
let clone = null
524+
525+
// If infinite enabled, going out of range takes the first slide from the other end.
526+
if (this.conf.infinite && index === -1) clone = 0
527+
else if (this.conf.infinite && index === this.slides.count) clone = 1
528+
529+
// If going beyond slides count, take the modulo as next slide index.
530+
// E.g. If we want to access slide 9 and there are only 6 slides, go to slide 3.
531+
let newIndex = index % this.slides.count + (index < 0 ? this.slides.count : 0)
532+
533+
if (this.conf.disableArrowsOnEdges && (index < 0 || index > this.slides.count - 1) && !autoPlaying) {
534+
newIndex = this.slides.current
535+
}
536+
537+
return { nextSlide: newIndex, clone: clone }
538+
},
539+
540+
getSlideInRange_old (index) {
514541
let clone = null
515542
516543
// If infinite enabled, going out of range takes the first slide from the other end.
517544
if (this.clones.length) {
518545
if (index < 0) {
519546
index = this.slides.count - 1
520547
clone = 0
521-
}
522-
else if (index > this.slides.count - 1) {
548+
} else if (index > this.slides.count - 1) {
523549
index = 0
524550
clone = 1
525551
}
@@ -554,19 +580,20 @@ export default {
554580
setTimeout(() => this.transition.animated = false, this.transitionSpeed)
555581
556582
// Get the next slide index and whether it's a clone.
557-
let { nextSlide, clone: nextSlideIsClone } = this.getSlideInRange(index)
583+
let { nextSlide, clone: nextSlideIsClone } = this.getSlideInRange(index, autoPlaying)
558584
559585
// Emit event. First use of `goToSlide` is while init, so should not propagate an event.
560586
if (this.isReady && !jumping) {
561587
this.emit('before-slide', true, nextSlide)
562588
589+
// Refresh clones.
563590
if (nextSlideIsClone !== null) this.cloneSlides()
564591
}
565592
566593
// Disable arrows if `disableArrowsOnEdges` is on and there is no slide to go to on that end.
567594
if (this.conf.arrows && this.conf.disableArrowsOnEdges) {
568-
this.arrowPrevDisabled = nextSlide === 0
569-
this.arrowNextDisabled = nextSlide === this.slides.count - 1
595+
this.arrowPrevDisabled = nextSlide === 0 || (this.conf.slideMultiple && nextSlide - this.conf.slideMultiple) < 0
596+
this.arrowNextDisabled = nextSlide === this.slides.count - 1 || (this.conf.slideMultiple && nextSlide + this.conf.slideMultiple) > this.slides.count - 1
570597
}
571598
572599
// Infinite sliding with cloned slides:
@@ -585,7 +612,7 @@ export default {
585612
if (!tooLateToSetTimeout) {
586613
this.transition.speed = 0
587614
this.goToSlide(nextSlideIsClone ? 0 : this.slides.count - 1, { animation: false, jumping: true })
588-
setTimeout(() => this.transition.speed = this.conf.transitionSpeed, 10)
615+
setTimeout(() => this.transition.speed = this.conf.transitionSpeed, 50)
589616
}
590617
}, this.transition.speed - 50)
591618
}
@@ -682,7 +709,7 @@ export default {
682709
...(this.$props.breakpoints && this.$props.breakpoints[this.breakpointsData.current] || {})
683710
}
684711
685-
if (conf.fade) {
712+
if (conf.fade || conf.disableArrowsOnEdges) {
686713
conf.infinite = false
687714
}
688715

0 commit comments

Comments
 (0)