Skip to content

Commit 0ef99cb

Browse files
committed
fix bug in carousel which made it not work in case of smaller carousel slides
1 parent d89798b commit 0ef99cb

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

components/misc/CardCarousel.vue

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,48 @@ export default {
3131
moveLeft() {
3232
const carouselContainer = document.querySelector('.carousel-container')
3333
const carousel = carouselContainer.querySelector('#carousel')
34+
const carouselSlideWidth = carousel.querySelectorAll(
35+
'.carousel__slide'
36+
)[0].clientWidth
3437
const carouselLength = carousel.querySelectorAll('.carousel__slide')
3538
.length
39+
const widthRatio = Math.floor(carousel.clientWidth / carouselSlideWidth)
40+
3641
--this.counter
37-
if (this.counter < 0) this.counter = carouselLength - 1
42+
43+
if (widthRatio !== 1) {
44+
if (this.counter < 0) this.counter = carouselLength - widthRatio
45+
} else {
46+
if (this.counter < 0) this.counter = carouselLength - 1
47+
}
3848
3949
carousel.scrollTo({
4050
top: 0,
41-
left: this.counter * carousel.clientWidth,
51+
left: this.counter * carouselSlideWidth,
4252
behavior: 'smooth',
4353
})
4454
},
4555
moveRight() {
4656
const carouselContainer = document.querySelector('.carousel-container')
4757
const carousel = carouselContainer.querySelector('#carousel')
58+
const carouselSlideWidth = carousel.querySelectorAll(
59+
'.carousel__slide'
60+
)[0].clientWidth
4861
const carouselLength = carousel.querySelectorAll('.carousel__slide')
4962
.length
63+
const widthRatio = Math.floor(carousel.clientWidth / carouselSlideWidth)
64+
5065
++this.counter
51-
if (this.counter > carouselLength - 1) this.counter = 0
66+
67+
if (widthRatio !== 1) {
68+
if (this.counter > carouselLength - widthRatio) this.counter = 0
69+
} else {
70+
if (this.counter > carouselLength - 1) this.counter = 0
71+
}
5272
5373
carousel.scrollTo({
5474
top: 0,
55-
left: this.counter * carousel.clientWidth,
75+
left: this.counter * carouselSlideWidth,
5676
behavior: 'smooth',
5777
})
5878
},

0 commit comments

Comments
 (0)