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

<script type="text/ecmascript-6">
D
dolymood 已提交
22
  import CubeSlideItem from './slide-item.vue'
M
init  
miaodian 已提交
23
  import BScroll from 'better-scroll'
A
Amy 已提交
24
  import scrollMixin from '../../common/mixins/scroll'
T
tank0317 已提交
25
  import deprecatedMixin from '../../common/mixins/deprecated'
M
init  
miaodian 已提交
26 27 28

  const COMPONENT_NAME = 'cube-slide'
  const EVENT_CHANGE = 'change'
A
AmyFoxFN 已提交
29
  const EVENT_SELECT = 'click'
A
Amy 已提交
30
  const EVENT_SCROLL_END = 'scroll-end'
J
JiZhi 已提交
31
  const EVENT_SCROLL = 'scroll'
A
Amy 已提交
32

33 34
  const DIRECTION_H = 'horizontal'
  const DIRECTION_V = 'vertical'
M
init  
miaodian 已提交
35

A
Amy 已提交
36 37 38
  const DEFAULT_OPTIONS = {
    momentum: false,
    click: true,
A
AmyFoxFN 已提交
39 40
    observeDOM: false,
    bounce: false
A
Amy 已提交
41 42
  }

M
init  
miaodian 已提交
43 44
  export default {
    name: COMPONENT_NAME,
T
tank0317 已提交
45
    mixins: [scrollMixin, deprecatedMixin],
M
init  
miaodian 已提交
46
    props: {
47 48 49
      data: {
        type: Array,
        default() {
50
          /* istanbul ignore next */
51 52 53
          return []
        }
      },
D
dolymood 已提交
54 55 56 57
      initialIndex: {
        type: Number,
        default: 0
      },
M
init  
miaodian 已提交
58 59 60 61 62 63 64 65 66 67 68
      loop: {
        type: Boolean,
        default: true
      },
      threshold: {
        type: Number,
        default: 0.3
      },
      speed: {
        type: Number,
        default: 400
D
dolymood 已提交
69
      },
A
Amy 已提交
70
      autoPlay: {
D
dolymood 已提交
71
        type: Boolean,
A
Amy 已提交
72
        default: true
73
      },
A
Amy 已提交
74 75 76 77 78
      interval: {
        type: Number,
        default: 4000
      },
      showDots: {
79
        type: Boolean,
A
Amy 已提交
80
        default: true
81 82 83 84 85
      },
      direction: {
        type: String,
        default: DIRECTION_H
      },
A
Amy 已提交
86 87
      // The props allowVertical, stopPropagation could be removed in next minor version.
      allowVertical: {
88
        type: Boolean,
T
tank0317 已提交
89 90 91 92
        default: undefined,
        deprecated: {
          replacedBy: 'options'
        }
A
Amy 已提交
93 94 95
      },
      stopPropagation: {
        type: Boolean,
T
tank0317 已提交
96 97 98 99
        default: undefined,
        deprecated: {
          replacedBy: 'options'
        }
M
init  
miaodian 已提交
100 101 102 103 104
      }
    },
    data() {
      return {
        dots: 0,
D
dolymood 已提交
105 106 107
        currentPageIndex: this.initialIndex || 0
      }
    },
108
    created() {
A
Amy 已提交
109
      const needRefreshProps = ['data', 'loop', 'autoPlay', 'options.eventPassthrough', 'threshold', 'speed', 'allowVertical']
110 111
      needRefreshProps.forEach((key) => {
        this.$watch(key, () => {
A
Amy 已提交
112 113 114 115 116
          // To fix the render bug when add items since loop.
          if (key === 'data') {
            this._destroy()
          }

117
          /* istanbul ignore next */
118 119 120
          this.$nextTick(() => {
            this.refresh()
          })
121 122 123
        })
      })
    },
D
dolymood 已提交
124 125 126 127 128
    watch: {
      initialIndex(newIndex) {
        if (newIndex !== this.currentPageIndex) {
          this.slide && this.slide.goToPage(newIndex)
        }
M
init  
miaodian 已提交
129 130 131
      }
    },
    methods: {
A
AmyFoxFN 已提交
132
      clickItem(item, index) {
133
        /* istanbul ignore next */
134 135
        this.$emit(EVENT_SELECT, item, index)
      },
M
init  
miaodian 已提交
136
      refresh() {
137 138 139 140
        /* istanbul ignore if */
        if (this.slide === null) {
          return
        }
A
Amy 已提交
141
        this._destroy()
D
dolymood 已提交
142
        clearTimeout(this._timer)
143 144 145 146 147 148

        if (this.slide) {
          this.currentPageIndex = 0
        }
        this._updateSlideDom()
        if (this.showDots) {
D
dolymood 已提交
149
          this._initDots()
150
        }
D
dolymood 已提交
151 152 153
        if (this.currentPageIndex >= this.dots.length) {
          this.currentPageIndex = this.dots.length - 1
        }
154
        this._initSlide()
D
dolymood 已提交
155

156 157 158
        if (this.autoPlay) {
          this._play()
        }
D
dolymood 已提交
159
      },
A
Amy 已提交
160 161 162
      _destroy() {
        this.slide && this.slide.destroy()
      },
D
dolymood 已提交
163
      _refresh() {
164
        this._updateSlideDom(true)
M
init  
miaodian 已提交
165 166
        this.slide.refresh()
      },
167
      _updateSlideDom(isResize) {
D
dolymood 已提交
168
        this._setSlideStyle(isResize)
M
init  
miaodian 已提交
169
      },
D
dolymood 已提交
170
      _setSlideStyle(isResize) {
171 172
        this.children = this.$refs.slideGroup.children

D
dolymood 已提交
173 174 175 176 177 178 179 180
        const target = this.direction === DIRECTION_H ? 'width' : 'height'
        let allSize = 0
        const slideSize = this.$refs.slide[`client${target[0].toUpperCase() + target.slice(1)}`]
        const len = this.children.length
        for (let i = 0; i < len; i++) {
          const child = this.children[i]
          child.style[target] = slideSize + 'px'
          allSize += slideSize
181
        }
D
dolymood 已提交
182 183
        if (this.loop && !isResize && len > 1) {
          allSize += 2 * slideSize
184
        }
D
dolymood 已提交
185
        this.$refs.slideGroup.style[target] = allSize + 'px'
186
      },
M
init  
miaodian 已提交
187
      _initSlide() {
188
        const eventPassthrough = this.direction === DIRECTION_H && this.allowVertical ? DIRECTION_V : ''
A
Amy 已提交
189 190

        const options = Object.assign({}, DEFAULT_OPTIONS, {
191 192 193
          scrollX: this.direction === DIRECTION_H,
          scrollY: this.direction === DIRECTION_V,
          eventPassthrough,
M
init  
miaodian 已提交
194 195 196 197 198
          snap: {
            loop: this.loop,
            threshold: this.threshold,
            speed: this.speed
          },
A
Amy 已提交
199 200 201 202
          stopPropagation: this.stopPropagation
        }, this.options)

        this.slide = new BScroll(this.$refs.slide, options)
M
init  
miaodian 已提交
203

D
dolymood 已提交
204 205
        this.slide.goToPage(this.currentPageIndex, 0, 0)

M
init  
miaodian 已提交
206
        this.slide.on('scrollEnd', this._onScrollEnd)
J
JiZhi 已提交
207 208 209 210 211 212
        /* dispatch scroll position */
        if (this.options.listenScroll) {
          //  ensure dispatch scroll position constantly
          this.options.probeType = 3
          this.slide.on('scroll', this._onScroll)
        }
213 214
        const slideEl = this.$refs.slide
        slideEl.removeEventListener('touchend', this._touchEndEvent, false)
D
dolymood 已提交
215
        this._touchEndEvent = () => {
M
init  
miaodian 已提交
216 217 218
          if (this.autoPlay) {
            this._play()
          }
D
dolymood 已提交
219
        }
220
        slideEl.addEventListener('touchend', this._touchEndEvent, false)
M
init  
miaodian 已提交
221 222 223 224 225 226 227 228

        this.slide.on('beforeScrollStart', () => {
          if (this.autoPlay) {
            clearTimeout(this._timer)
          }
        })
      },
      _onScrollEnd() {
A
Amy 已提交
229 230
        const { pageX, pageY } = this.slide.getCurrentPage()
        let pageIndex = this.direction === DIRECTION_H ? pageX : pageY
M
init  
miaodian 已提交
231 232
        if (this.currentPageIndex !== pageIndex) {
          this.currentPageIndex = pageIndex
A
Amy 已提交
233
          this.$emit(EVENT_CHANGE, pageIndex)
M
init  
miaodian 已提交
234 235
        }

A
Amy 已提交
236 237
        this.$emit(EVENT_SCROLL_END, pageIndex)

M
init  
miaodian 已提交
238 239 240 241
        if (this.autoPlay) {
          this._play()
        }
      },
J
JiZhi 已提交
242 243 244
      _onScroll(pos) {
        this.$emit(EVENT_SCROLL, pos)
      },
M
init  
miaodian 已提交
245 246 247 248 249 250
      _initDots() {
        this.dots = new Array(this.children.length)
      },
      _play() {
        clearTimeout(this._timer)
        this._timer = setTimeout(() => {
D
dolymood 已提交
251
          this.slide.next()
M
init  
miaodian 已提交
252 253 254 255 256 257
        }, this.interval)
      },
      _deactivated() {
        clearTimeout(this._timer)
        clearTimeout(this._resizeTimer)
        window.removeEventListener('resize', this._resizeHandler)
258 259 260 261
        const slideEl = this.$refs.slide
        if (slideEl) {
          slideEl.removeEventListener('touchend', this._touchEndEvent, false)
        }
M
init  
miaodian 已提交
262 263
      },
      _resizeHandler() {
264
        /* istanbul ignore if */
M
init  
miaodian 已提交
265 266 267 268 269
        if (!this.slide) {
          return
        }
        clearTimeout(this._resizeTimer)
        this._resizeTimer = setTimeout(() => {
270
          /* istanbul ignore if */
M
init  
miaodian 已提交
271 272 273 274 275 276 277
          if (this.slide.isInTransition) {
            this._onScrollEnd()
          } else {
            if (this.autoPlay) {
              this._play()
            }
          }
D
dolymood 已提交
278
          this._refresh()
M
init  
miaodian 已提交
279 280 281 282
        }, 60)
      }
    },
    mounted() {
283 284 285
      this.$nextTick(() => {
        this.refresh()
      })
M
init  
miaodian 已提交
286 287 288 289

      window.addEventListener('resize', this._resizeHandler)
    },
    activated() {
290
      /* istanbul ignore next */
M
init  
miaodian 已提交
291 292 293 294 295 296
      if (this.autoPlay) {
        this._play()
      }
      window.addEventListener('resize', this._resizeHandler)
    },
    deactivated() {
297
      /* istanbul ignore next */
M
init  
miaodian 已提交
298 299 300 301
      this._deactivated()
    },
    destroyed() {
      this._deactivated()
A
Amy 已提交
302 303 304
      this._destroy()

      this.slide = null
D
dolymood 已提交
305 306 307
    },
    components: {
      CubeSlideItem
M
init  
miaodian 已提交
308 309 310 311 312
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
D
dolymood 已提交
313
  @require "../../common/stylus/variable.styl"
M
init  
miaodian 已提交
314
  .cube-slide
A
AmyFoxFN 已提交
315
    position: relative
M
init  
miaodian 已提交
316
    min-height: 1px
A
AmyFoxFN 已提交
317
    height: 100%
M
init  
miaodian 已提交
318 319 320

  .cube-slide-group
    position: relative
A
AmyFoxFN 已提交
321
    height: 100%
M
init  
miaodian 已提交
322 323 324 325 326 327 328 329 330
    overflow: hidden
    white-space: nowrap

  .cube-slide-dots
    position: absolute
    bottom: 2px
    right: 0
    left: 0
    padding: 0 6px
A
AmyFoxFN 已提交
331
    font-size: 0
M
init  
miaodian 已提交
332 333 334 335
    text-align: center
    transform: translateZ(1px)
    > span
      display: inline-block
D
dolymood 已提交
336
      vertical-align: bottom
M
init  
miaodian 已提交
337 338 339 340 341 342 343
      margin: 0 1px
      width: 10px
      height: 1px
      background: $slide-dot-bgc
      &.active
        background: $slide-dot-active-bgc
</style>