Skip to content

Commit 44de9f9

Browse files
authored
Add go to slide API (#54)
1 parent fa7804e commit 44de9f9

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/components/Carousel/Carousel.vue

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,6 @@ export default {
104104
onResizeFn: null,
105105
onScrollFn: null
106106
}),
107-
watch: {
108-
currentPage(current, previous) {
109-
if (current !== previous) {
110-
/**
111-
* Page changed
112-
* @event page
113-
* @type {Event}
114-
*/
115-
this.$emit('page', { current, previous })
116-
}
117-
}
118-
},
119107
mounted() {
120108
this.calcOnInit()
121109
@@ -130,6 +118,8 @@ export default {
130118
// Events
131119
this.$refs.vsWrapper.addEventListener('scroll', this.onScrollFn)
132120
window.addEventListener('resize', this.onResizeFn, false)
121+
122+
this.$on('go-to-page', index => this.goToSlide(index))
133123
}
134124
},
135125
beforeDestroy() {
@@ -215,9 +205,15 @@ export default {
215205
return approximatelyEqual(slide.offsetLeft, this.currentPos, 5)
216206
})
217207
218-
if (getCurrentPage !== -1 && getCurrentPage !== -2) {
219-
this.currentPage = getCurrentPage || 0
208+
if (getCurrentPage < 0) {
209+
return
220210
}
211+
212+
const previous = this.currentPage
213+
const current = getCurrentPage || 0
214+
215+
this.currentPage = current
216+
this.$emit('page', { current, previous })
221217
},
222218
calcCurrentPosition() {
223219
this.currentPos = this.$refs.vsWrapper.scrollLeft || 0
@@ -260,10 +256,20 @@ export default {
260256
return
261257
}
262258
263-
this.scrollTo(nextSlideWidth)
259+
this.$refs.vsWrapper.scrollBy({
260+
left: nextSlideWidth,
261+
behavior: 'smooth'
262+
})
264263
},
265-
scrollTo(x = 0) {
266-
this.$refs.vsWrapper.scrollBy({ left: x, behavior: 'smooth' })
264+
goToSlide(index) {
265+
if (!this.slidesWidth[index]) {
266+
return
267+
}
268+
269+
this.$refs.vsWrapper.scrollTo({
270+
left: this.slidesWidth[index].offsetLeft,
271+
behavior: 'smooth'
272+
})
267273
}
268274
}
269275
}

0 commit comments

Comments
 (0)