|
1 | 1 | <template>
|
2 | 2 | <div class="agile"
|
3 | 3 | :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}"> |
5 | 5 | <div ref="track" class="agile__track"
|
6 | 6 | :style="{width: width.track + 'px', transform: 'translate(-' + transform + 'px)', transition: 'transform ' + settings.timing + ' ' + transitionDelay + 'ms'}"
|
7 | 7 | @mouseover="handleMouseOver('track')" @mouseout="handleMouseOut('track')">
|
|
55 | 55 | default: 3000
|
56 | 56 | },
|
57 | 57 |
|
| 58 | + centerMode: { |
| 59 | + type: Boolean, |
| 60 | + default: false |
| 61 | + }, |
| 62 | +
|
| 63 | + centerPadding: { |
| 64 | + type: String, |
| 65 | + default: '15%' |
| 66 | + }, |
| 67 | +
|
58 | 68 | dots: {
|
59 | 69 | type: Boolean,
|
60 | 70 | default: true
|
|
170 | 180 | arrows: this.arrows,
|
171 | 181 | autoplay: this.autoplay,
|
172 | 182 | autoplaySpeed: this.autoplaySpeed,
|
| 183 | + centerMode: this.centerMode, |
| 184 | + centerPadding: this.centerPadding, |
173 | 185 | dots: this.dots,
|
174 | 186 | fade: this.fade,
|
175 | 187 | infinite: this.infinite,
|
|
256 | 268 |
|
257 | 269 | methods: {
|
258 | 270 | getWidth () {
|
| 271 | + let computedStyle = getComputedStyle(this.$refs.list) |
| 272 | +
|
259 | 273 | this.width = {
|
260 | 274 | 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 |
263 | 276 | }
|
264 | 277 |
|
265 |
| - return this.width |
| 278 | + this.width.slide = !this.settings.unagile ? this.width.container / this.settings.slidesToShow : 'auto' |
266 | 279 | },
|
267 | 280 |
|
268 | 281 | compare (a, b) {
|
|
324 | 337 | let slides = {}
|
325 | 338 | Object.assign(slides, this.slides)
|
326 | 339 |
|
327 |
| - for (let i = 0; i < this.settings.slidesToShow; i++) { |
| 340 | + for (let i = 0; i < this.settings.slidesToShow + 1; i++) { |
328 | 341 | let index = this.slidesCount + i - 1
|
329 | 342 | let cloned = slides[i].cloneNode(true)
|
330 | 343 | cloned.classList.add('agile__slide--cloned')
|
331 | 344 | this.$refs.track.insertBefore(cloned, this.slides[index].nextSibling)
|
332 | 345 | }
|
333 | 346 |
|
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--) { |
335 | 348 | let cloned = slides[i].cloneNode(true)
|
336 | 349 | cloned.classList.add('agile__slide--cloned')
|
337 | 350 | this.$refs.track.insertBefore(cloned, this.slides[0])
|
|
369 | 382 |
|
370 | 383 | countSlides () {
|
371 | 384 | 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)) |
373 | 386 | } else {
|
374 | 387 | this.allSlidesCount = this.slidesCount
|
375 | 388 | }
|
|
470 | 483 | } else {
|
471 | 484 | let transform = n * this.width.slide
|
472 | 485 |
|
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 | + } |
475 | 496 | }
|
476 | 497 |
|
477 | 498 | this.transform = transform
|
|
482 | 503 | }
|
483 | 504 |
|
484 | 505 | 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) |
486 | 507 | this.addActiveClass(n + 1)
|
487 | 508 | } else {
|
488 | 509 | this.addActiveClass(n)
|
|
548 | 569 | Object.assign(this.settings, responsiveSettings)
|
549 | 570 | }
|
550 | 571 |
|
| 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 | +
|
551 | 578 | // Check infinity mode status and enable/disable
|
552 | 579 | if (this.settings.infinite && !this.settings.fade && !this.settings.unagile) {
|
553 | 580 | this.enableInfiniteMode()
|
|
597 | 624 | },
|
598 | 625 |
|
599 | 626 | computed: {
|
| 627 | + listPadding: function () { |
| 628 | + if (this.settings.centerMode) { |
| 629 | + return this.settings.centerPadding |
| 630 | + } else { |
| 631 | + return 0 |
| 632 | + } |
| 633 | + }, |
| 634 | +
|
600 | 635 | documentWidth: function () {
|
601 | 636 | return this.width.document
|
602 | 637 | }
|
|
0 commit comments