slide.vue 5.1 KB
Newer Older
M
init  
miaodian 已提交
1 2 3 4 5 6
<template>
  <div class="cube-slide" ref="slide">
    <div class="cube-slide-group" ref="slideGroup">
      <slot></slot>
    </div>
    <div class="cube-slide-dots">
D
dolymood 已提交
7 8 9
      <slot name="dots" :current="currentPageIndex" :dots="dots">
        <span :class="{active: currentPageIndex === index}" v-for="(item, index) in dots"></span>
      </slot>
M
init  
miaodian 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll'

  const COMPONENT_NAME = 'cube-slide'
  const EVENT_CHANGE = 'change'

  export default {
    name: COMPONENT_NAME,
    props: {
D
dolymood 已提交
23 24 25 26
      initialIndex: {
        type: Number,
        default: 0
      },
M
init  
miaodian 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      loop: {
        type: Boolean,
        default: true
      },
      autoPlay: {
        type: Boolean,
        default: true
      },
      interval: {
        type: Number,
        default: 4000
      },
      threshold: {
        type: Number,
        default: 0.3
      },
      speed: {
        type: Number,
        default: 400
      }
    },
    data() {
      return {
        dots: 0,
D
dolymood 已提交
51 52 53 54 55 56 57 58
        currentPageIndex: this.initialIndex || 0
      }
    },
    watch: {
      initialIndex(newIndex) {
        if (newIndex !== this.currentPageIndex) {
          this.slide && this.slide.goToPage(newIndex)
        }
M
init  
miaodian 已提交
59 60 61 62
      }
    },
    methods: {
      refresh() {
D
dolymood 已提交
63
        this.slide && this.slide.destroy()
D
dolymood 已提交
64
        clearTimeout(this._timer)
D
dolymood 已提交
65 66 67 68
        this.$nextTick(() => {
          if (this.slide === null) {
            return
          }
D
dolymood 已提交
69 70 71
          if (this.slide !== undefined) {
            this.currentPageIndex = 0
          }
D
dolymood 已提交
72 73 74 75 76 77 78 79 80 81 82
          this.dots = 0
          this._setSlideWidth()
          this._initDots()
          this._initSlide()

          if (this.autoPlay) {
            this._play()
          }
        })
      },
      _refresh() {
M
init  
miaodian 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        this._setSlideWidth(true)
        this.slide.refresh()
      },
      _setSlideWidth(isResize) {
        this.children = this.$refs.slideGroup.children

        let width = 0
        let slideWidth = this.$refs.slide.clientWidth
        for (let i = 0; i < this.children.length; i++) {
          let child = this.children[i]
          child.style.width = slideWidth + 'px'
          width += slideWidth
        }
        if (this.loop && !isResize) {
          width += 2 * slideWidth
        }
        this.$refs.slideGroup.style.width = width + 'px'
      },
      _initSlide() {
        this.slide = new BScroll(this.$refs.slide, {
          scrollX: true,
          scrollY: false,
          momentum: false,
          snap: {
            loop: this.loop,
            threshold: this.threshold,
            speed: this.speed
          },
D
dolymood 已提交
111 112
          click: true,
          observeDOM: false
M
init  
miaodian 已提交
113 114
        })

D
dolymood 已提交
115 116
        this.slide.goToPage(this.currentPageIndex, 0, 0)

M
init  
miaodian 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        this.slide.on('scrollEnd', this._onScrollEnd)

        this.slide.on('touchend', () => {
          if (this.autoPlay) {
            this._play()
          }
        })

        this.slide.on('beforeScrollStart', () => {
          if (this.autoPlay) {
            clearTimeout(this._timer)
          }
        })
      },
      _onScrollEnd() {
        let pageIndex = this.slide.getCurrentPage().pageX
        if (this.currentPageIndex !== pageIndex) {
          this.currentPageIndex = pageIndex
          this.$emit(EVENT_CHANGE, this.currentPageIndex)
        }

        if (this.autoPlay) {
          this._play()
        }
      },
      _initDots() {
        this.dots = new Array(this.children.length)
      },
      _play() {
        clearTimeout(this._timer)
        this._timer = setTimeout(() => {
D
dolymood 已提交
148
          this.slide.next()
M
init  
miaodian 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        }, this.interval)
      },
      _deactivated() {
        clearTimeout(this._timer)
        clearTimeout(this._resizeTimer)
        window.removeEventListener('resize', this._resizeHandler)
      },
      _resizeHandler() {
        if (!this.slide) {
          return
        }
        clearTimeout(this._resizeTimer)
        this._resizeTimer = setTimeout(() => {
          if (this.slide.isInTransition) {
            this._onScrollEnd()
          } else {
            if (this.autoPlay) {
              this._play()
            }
          }
D
dolymood 已提交
169
          this._refresh()
M
init  
miaodian 已提交
170 171 172 173
        }, 60)
      }
    },
    mounted() {
D
dolymood 已提交
174
      this.refresh()
M
init  
miaodian 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188

      window.addEventListener('resize', this._resizeHandler)
    },
    activated() {
      if (this.autoPlay) {
        this._play()
      }
      window.addEventListener('resize', this._resizeHandler)
    },
    deactivated() {
      this._deactivated()
    },
    destroyed() {
      this._deactivated()
189 190 191 192
      if (this.slide) {
        this.slide.destroy()
        this.slide = null
      }
M
init  
miaodian 已提交
193 194 195 196 197
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
D
dolymood 已提交
198
  @require "../../common/stylus/variable.styl"
M
init  
miaodian 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  .cube-slide
    min-height: 1px

  .cube-slide-group
    position: relative
    overflow: hidden
    white-space: nowrap

  .cube-slide-dots
    position: absolute
    bottom: 2px
    right: 0
    left: 0
    padding: 0 6px
    text-align: center
    transform: translateZ(1px)
    > span
      display: inline-block
      margin: 0 1px
      width: 10px
      height: 1px
      background: $slide-dot-bgc
      &.active
        background: $slide-dot-active-bgc
</style>