Skip to content

Commit d3b248b

Browse files
committed
[antoniandre#131] Reset the autoplay timer on mobile drag.
1 parent 637e941 commit d3b248b

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

src/components/vueperslides/vueperslides.vue

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export default {
222222
223223
data: () => ({
224224
isReady: false,
225+
isPaused: false, // When autoplay is true, whether the slideshow in a playing or paused state.
225226
container: null,
226227
slides: {
227228
list: [],
@@ -532,8 +533,8 @@ export default {
532533
// Pause autoplay on mouseover or touch.
533534
if (this.conf.autoplay) {
534535
if (this.conf.pauseOnHover && !hasTouch) {
535-
this.container.addEventListener('mouseover', this.onMouseIn)
536-
this.container.addEventListener('mouseout', this.onMouseOut)
536+
this.container.addEventListener('mouseenter', this.onMouseEnter)
537+
this.container.addEventListener('mouseleave', this.onMouseLeave)
537538
}
538539
else if (this.conf.pauseOnTouch && hasTouch) {
539540
document.addEventListener('touchstart', e => {
@@ -616,16 +617,19 @@ export default {
616617
if (this.conf.parallax) this.getSlideshowOffsetTop(true)
617618
},
618619
619-
onMouseIn () {
620+
// Not on touch device.
621+
onMouseEnter () {
620622
this.mouseOver = true
621-
if (this.conf.pauseOnHover && this.conf.autoplay) this.pauseAutoplay()
623+
if (this.conf.pauseOnHover && this.conf.autoplay) this.isPaused = true
622624
},
623625
624-
onMouseOut () {
626+
// Not on touch device.
627+
onMouseLeave () {
625628
this.mouseOver = false
626-
if (this.conf.pauseOnHover && this.conf.autoplay) this.resumeAutoplay()
629+
if (this.conf.pauseOnHover && this.conf.autoplay) this.isPaused = false
627630
},
628631
632+
// Both on desktop and touch device.
629633
onMouseDown (e) {
630634
if (!this.touchEnabled || this.disable) return
631635
if (!e.touches && this.preventYScroll) e.preventDefault()
@@ -638,8 +642,10 @@ export default {
638642
if (!this.conf.draggingDistance) this.updateTrackTranslation(this.touch.dragStartX)
639643
},
640644
645+
// Both on desktop and touch device.
641646
onMouseMove (e) {
642647
if (this.mouseDown || this.touch.dragging) {
648+
if (this.conf.autoplay) this.isPaused = true // Pause any autoplay while dragging.
643649
if (this.preventYScroll) e.preventDefault()
644650
this.mouseDown = false
645651
this.touch.dragging = true
@@ -658,12 +664,25 @@ export default {
658664
}
659665
},
660666
667+
// Both on desktop and touch device.
668+
// Note: on touch device, the e.target is the same target as the one you start dragging.
661669
onMouseUp (e) {
662670
this.mouseDown = false
663671
664672
// If no mouse move there is nothing to do so don't go further.
665673
if (!this.touch.dragging) return this.cancelSlideChange()
666674
675+
else if (this.conf.autoplay) {
676+
// Since the autoplay is always paused while dragging, resume autoplay if:
677+
// - not touch device and not hover slideshow
678+
// - touch device and pauseOnTouch is set to false.
679+
680+
const hasTouch = 'ontouchstart' in window
681+
if (!hasTouch && !this.mouseOver) this.isPaused = false
682+
// On touch device if pauseOnTouch is false, resume the autoplay on release of a slide dragging.
683+
else if (!this.conf.pauseOnTouch) this.isPaused = false
684+
}
685+
667686
this.touch.dragging = false
668687
const dragAmount = this.conf.draggingDistance ? -this.touch.dragAmount : 0
669688
const dragPercentageStart = (this.touch.dragStartX - this.container.offsetLeft) / this.container.clientWidth
@@ -707,13 +726,11 @@ export default {
707726
},
708727
709728
onSlideshowTouch () {
710-
this.mouseOver = true
711-
this.pauseAutoplay()
729+
this.isPaused = true
712730
},
713731
714732
onOustideTouch () {
715-
this.mouseOver = false
716-
this.resumeAutoplay()
733+
this.isPaused = false
717734
},
718735
719736
// Check if dragging just happened - also for external use.
@@ -812,16 +829,22 @@ export default {
812829
},
813830
814831
pauseAutoplay () {
832+
this.isPaused = true
815833
clearTimeout(this.autoplayTimer)
816834
this.autoplayTimer = 0
817835
this.emit('autoplay-pause')
818836
},
819837
820838
resumeAutoplay () {
839+
this.isPaused = false
840+
this.doAutoplay()
841+
this.emit('autoplay-resume')
842+
},
843+
844+
doAutoplay () {
821845
this.autoplayTimer = setTimeout(() => {
822846
this.goToSlide(this.slides.current + this.conf.slideMultiple, { autoPlaying: true })
823847
}, this.currentSlide.duration || this.conf.duration)
824-
this.emit('autoplay-resume')
825848
},
826849
827850
previous (emit = true) {
@@ -897,7 +920,11 @@ export default {
897920
goToSlide (index, { animation = true, autoPlaying = false, jumping = false, breakpointChange = false, emit = true } = {}) {
898921
if (!this.slidesCount || this.disable) return
899922
900-
if (this.conf.autoplay && autoPlaying) this.pauseAutoplay()
923+
// When autoplay is on and user slides via kbd arrows but not mouse hovering, reset the timer.
924+
if (this.conf.autoplay && !autoPlaying && !this.isPaused) {
925+
this.isPaused = true
926+
this.$nextTick(() => (this.isPaused = false))
927+
}
901928
902929
this.transition.animated = animation
903930
setTimeout(() => (this.transition.animated = false), this.transitionSpeed)
@@ -964,9 +991,7 @@ export default {
964991
965992
this.slides.activeId = this.slides.list[this.slides.current].id
966993
967-
if (this.conf.autoplay && autoPlaying && !(this.conf.pauseOnHover && this.mouseOver)) {
968-
this.resumeAutoplay()
969-
}
994+
if (this.conf.autoplay && autoPlaying && !this.isPaused) this.doAutoplay()
970995
971996
if (this.slidesCount) {
972997
// First use of goToSlide is while init, so should not propagate an event.
@@ -996,7 +1021,9 @@ export default {
9961021
this.slides.list.push(newSlide)
9971022
// If the slideshow was initialized with no slides and with autoplay, resume the autoplay
9981023
// when the first slide is added.
999-
if (this.isReady && this.slidesCount === 1 && this.conf.autoplay) this.resumeAutoplay()
1024+
if (this.isReady && this.slidesCount === 1 && this.conf.autoplay && this.isPaused) {
1025+
this.isPaused = false
1026+
}
10001027
10011028
return this.slidesCount
10021029
},
@@ -1066,12 +1093,12 @@ export default {
10661093
this.$refs.track.removeEventListener(hasTouch ? 'touchstart' : 'mousedown', this.onMouseDown, { passive: !this.preventYScroll })
10671094
document.removeEventListener(hasTouch ? 'touchmove' : 'mousemove', this.onMouseMove, { passive: !this.preventYScroll })
10681095
document.removeEventListener(hasTouch ? 'touchend' : 'mouseup', this.onMouseUp, { passive: true })
1096+
}
1097+
},
10691098
1070-
this.container.removeEventListener('mouseover', this.onMouseIn)
1071-
this.container.removeEventListener('mouseout', this.onMouseOut)
1072-
document.removeEventListener('touchstart', e => {
1073-
this[this.$el.contains(e.target) ? 'onSlideshowTouch' : 'onOustideTouch']()
1074-
})
1099+
watch: {
1100+
isPaused (bool) {
1101+
this[bool ? 'pauseAutoplay' : 'resumeAutoplay']()
10751102
}
10761103
},
10771104
@@ -1081,12 +1108,19 @@ export default {
10811108
10821109
beforeDestroy () {
10831110
this.removeEventListeners()
1111+
10841112
if (this.pageScrollingElement) {
10851113
document.querySelector(this.pageScrollingElement).removeEventListener('scroll', this.onScroll)
10861114
}
10871115
else document.removeEventListener('scroll', this.onScroll)
10881116
document.removeEventListener('scroll', this.onScroll)
10891117
window.removeEventListener('resize', this.onResize)
1118+
document.removeEventListener('touchstart', e => {
1119+
this[this.$el.contains(e.target) ? 'onSlideshowTouch' : 'onOustideTouch']()
1120+
})
1121+
1122+
this.container.removeEventListener('mouseenter', this.onMouseEnter)
1123+
this.container.removeEventListener('mouseleave', this.onMouseLeave)
10901124
}
10911125
}
10921126
</script>

0 commit comments

Comments
 (0)