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

Commit bde0e1e

Browse files
author
Łukasz Florczak
committed
Basic center mode support
1 parent f48cde2 commit bde0e1e

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ Every first-level child of `<agile>` is a new slide.
5757
| --- | --- | --- | --- |
5858
| arrows | boolean | `true` | Enable prev/next arrows |
5959
| asNavFor | array (refs) | `[]` | Set the slider to be the navigation of other one – more in „asNavFor” section |
60-
| prevArrow | string (HTML/SVG) | `<svg>` | Prev arrow code – more in „Arrows” section |
61-
| nextArrow | string (HTML/SVG) | `<svg>` | Next arrow code – more in „Arrows” section |
60+
| prevArrow | string (HTML / SVG) | `<svg>` | Prev arrow code – more in „Arrows” section |
61+
| nextArrow | string (HTML / SVG) | `<svg>` | Next arrow code – more in „Arrows” section |
6262
| autoplay | boolean | `false` | Enable autoplay |
6363
| autoplaySpeed | integer (ms) | `3000` | Autoplay interval in milliseconds |
64+
| centerMode | boolean | `false` | Enables active slides centering |
65+
| centerPadding | string (px / % ) | `15%` | Side padding for center mode |
6466
| dots | boolean | `true` | Enable dot indicators/pagination |
6567
| fade | boolean | `false` | Enable fade effect |
6668
| infinite | boolean | `true` | Infinite loop sliding |

src/Agile.vue

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="agile"
33
:class="{'agile--fade': settings.fade && !settings.unagile, 'agile--disabled': settings.unagile}">
4-
<div ref="list" class="agile__list">
4+
<div ref="list" class="agile__list" :style="{paddingLeft: listPadding, paddingRight: listPadding}">
55
<div ref="track" class="agile__track"
66
:style="{width: width.track + 'px', transform: 'translate(-' + transform + 'px)', transition: 'transform ' + settings.timing + ' ' + transitionDelay + 'ms'}"
77
@mouseover="handleMouseOver('track')" @mouseout="handleMouseOut('track')">
@@ -55,6 +55,16 @@
5555
default: 3000
5656
},
5757
58+
centerMode: {
59+
type: Boolean,
60+
default: false
61+
},
62+
63+
centerPadding: {
64+
type: String,
65+
default: '15%'
66+
},
67+
5868
dots: {
5969
type: Boolean,
6070
default: true
@@ -170,6 +180,8 @@
170180
arrows: this.arrows,
171181
autoplay: this.autoplay,
172182
autoplaySpeed: this.autoplaySpeed,
183+
centerMode: this.centerMode,
184+
centerPadding: this.centerPadding,
173185
dots: this.dots,
174186
fade: this.fade,
175187
infinite: this.infinite,
@@ -256,13 +268,14 @@
256268
257269
methods: {
258270
getWidth () {
271+
let computedStyle = getComputedStyle(this.$refs.list)
272+
259273
this.width = {
260274
document: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
261-
container: this.$refs.list.clientWidth,
262-
slide: !this.settings.unagile ? this.$refs.list.clientWidth / this.settings.slidesToShow : 'auto'
275+
container: this.settings.centerMode ? this.$refs.list.clientWidth - parseFloat(computedStyle.paddingLeft) - parseFloat(computedStyle.paddingRight) : this.$refs.list.clientWidth
263276
}
264277
265-
return this.width
278+
this.width.slide = !this.settings.unagile ? this.width.container / this.settings.slidesToShow : 'auto'
266279
},
267280
268281
compare (a, b) {
@@ -324,14 +337,14 @@
324337
let slides = {}
325338
Object.assign(slides, this.slides)
326339
327-
for (let i = 0; i < this.settings.slidesToShow; i++) {
340+
for (let i = 0; i < this.settings.slidesToShow + 1; i++) {
328341
let index = this.slidesCount + i - 1
329342
let cloned = slides[i].cloneNode(true)
330343
cloned.classList.add('agile__slide--cloned')
331344
this.$refs.track.insertBefore(cloned, this.slides[index].nextSibling)
332345
}
333346
334-
for (let i = this.slidesCount - 1; i > this.slidesCount - 1 - this.settings.slidesToShow; i--) {
347+
for (let i = this.slidesCount - 1; i > this.slidesCount - 2 - this.settings.slidesToShow; i--) {
335348
let cloned = slides[i].cloneNode(true)
336349
cloned.classList.add('agile__slide--cloned')
337350
this.$refs.track.insertBefore(cloned, this.slides[0])
@@ -369,7 +382,7 @@
369382
370383
countSlides () {
371384
if (this.settings.infinite && !this.settings.fade && !this.settings.unagile) {
372-
this.allSlidesCount = this.slidesCount + (2 * this.settings.slidesToShow)
385+
this.allSlidesCount = this.slidesCount + (2 * (this.settings.slidesToShow + 1))
373386
} else {
374387
this.allSlidesCount = this.slidesCount
375388
}
@@ -470,8 +483,16 @@
470483
} else {
471484
let transform = n * this.width.slide
472485
473-
if (!this.settings.infinite && this.slidesCount - n < this.settings.slidesToShow) {
474-
transform = this.width.slide * (this.slidesCount - this.settings.slidesToShow)
486+
if (!this.settings.infinite && this.slidesCount - n < this.settings.slidesToShow + 1) {
487+
transform = this.width.slide * (this.slidesCount - this.settings.slidesToShow + 1)
488+
}
489+
490+
if (this.settings.centerMode) {
491+
if (this.settings.slidesToShow % 2) {
492+
transform -= Math.floor(this.settings.slidesToShow / 2) * this.width.slide
493+
} else if (this.settings.slidesToShow >= 4) {
494+
// transform -= Math.floor(this.settings.slidesToShow / 3) * this.width.slide
495+
}
475496
}
476497
477498
this.transform = transform
@@ -482,7 +503,7 @@
482503
}
483504
484505
if (this.settings.infinite && !this.settings.fade) {
485-
this.transform += this.width.slide * this.settings.slidesToShow
506+
this.transform += this.width.slide * (this.settings.slidesToShow + 1)
486507
this.addActiveClass(n + 1)
487508
} else {
488509
this.addActiveClass(n)
@@ -548,6 +569,12 @@
548569
Object.assign(this.settings, responsiveSettings)
549570
}
550571
572+
if (this.settings.centerMode && this.settings.slidesToShow > this.slidesCount - 1) {
573+
this.settings.slidesToShow = this.slidesCount - 1
574+
} else if (this.settings.slidesToShow > this.slidesCount) {
575+
this.settings.slidesToShow = this.slidesCount
576+
}
577+
551578
// Check infinity mode status and enable/disable
552579
if (this.settings.infinite && !this.settings.fade && !this.settings.unagile) {
553580
this.enableInfiniteMode()
@@ -597,6 +624,14 @@
597624
},
598625
599626
computed: {
627+
listPadding: function () {
628+
if (this.settings.centerMode) {
629+
return this.settings.centerPadding
630+
} else {
631+
return 0
632+
}
633+
},
634+
600635
documentWidth: function () {
601636
return this.width.document
602637
}

0 commit comments

Comments
 (0)