Skip to content
This repository was archived by the owner on Feb 24, 2023. It is now read-only.

Commit aa69623

Browse files
author
Łukasz Florczak
committed
Basic slidesToShow support
1 parent b65e5ec commit aa69623

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Every first-level child of `<agile>` is a new slide.
6969
| pauseOnHover | boolean | `true` | Pause autoplay when a slide is hovered |
7070
| pauseOnDotsHover | boolean | `false` | Pause autoplay when a dot is hovered |
7171
| responsive | object | `null` | Object containing breakpoints and settings objects |
72+
| slidesToShow | integer | `1` | Number of slides to show |
7273
| speed | integer (ms) | `300` | Slide animation speed in milliseconds |
7374
| timing | string | `ease` | Transition timing function <br> (`linear`/`ease`/`ease-in`/`ease-out`/`ease-in-out`) |
7475
| unagile | boolean | `false` | Disable agile carousel |

src/Agile.vue

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@
114114
default: true
115115
},
116116
117+
slidesToScroll: {
118+
type: Number,
119+
default: 1
120+
},
121+
122+
slidesToShow: {
123+
type: Number,
124+
default: 1
125+
},
126+
117127
speed: {
118128
type: Number,
119129
default: 300
@@ -151,7 +161,6 @@
151161
slide: 0,
152162
track: 0
153163
},
154-
slidesToShow: 1,
155164
defaultSettings: {
156165
prevArrow: this.prevArrow,
157166
nextArrow: this.nextArrow,
@@ -164,6 +173,8 @@
164173
pauseOnDotsHover: this.pauseOnDotsHover,
165174
pauseOnHover: this.pauseOnHover,
166175
responsive: this.responsive,
176+
slidesToScroll: this.slidesToScroll,
177+
slidesToShow: this.slidesToShow,
167178
speed: this.speed,
168179
timing: this.timing,
169180
unagile: this.unagile
@@ -242,7 +253,7 @@
242253
this.width = {
243254
document: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
244255
container: this.$refs.list.clientWidth,
245-
slide: !this.settings.unagile ? this.$refs.list.clientWidth / this.slidesToShow : 'auto'
256+
slide: !this.settings.unagile ? this.$refs.list.clientWidth / this.settings.slidesToShow : 'auto'
246257
}
247258
248259
return this.width
@@ -304,17 +315,24 @@
304315
305316
enableInfiniteMode () {
306317
if (!this.settings.fade && !this.$refs.list.getElementsByClassName('agile__slide--cloned')[0]) {
307-
let firstSlide = this.$refs.track.firstChild.cloneNode(true)
308-
let lastSlide = this.$refs.track.lastChild.cloneNode(true)
318+
let slides = {}
319+
Object.assign(slides, this.slides)
320+
321+
for (let i = 0; i < this.settings.slidesToShow; i++) {
322+
let index = this.slidesCount + i - 1
323+
let cloned = slides[i].cloneNode(true)
324+
cloned.classList.add('agile__slide--cloned')
325+
this.$refs.track.insertBefore(cloned, this.slides[index].nextSibling)
326+
}
309327
310-
firstSlide.classList.add('agile__slide--cloned')
311-
lastSlide.classList.add('agile__slide--cloned')
328+
for (let i = this.slidesCount - 1; i > this.slidesCount - 1 - this.settings.slidesToShow; i--) {
329+
let cloned = slides[i].cloneNode(true)
330+
cloned.classList.add('agile__slide--cloned')
331+
this.$refs.track.insertBefore(cloned, this.slides[0])
332+
}
312333
313-
this.$refs.track.insertBefore(lastSlide, this.slides[0])
314-
this.$refs.track.insertBefore(firstSlide, this.slides[this.slidesCount].nextSibling)
334+
this.countSlides()
315335
}
316-
317-
this.countSlides()
318336
},
319337
320338
disableInfiniteMode () {
@@ -345,7 +363,7 @@
345363
346364
countSlides () {
347365
if (this.settings.infinite && !this.settings.fade && !this.settings.unagile) {
348-
this.allSlidesCount = this.slidesCount + 2
366+
this.allSlidesCount = this.slidesCount + (2 * this.settings.slidesToShow)
349367
} else {
350368
this.allSlidesCount = this.slidesCount
351369
}
@@ -444,15 +462,21 @@
444462
445463
this.transform = 0
446464
} else {
447-
this.transform = n * this.width.slide
465+
let transform = n * this.width.slide
466+
467+
if (!this.settings.infinite && this.slidesCount - n < this.settings.slidesToShow) {
468+
transform -= this.width.slide * (this.slidesCount - n)
469+
}
470+
471+
this.transform = transform
448472
}
449473
450474
for (let i = 0; i < this.allSlidesCount; ++i) {
451475
this.slides[i].classList.remove('agile__slide--active')
452476
}
453477
454478
if (this.settings.infinite && !this.settings.fade) {
455-
this.transform += this.width.slide
479+
this.transform += this.width.slide * this.settings.slidesToShow
456480
this.addActiveClass(n + 1)
457481
} else {
458482
this.addActiveClass(n)
@@ -536,7 +560,7 @@
536560
537561
// Actions on document resize
538562
for (let i = 0; i < this.allSlidesCount; ++i) {
539-
this.slides[i].style.width = this.width.container + 'px'
563+
this.slides[i].style.width = this.width.slide + 'px'
540564
541565
// Prepare slides for fade mode
542566
if (this.settings.fade && !this.settings.unagile) {
@@ -551,7 +575,7 @@
551575
this.width.track = this.width.container
552576
this.transform = 0
553577
} else {
554-
this.width.track = this.width.container * this.allSlidesCount
578+
this.width.track = this.width.slide * this.allSlidesCount
555579
this.goTo(this.currentSlide, false, false)
556580
}
557581
},
@@ -584,18 +608,18 @@
584608
return
585609
}
586610
587-
this.prevSlide()
611+
this.goToPrev()
612+
this.handleMouseUp()
588613
}
589614
590615
if (this.dragDistance < -1 * this.swipeDistance) {
591616
if (!this.settings.infinite && this.currentSlide === this.slidesCount - 1) {
592617
return
593618
}
594619
595-
this.nextSlide()
620+
this.goToNext()
621+
this.handleMouseUp()
596622
}
597-
598-
this.handleMouseUp()
599623
}
600624
}
601625
}
@@ -661,7 +685,6 @@
661685
align-items: center;
662686
display: flex;
663687
list-style: none;
664-
margin: 0;
665688
padding: 0;
666689
white-space: nowrap;
667690
}

0 commit comments

Comments
 (0)