slide.vue 5.5 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
      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
D
dolymood 已提交
46 47 48 49
      },
      allowVertical: {
        type: Boolean,
        default: false
M
init  
miaodian 已提交
50 51 52 53 54
      }
    },
    data() {
      return {
        dots: 0,
D
dolymood 已提交
55 56 57 58 59 60 61 62
        currentPageIndex: this.initialIndex || 0
      }
    },
    watch: {
      initialIndex(newIndex) {
        if (newIndex !== this.currentPageIndex) {
          this.slide && this.slide.goToPage(newIndex)
        }
M
init  
miaodian 已提交
63 64 65 66
      }
    },
    methods: {
      refresh() {
D
dolymood 已提交
67
        this.slide && this.slide.destroy()
D
dolymood 已提交
68
        clearTimeout(this._timer)
D
dolymood 已提交
69 70 71 72
        this.$nextTick(() => {
          if (this.slide === null) {
            return
          }
D
dolymood 已提交
73 74 75
          if (this.slide !== undefined) {
            this.currentPageIndex = 0
          }
D
dolymood 已提交
76 77 78 79 80 81 82 83 84 85 86
          this.dots = 0
          this._setSlideWidth()
          this._initDots()
          this._initSlide()

          if (this.autoPlay) {
            this._play()
          }
        })
      },
      _refresh() {
M
init  
miaodian 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        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,
110
          bounce: false,
D
dolymood 已提交
111
          eventPassthrough: this.allowVertical ? 'vertical' : '',
M
init  
miaodian 已提交
112 113 114 115 116
          snap: {
            loop: this.loop,
            threshold: this.threshold,
            speed: this.speed
          },
D
dolymood 已提交
117 118
          click: true,
          observeDOM: false
M
init  
miaodian 已提交
119 120
        })

D
dolymood 已提交
121 122
        this.slide.goToPage(this.currentPageIndex, 0, 0)

M
init  
miaodian 已提交
123 124
        this.slide.on('scrollEnd', this._onScrollEnd)

D
dolymood 已提交
125 126
        window.removeEventListener('touchend', this._touchEndEvent, false)
        this._touchEndEvent = () => {
M
init  
miaodian 已提交
127 128 129
          if (this.autoPlay) {
            this._play()
          }
D
dolymood 已提交
130 131
        }
        window.addEventListener('touchend', this._touchEndEvent, false)
M
init  
miaodian 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

        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 已提交
156
          this.slide.next()
M
init  
miaodian 已提交
157 158 159 160 161 162
        }, this.interval)
      },
      _deactivated() {
        clearTimeout(this._timer)
        clearTimeout(this._resizeTimer)
        window.removeEventListener('resize', this._resizeHandler)
D
dolymood 已提交
163
        window.removeEventListener('touchend', this._touchEndEvent, false)
M
init  
miaodian 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177
      },
      _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 已提交
178
          this._refresh()
M
init  
miaodian 已提交
179 180 181 182
        }, 60)
      }
    },
    mounted() {
D
dolymood 已提交
183
      this.refresh()
M
init  
miaodian 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197

      window.addEventListener('resize', this._resizeHandler)
    },
    activated() {
      if (this.autoPlay) {
        this._play()
      }
      window.addEventListener('resize', this._resizeHandler)
    },
    deactivated() {
      this._deactivated()
    },
    destroyed() {
      this._deactivated()
198 199 200 201
      if (this.slide) {
        this.slide.destroy()
        this.slide = null
      }
M
init  
miaodian 已提交
202 203 204 205 206
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
D
dolymood 已提交
207
  @require "../../common/stylus/variable.styl"
M
init  
miaodian 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  .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
D
dolymood 已提交
226
      vertical-align: bottom
M
init  
miaodian 已提交
227 228 229 230 231 232 233
      margin: 0 1px
      width: 10px
      height: 1px
      background: $slide-dot-bgc
      &.active
        background: $slide-dot-active-bgc
</style>