|
| 1 | +<template> |
| 2 | + <div class="carousel slide" > |
| 3 | + <ol v-if="!noIndicators" :class="indicatorClasses"> |
| 4 | + <li |
| 5 | + v-for="(index, key) in itemsLength" |
| 6 | + @click="setSlide(key)" |
| 7 | + :class="{'active' : active === key}" |
| 8 | + :key="key" |
| 9 | + ></li> |
| 10 | + </ol> |
| 11 | + <div class="carousel-inner"> |
| 12 | + <slot></slot> |
| 13 | + </div> |
| 14 | + <template v-if="!noArrows"> |
| 15 | + <a class="carousel-control-prev" @click="previousSlide"> |
| 16 | + <span class="carousel-control-prev-icon"></span> |
| 17 | + <span class="sr-only">Previous</span> |
| 18 | + </a> |
| 19 | + <a class="carousel-control-next" @click="nextSlide"> |
| 20 | + <span class="carousel-control-next-icon"></span> |
| 21 | + <span class="sr-only">Next</span> |
| 22 | + </a> |
| 23 | + </template> |
| 24 | + </div> |
| 25 | +</template> |
| 26 | + |
| 27 | +<script> |
| 28 | +export default { |
| 29 | + name: 'CCarousel', |
| 30 | + props: { |
| 31 | + interval: { |
| 32 | + type: Number, |
| 33 | + default: 4000 |
| 34 | + }, |
| 35 | + noAnimation: Boolean, |
| 36 | + noIndicators: Boolean, |
| 37 | + noArrows: Boolean, |
| 38 | + indicatorClasses: { |
| 39 | + type: [String, Array], |
| 40 | + default: 'carousel-indicators' |
| 41 | + }, |
| 42 | + }, |
| 43 | + data () { |
| 44 | + return { |
| 45 | + active: 0, |
| 46 | + itemsLength: null, |
| 47 | + currentInterval: null, |
| 48 | + transitioning: null |
| 49 | + } |
| 50 | + }, |
| 51 | + mounted () { |
| 52 | + this.itemsLength = this.$children.length |
| 53 | + this.$children.forEach((item, index) => item.index = index) |
| 54 | + const activeItem = this.$children.filter(item => item.active) |
| 55 | + if (activeItem[0]) { |
| 56 | + this.active = activeItem[0].index || 0 |
| 57 | + } else { |
| 58 | + this.$children[0].classes = 'active' |
| 59 | + } |
| 60 | + this.menageInterval() |
| 61 | + }, |
| 62 | + methods: { |
| 63 | + menageInterval () { |
| 64 | + if (this.noAnimation) return |
| 65 | +
|
| 66 | + clearInterval(this.currentInterval) |
| 67 | + this.currentInterval = setInterval(() => { |
| 68 | + this.nextSlide() |
| 69 | + }, this.interval) |
| 70 | + }, |
| 71 | + nextSlide () { |
| 72 | + let index = this.active === this.itemsLength - 1 ? 0 : this.active + 1 |
| 73 | + this.setSlide(index, 'left') |
| 74 | + }, |
| 75 | + previousSlide () { |
| 76 | + let index = this.active === 0 ? this.itemsLength -1 : this.active - 1 |
| 77 | + this.setSlide(index, 'right') |
| 78 | + }, |
| 79 | + setSlide (index, direction = null) { |
| 80 | + if (index === this.active) { |
| 81 | + return this.menageInterval() |
| 82 | + } |
| 83 | + direction === null ? direction = this.active < index ? 'left' : 'right' : '' |
| 84 | + if (this.noAnimation) { |
| 85 | + this.$children[this.active].classes = '' |
| 86 | + this.$children[index].classes = 'active' |
| 87 | + this.active = index |
| 88 | + } else if (!this.transitioning) { |
| 89 | + this.slide(index, direction) |
| 90 | + } |
| 91 | + }, |
| 92 | + slide (index, direction) { |
| 93 | + this.menageInterval() |
| 94 | + let oldIndex = this.active |
| 95 | + this.active = index |
| 96 | + const order = direction === 'right' ? 'prev' : 'next' |
| 97 | + const orderClass = `carousel-item-${order}` |
| 98 | + const directionClass = `carousel-item-${direction}` |
| 99 | + const activeEl = this.$children[oldIndex].$el |
| 100 | + const nextEl = this.$children[index].$el |
| 101 | + setTimeout(() =>{ |
| 102 | + nextEl.classList.add(orderClass) |
| 103 | + nextEl.offsetHeight |
| 104 | + nextEl.classList.add(directionClass) |
| 105 | + activeEl.classList.add(directionClass) |
| 106 | + }, 0) |
| 107 | +
|
| 108 | + this.transitioning = setTimeout(() => { |
| 109 | + this.transitioning = null |
| 110 | + nextEl.classList.remove(orderClass) |
| 111 | + nextEl.classList.remove(directionClass) |
| 112 | + activeEl.classList.remove(directionClass) |
| 113 | + this.$children[oldIndex].classes = '' |
| 114 | + this.$children[index].classes = 'active' |
| 115 | + }, 600) |
| 116 | + } |
| 117 | + } |
| 118 | +
|
| 119 | +} |
| 120 | +</script> |
0 commit comments