Skip to content

Commit e6e1021

Browse files
authored
fix(carousel): Ensure slideshow rotates when the end is reached (#814)
* fix(carousel): Ensure slideshow rotates when the end is reached * Update carousel.vue
1 parent d2e566c commit e6e1021

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

lib/components/carousel.vue

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,25 @@
157157
return;
158158
}
159159
160-
// Don't do anything if noting to slide to
161-
if (this.slides.length === 0) {
160+
const len = this.slides.length;
161+
162+
// Don't do anything if nothing to slide to
163+
if (len === 0) {
162164
return;
163165
}
164166
165167
// Don't change slide while transitioning, wait until transition is done
166168
if (this.isSliding) {
169+
// Schedule slide after sliding complete
167170
this.$once('slid', () => this.setSlide(slide));
168171
return;
169172
}
170173
171-
// Wrap around if necessary
172-
this.index = Math.max(0, Math.min(Math.floor(slide), this.slides.length - 1));
174+
// Make sure we have an integer (you never know!)
175+
slide = Math.floor(slide);
176+
177+
// Set new slide index. Wrap around if necessary
178+
this.index = slide >= len ? 0 : (slide >= 0 ? slide : len - 1);
173179
},
174180
175181
// Previous slide
@@ -223,16 +229,14 @@
223229
224230
const id = this.id;
225231
const numSlides = this.slides.length;
232+
226233
// Keep slide number in range
227234
const index = Math.max(0, Math.min(Math.floor(this.index), numSlides - 1));
228-
235+
229236
this.slides.forEach((slide, idx) => {
230237
const n = idx + 1;
231-
if (idx === index) {
232-
slide.classList.add('active');
233-
} else {
234-
slide.classList.remove('active');
235-
}
238+
239+
slide.classList[idx === index ? 'add' : 'remove']('active');
236240
slide.setAttribute('aria-current', idx === index ? 'true' : 'false');
237241
slide.setAttribute('aria-posinset', String(n));
238242
slide.setAttribute('aria-setsize', String(numSlides));
@@ -271,7 +275,7 @@
271275
return;
272276
}
273277
if (!Boolean(newVal)) {
274-
// Pausing slide show
278+
// Pausing slide show
275279
this.pause();
276280
} else {
277281
// Restarting or Changing interval

0 commit comments

Comments
 (0)