index.vue 27.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
<script>
import touchtrack from 'uni-mixins/touchtrack'
3
import { deepClone } from 'uni-shared'
4

fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
export default {
  name: 'Swiper',
  mixins: [touchtrack],
  props: {
    indicatorDots: {
      type: [Boolean, String],
      default: false
    },
    vertical: {
      type: [Boolean, String],
      default: false
    },
    autoplay: {
      type: [Boolean, String],
      default: false
    },
    circular: {
      type: [Boolean, String],
      default: false
    },
    interval: {
      type: [Number, String],
      default: 5e3
    },
    duration: {
      type: [Number, String],
      default: 500
    },
    current: {
      type: [Number, String],
      default: 0
    },
    indicatorColor: {
      type: String,
      default: ''
    },
    indicatorActiveColor: {
      type: String,
      default: ''
    },
    previousMargin: {
      type: String,
      default: ''
    },
    nextMargin: {
      type: String,
      default: ''
    },
    currentItemId: {
      type: String,
      default: ''
    },
    skipHiddenItemLayout: {
      type: [Boolean, String],
      default: false
    },
    displayMultipleItems: {
      type: [Number, String],
      default: 1
64 65 66 67
    },
    disableTouch: {
      type: [Boolean, String],
      default: false
D
DCloud_LXH 已提交
68 69 70 71 72 73 74 75 76 77 78 79
    },
    navigation: {
      type: [Boolean, String],
      default: false
    },
    navigationColor: {
      type: String,
      default: '#fff'
    },
    navigationActiveColor: {
      type: String,
      default: 'rgba(53, 53, 53, 0.6)'
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85 86 87
    }
  },
  data () {
    return {
      currentSync: Math.round(this.current) || 0,
      currentItemIdSync: this.currentItemId || '',
      userTracking: false,
      currentChangeSource: '',
D
DCloud_LXH 已提交
88 89 90 91 92 93
      items: [],

      isNavigationAuto: false,
      hideNavigation: false,
      prevDisabled: false,
      nextDisabled: false
fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    }
  },
  computed: {
    intervalNumber () {
      const interval = Number(this.interval)
      return isNaN(interval) ? 5e3 : interval
    },
    durationNumber () {
      const duration = Number(this.duration)
      return isNaN(duration) ? 500 : duration
    },
    displayMultipleItemsNumber () {
      const displayMultipleItems = Math.round(this.displayMultipleItems)
      return isNaN(displayMultipleItems) ? 1 : displayMultipleItems
    },
    slidesStyle () {
      var style = {}
111
      if (this.nextMargin || this.previousMargin) {
fxy060608's avatar
fxy060608 已提交
112 113 114 115 116 117
        style = this.vertical ? {
          left: 0,
          right: 0,
          top: this._upx2px(this.previousMargin),
          bottom: this._upx2px(this.nextMargin)
        } : {
郭胜强 已提交
118 119 120 121 122
          top: 0,
          bottom: 0,
          left: this._upx2px(this.previousMargin),
          right: this._upx2px(this.nextMargin)
        }
fxy060608's avatar
fxy060608 已提交
123 124 125
      }
      return style
    },
126 127 128 129 130 131 132
    slideFrameStyle () {
      var value = Math.abs(100 / this.displayMultipleItemsNumber) + '%'
      return {
        width: this.vertical ? '100%' : value,
        height: !this.vertical ? '100%' : value
      }
    },
D
DCloud_LXH 已提交
133 134 135
    swiperEnabled () {
      return this.items.length > this.displayMultipleItemsNumber
    },
fxy060608's avatar
fxy060608 已提交
136
    circularEnabled () {
D
DCloud_LXH 已提交
137
      return this.circular && this.swiperEnabled
fxy060608's avatar
fxy060608 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }
  },
  watch: {
    vertical () {
      this._resetLayout()
    },
    circular () {
      this._resetLayout()
    },
    intervalNumber (val) {
      if (this._timer) {
        this._cancelSchedule()
        this._scheduleAutoplay()
      }
    },
    current (val) {
      this._currentCheck()
    },
Q
qiang 已提交
156 157
    currentSync (val, oldVal) {
      this._currentChanged(val, oldVal)
fxy060608's avatar
fxy060608 已提交
158
      this.$emit('update:current', val)
D
DCloud_LXH 已提交
159
      this._setNavigationState()
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165 166 167 168
    },
    currentItemId (val) {
      this._currentCheck()
    },
    currentItemIdSync (val) {
      this.$emit('update:currentItemId', val)
    },
    displayMultipleItemsNumber () {
      this._resetLayout()
D
DCloud_LXH 已提交
169
    },
D
DCloud_LXH 已提交
170 171 172 173 174 175 176
    navigation: {
      immediate: true,
      handler (val) {
        this.isNavigationAuto = val === 'auto'
        this.hideNavigation = val !== true || this.isNavigationAuto
        this._navigationSwiperAddMouseEvent()
      }
D
DCloud_LXH 已提交
177 178 179 180 181 182 183 184 185 186
    },
    items () {
      this._setNavigationState()
    },
    swiperEnabled (val) {
      if (!val) {
        this.prevDisabled = true
        this.nextDisabled = true
        this.isNavigationAuto && (this.hideNavigation = true)
      }
fxy060608's avatar
fxy060608 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    }
  },
  created () {
    this._invalid = true
    this._viewportPosition = 0
    this._viewportMoveRatio = 1
    this._animating = null
    this._requestedAnimation = false
    this._userDirectionChecked = false
    this._contentTrackViewport = 0
    this._contentTrackSpeed = 0
    this._contentTrackT = 0
  },
  mounted () {
    this._currentCheck()
    this.touchtrack(this.$refs.slidesWrapper, '_handleContentTrack', true)
D
DCloud_LXH 已提交
203
    this._resetLayout()
fxy060608's avatar
fxy060608 已提交
204 205 206 207 208
    this.$watch(() => {
      return this.autoplay && !this.userTracking
    }, this._inintAutoplay)
    this._inintAutoplay(this.autoplay && !this.userTracking)
    this.$watch('items.length', this._resetLayout)
D
DCloud_LXH 已提交
209
    this._navigationSwiperAddMouseEvent()
fxy060608's avatar
fxy060608 已提交
210 211 212
  },
  beforeDestroy () {
    this._cancelSchedule()
213
    cancelAnimationFrame(this._animationFrame)
fxy060608's avatar
fxy060608 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
  },
  methods: {
    _inintAutoplay (enable) {
      if (enable) {
        this._scheduleAutoplay()
      } else {
        this._cancelSchedule()
      }
    },
    /**
     * 页面变更检查和同步
     */
    _currentCheck () {
227 228 229
      var current = -1
      if (this.currentItemId) {
        for (let i = 0, items = this.items; i < items.length; i++) {
fxy060608's avatar
fxy060608 已提交
230
          const componentInstance = items[i].componentInstance
231 232 233 234 235 236
          if (componentInstance && componentInstance.itemId === this.currentItemId) {
            current = i
            break
          }
        }
      }
fxy060608's avatar
fxy060608 已提交
237 238 239 240
      if (current < 0) {
        current = Math.round(this.current) || 0
      }
      current = current < 0 ? 0 : current
241 242 243 244
      if (this.currentSync !== current) {
        this.currentChangeSource = ''
        this.currentSync = current
      }
fxy060608's avatar
fxy060608 已提交
245 246 247 248 249 250 251 252 253 254 255 256
    },
    _itemReady (vnode, callback) {
      if (vnode.componentInstance && vnode.componentInstance._isMounted) {
        callback()
      } else {
        vnode._callbacks = vnode._callbacks || []
        vnode._callbacks.push(callback)
      }
    },
    /**
     * 当前页面变更
     */
Q
qiang 已提交
257
    _currentChanged (current, history) {
fxy060608's avatar
fxy060608 已提交
258 259 260
      var source = this.currentChangeSource
      this.currentChangeSource = ''
      if (!source) {
Q
qiang 已提交
261 262
        const length = this.items.length
        this._animateViewport(current, '', this.circularEnabled && history + (length - current) % length > length / 2 ? 1 : 0)
fxy060608's avatar
fxy060608 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
      }
      var item = this.items[current]
      if (item) {
        this._itemReady(item, () => {
          var currentItemId = this.currentItemIdSync = item.componentInstance.itemId || ''
          this.$trigger('change', {}, {
            current: this.currentSync,
            currentItemId,
            source
          })
        })
      }
    },
    /**
     * 自动播放
     */
    _scheduleAutoplay () {
      var self = this
      this._cancelSchedule()
      function timer () {
        self._timer = null
        self.currentChangeSource = 'autoplay'
        if (self.circularEnabled) {
          self.currentSync = self._normalizeCurrentValue(self.currentSync + 1)
        } else {
          self.currentSync = self.currentSync + self.displayMultipleItemsNumber < self.items.length ? self.currentSync + 1 : 0
        }
        self._animateViewport(self.currentSync, 'autoplay', self.circularEnabled ? 1 : 0)
        self._timer = setTimeout(timer, self.intervalNumber)
      }
      if (!(!this._isMounted || this._invalid || this.items.length <= this.displayMultipleItemsNumber)) {
        this._timer = setTimeout(timer, this.intervalNumber)
      }
    },
    /**
     * 清除定时器
     */
    _cancelSchedule () {
      if (this._timer) {
        clearTimeout(this._timer)
        this._timer = null
      }
    },
    /**
     * 检查当前页值
     */
    _normalizeCurrentValue (current) {
      var length = this.items.length
      if (!length) {
        return -1
      }
      var index = (Math.round(current) % length + length) % length
      if (this.circularEnabled) {
        if (length <= this.displayMultipleItemsNumber) {
          return 0
        }
      } else if (index > length - this.displayMultipleItemsNumber) {
        return length - this.displayMultipleItemsNumber
      }
      return index
    },
    _upx2px (val) {
      if (/\d+[ur]px$/i.test(val)) {
        val.replace(/\d+[ur]px$/i, text => {
          return `${uni.upx2px(parseFloat(text))}px`
        })
      }
      return val || ''
    },
    /**
     * 重新布局
     */
    _resetLayout () {
      if (this._isMounted) {
        this._cancelSchedule()
        this._endViewportAnimation()
        var items = this.items

        for (var i = 0; i < items.length; i++) {
          this._updateItemPos(i, i)
        }
        this._viewportMoveRatio = 1
        if (this.displayMultipleItemsNumber === 1 && items.length) {
          var itemRect = items[0].componentInstance.$el.getBoundingClientRect()
          var slideFrameRect = this.$refs.slideFrame.getBoundingClientRect()
          this._viewportMoveRatio = itemRect.width / slideFrameRect.width
          if (!(this._viewportMoveRatio > 0 && this._viewportMoveRatio < 1)) {
            this._viewportMoveRatio = 1
          }
        }
        var position = this._viewportPosition
        this._viewportPosition = -2
        var current = this.currentSync
        if (current >= 0) {
          this._invalid = false
          if (this.userTracking) {
            this._updateViewport(position + current - this._contentTrackViewport)
            this._contentTrackViewport = current
          } else {
            this._updateViewport(current)
363 364 365
            if (this.autoplay) {
              this._scheduleAutoplay()
            }
fxy060608's avatar
fxy060608 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
          }
        } else {
          this._invalid = true
          this._updateViewport(-this.displayMultipleItemsNumber - 1)
        }
      }
    },
    _checkCircularLayout (e) {
      if (!this._invalid) {
        for (var items = this.items, n = items.length, i = e + this.displayMultipleItemsNumber, r = 0; r < n; r++) {
          var item = items[r]
          var _position = item._position
          var s = Math.floor(e / n) * n + r
          var l = s + n
          var c = s - n
          var u = Math.max(e - (s + 1), s - i, 0)
          var d = Math.max(e - (l + 1), l - i, 0)
          var h = Math.max(e - (c + 1), c - i, 0)
          var p = Math.min(u, d, h)
          var f = [s, l, c][[u, d, h].indexOf(p)]
          if (_position !== f) {
            this._updateItemPos(r, f)
          }
        }
      }
    },
    _updateItemPos (current, position) {
393 394 395
      var x = this.vertical ? '0' : 100 * position + '%'
      var y = this.vertical ? 100 * position + '%' : '0'
      var transform = 'translate(' + x + ', ' + y + ') translateZ(0)'
fxy060608's avatar
fxy060608 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
      var item = this.items[current]
      this._itemReady(item, () => {
        var el = item.componentInstance.$el
        el.style['-webkit-transform'] = transform
        el.style.transform = transform
        el._position = position
      })
    },
    _updateViewport (index) {
      if (!(Math.floor(2 * this._viewportPosition) === Math.floor(2 * index) && Math.ceil(2 * this._viewportPosition) === Math.ceil(2 * index))) {
        if (this.circularEnabled) {
          this._checkCircularLayout(index)
        }
      }

411 412 413
      var x = this.vertical ? '0' : 100 * -index * this._viewportMoveRatio + '%'
      var y = this.vertical ? 100 * -index * this._viewportMoveRatio + '%' : '0'
      var transform = 'translate(' + x + ', ' + y + ') translateZ(0)'
fxy060608's avatar
fxy060608 已提交
414 415
      var slideFrame = this.$refs.slideFrame
      if (slideFrame) {
416 417
        slideFrame.style['-webkit-transform'] = transform
        slideFrame.style.transform = transform
fxy060608's avatar
fxy060608 已提交
418 419
      }
      this._viewportPosition = index
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
      if (!this._transitionStart) {
        if (index % 1 === 0) {
          return
        }
        this._transitionStart = index
      }
      index -= Math.floor(this._transitionStart)
      if (index <= -(this.items.length - 1)) {
        index += this.items.length
      } else if (index >= this.items.length) {
        index -= this.items.length
      }
      index = this._transitionStart % 1 > 0.5 || this._transitionStart < 0 ? index - 1 : index
      this.$trigger('transition', {}, {
        dx: this.vertical ? 0 : index * slideFrame.offsetWidth,
        dy: this.vertical ? index * slideFrame.offsetHeight : 0
      })
fxy060608's avatar
fxy060608 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    },
    _animateFrameFuncProto () {
      if (!this._animating) {
        this._requestedAnimation = false
        return
      }
      var _animating = this._animating
      var toPos = _animating.toPos
      var acc = _animating.acc
      var endTime = _animating.endTime
      var source = _animating.source
      var time = endTime - Date.now()
      if (time <= 0) {
        this._updateViewport(toPos)
        this._animating = null
        this._requestedAnimation = false
453
        this._transitionStart = null
fxy060608's avatar
fxy060608 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
        var item = this.items[this.currentSync]
        if (item) {
          this._itemReady(item, () => {
            var currentItemId = item.componentInstance.itemId || ''
            this.$trigger('animationfinish', {}, {
              current: this.currentSync,
              currentItemId,
              source
            })
          })
        }
        return
      }
      var s = acc * time * time / 2
      var l = toPos + s
      this._updateViewport(l)
470
      this._animationFrame = requestAnimationFrame(this._animateFrameFuncProto.bind(this))
fxy060608's avatar
fxy060608 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    },
    _animateViewport (current, source, n) {
      this._cancelViewportAnimation()
      var duration = this.durationNumber
      var length = this.items.length
      var position = this._viewportPosition
      if (this.circularEnabled) {
        if (n < 0) {
          for (; position < current;) {
            position += length
          }
          for (; position - length > current;) {
            position -= length
          }
        } else if (n > 0) {
          for (; position > current;) {
            position -= length
          }
          for (; position + length < current;) {
            position += length
          }
D
DCloud_LXH 已提交
492 493 494
          if (position + length - current < current - position) {
            position += length
          }
fxy060608's avatar
fxy060608 已提交
495 496 497 498 499 500 501 502 503 504 505
        } else {
          for (; position + length < current;) {
            position += length
          }
          for (; position - length > current;) {
            position -= length
          }
          if (position + length - current < current - position) {
            position += length
          }
        }
D
DCloud_LXH 已提交
506 507
      } else if (source === 'click') {
        current = current + this.displayMultipleItemsNumber - 1 < length ? current : 0
fxy060608's avatar
fxy060608 已提交
508 509 510 511 512 513 514 515 516 517
      }

      this._animating = {
        toPos: current,
        acc: 2 * (position - current) / (duration * duration),
        endTime: Date.now() + duration,
        source: source
      }
      if (!this._requestedAnimation) {
        this._requestedAnimation = true
518
        this._animationFrame = requestAnimationFrame(this._animateFrameFuncProto.bind(this))
fxy060608's avatar
fxy060608 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
      }
    },
    _cancelViewportAnimation () {
      this._animating = null
    },
    /**
     * 结束动画
     */
    _endViewportAnimation () {
      if (this._animating) {
        this._updateViewport(this._animating.toPos)
        this._animating = null
      }
    },
    _handleTrackStart () {
      this._cancelSchedule()
      this._contentTrackViewport = this._viewportPosition
      this._contentTrackSpeed = 0
      this._contentTrackT = Date.now()
      this._cancelViewportAnimation()
    },
    _handleTrackMove (data) {
      var self = this
      var contentTrackT = this._contentTrackT
      this._contentTrackT = Date.now()
      var length = this.items.length
      var other = length - this.displayMultipleItemsNumber
      function calc (val) {
        return 0.5 - 0.25 / (val + 0.5)
      }

      function move (oldVal, newVal) {
        var val = self._contentTrackViewport + oldVal
        self._contentTrackSpeed = 0.6 * self._contentTrackSpeed + 0.4 * newVal
        if (!self.circularEnabled) {
          if (val < 0 || val > other) {
            if (val < 0) {
              val = -calc(-val)
            } else {
              if (val > other) {
                val = other + calc(val - other)
              }
            }
            self._contentTrackSpeed = 0
          }
        }
        self._updateViewport(val)
      }
567
      var time = (this._contentTrackT - contentTrackT) || 1
fxy060608's avatar
fxy060608 已提交
568
      if (this.vertical) {
569
        move(-data.dy / this.$refs.slideFrame.offsetHeight, -data.ddy / time)
fxy060608's avatar
fxy060608 已提交
570
      } else {
571
        move(-data.dx / this.$refs.slideFrame.offsetWidth, -data.ddx / time)
fxy060608's avatar
fxy060608 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
      }
    },
    _handleTrackEnd (isCancel) {
      this.userTracking = false
      var t = this._contentTrackSpeed / Math.abs(this._contentTrackSpeed)
      var n = 0
      if (!isCancel && Math.abs(this._contentTrackSpeed) > 0.2) {
        n = 0.5 * t
      }
      var current = this._normalizeCurrentValue(this._viewportPosition + n)
      if (isCancel) {
        this._updateViewport(this._contentTrackViewport)
      } else {
        this.currentChangeSource = 'touch'
        this.currentSync = current
        this._animateViewport(current, 'touch', n !== 0 ? n : current === 0 && (this.circularEnabled && this._viewportPosition >= 1) ? 1 : 0)
      }
    },
    _handleContentTrack (e) {
D
DCloud_LXH 已提交
591
      if (this.disableTouch || !this.items.length) {
592 593
        return
      }
fxy060608's avatar
fxy060608 已提交
594 595 596
      if (!this._invalid) {
        if (e.detail.state === 'start') {
          this.userTracking = true
597
          this._userDirectionChecked = false
fxy060608's avatar
fxy060608 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
          return this._handleTrackStart()
        }
        // fixed by xxxxxx
        if (e.detail.state === 'end') {
          return this._handleTrackEnd(false)
        }
        if (e.detail.state === 'cancel') {
          return this._handleTrackEnd(true)
        }
        if (this.userTracking) {
          if (!this._userDirectionChecked) {
            this._userDirectionChecked = true
            var t = Math.abs(e.detail.dx)
            var n = Math.abs(e.detail.dy)
            if (t >= n && this.vertical) {
              this.userTracking = false
            } else {
              if (t <= n && !this.vertical) {
                this.userTracking = false
              }
            }
            if (!this.userTracking) {
620 621 622
              if (this.autoplay) {
                this._scheduleAutoplay()
              }
fxy060608's avatar
fxy060608 已提交
623 624 625 626 627 628 629
              return
            }
          }
          this._handleTrackMove(e.detail)
          return false
        }
      }
D
DCloud_LXH 已提交
630 631 632 633 634 635 636 637 638 639 640
    },
    /**
     * navigation
     */
    _onSwiperDotClick (index) {
      this._animateViewport(
        (this.currentSync = index),
        (this.currentChangeSource = 'click'),
        this.circularEnabled ? 1 : 0
      )
    },
D
DCloud_LXH 已提交
641
    _navigationClick ($event, type, disabled) {
642 643
      $event.stopPropagation()

D
DCloud_LXH 已提交
644 645
      if (disabled) return

D
DCloud_LXH 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
      const swiperItemLength = this.items.length
      let _current = this.currentSync

      switch (type) {
        case 'prev':
          _current--
          if (_current < 0 && this.circularEnabled) {
            _current = swiperItemLength - 1
          }
          break
        case 'next':
          _current++
          if (_current >= swiperItemLength && this.circularEnabled) {
            _current = 0
          }
          break
      }

      this._onSwiperDotClick(_current)
    },
    _navigationMouseMove (e) {
D
DCloud_LXH 已提交
667 668
      clearTimeout(this.hideNavigationTimer)

D
DCloud_LXH 已提交
669 670 671 672 673 674 675 676 677 678
      const { clientX, clientY } = e
      const {
        left,
        right,
        top,
        bottom,
        width,
        height
      } = this.$refs.slidesWrapper.getBoundingClientRect()

D
DCloud_LXH 已提交
679
      let hide = false
D
DCloud_LXH 已提交
680
      if (this.vertical) {
D
DCloud_LXH 已提交
681
        hide = !(clientY - top < height / 3 || bottom - clientY < height / 3)
D
DCloud_LXH 已提交
682
      } else {
D
DCloud_LXH 已提交
683
        hide = !(clientX - left < width / 3 || right - clientX < width / 3)
D
DCloud_LXH 已提交
684
      }
D
DCloud_LXH 已提交
685 686

      if (hide) {
687
        this.hideNavigationTimer = setTimeout(() => {
D
DCloud_LXH 已提交
688 689
          this.hideNavigation = hide
        }, 300)
690
        return
D
DCloud_LXH 已提交
691 692 693
      }

      this.hideNavigation = hide
D
DCloud_LXH 已提交
694 695 696 697 698 699 700 701 702
    },
    _navigationMouseOut () {
      this.hideNavigation = true
    },
    _navigationSwiperAddMouseEvent () {
      if (__PLATFORM__ === 'h5') {
        const rootRef = this.$refs.slidesWrapper
        if (rootRef) {
          rootRef.removeEventListener('mousemove', this._navigationMouseMove)
D
DCloud_LXH 已提交
703
          rootRef.removeEventListener('mouseleave', this._navigationMouseOut)
D
DCloud_LXH 已提交
704 705 706

          if (this.isNavigationAuto) {
            rootRef.addEventListener('mousemove', this._navigationMouseMove)
D
DCloud_LXH 已提交
707
            rootRef.addEventListener('mouseleave', this._navigationMouseOut)
D
DCloud_LXH 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
          }
        }
      }
    },
    _navigationHover (event, type) {
      const target = event.currentTarget
      if (!target) return
      target.style.backgroundColor =
        type === 'over' ? this.navigationActiveColor : ''
    },
    _setNavigationState () {
      const swiperItemLength = this.items.length
      const notCircular = !this.circularEnabled

      this.prevDisabled = this.currentSync === 0 && notCircular
      this.nextDisabled =
        (this.currentSync === swiperItemLength - 1 && notCircular) ||
        (notCircular &&
          this.currentSync + this.displayMultipleItemsNumber >= swiperItemLength)
fxy060608's avatar
fxy060608 已提交
727 728 729 730 731
    }
  },
  render (createElement) {
    var slidesDots = []
    var swiperItems = []
732
    if (this.$slots.default) {
733
      deepClone(this.$slots.default, createElement).forEach(vnode => {
734 735 736 737 738
        if (vnode.componentOptions && vnode.componentOptions.tag === 'v-uni-swiper-item') {
          swiperItems.push(vnode)
        }
      })
    }
X
xzs02 已提交
739
    for (let index = 0, length = swiperItems.length; index < length; index++) {
fxy060608's avatar
fxy060608 已提交
740
      const currentSync = this.currentSync
X
xzs02 已提交
741
      slidesDots.push(createElement('div', {
Q
qiang 已提交
742
        on: {
D
DCloud_LXH 已提交
743
          click: () => this._onSwiperDotClick(index)
Q
qiang 已提交
744
        },
X
xzs02 已提交
745 746 747 748 749
        class: {
          'uni-swiper-dot': true,
          'uni-swiper-dot-active': (index < currentSync + this.displayMultipleItemsNumber && index >= currentSync) || (index < currentSync + this.displayMultipleItemsNumber - length)
        },
        style: {
fxy060608's avatar
fxy060608 已提交
750
          background: index === currentSync ? this.indicatorActiveColor : this.indicatorColor
X
xzs02 已提交
751 752 753
        }
      }))
    }
fxy060608's avatar
fxy060608 已提交
754 755 756 757
    this.items = swiperItems
    var slidesWrapperChild = [createElement('div', {
      ref: 'slides',
      style: this.slidesStyle,
fxy060608's avatar
fxy060608 已提交
758
      class: 'uni-swiper-slides'
fxy060608's avatar
fxy060608 已提交
759
    }, [
郭胜强 已提交
760 761 762 763 764 765
      createElement('div', {
        ref: 'slideFrame',
        class: 'uni-swiper-slide-frame',
        style: this.slideFrameStyle
      }, swiperItems)
    ])]
fxy060608's avatar
fxy060608 已提交
766 767 768
    if (this.indicatorDots) {
      slidesWrapperChild.push(createElement('div', {
        ref: 'slidesDots',
fxy060608's avatar
fxy060608 已提交
769
        class: ['uni-swiper-dots', this.vertical ? 'uni-swiper-dots-vertical' : 'uni-swiper-dots-horizontal']
fxy060608's avatar
fxy060608 已提交
770 771 772
      }, slidesDots))
    }

D
DCloud_LXH 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
    if (__PLATFORM__ === 'h5') {
      if (this.navigation) {
        const navigationClass = {
          'uni-swiper-navigation-hide': this.hideNavigation,
          'uni-swiper-navigation-vertical': this.vertical
        }

        const iconElement = createElement(
          'i',
          {
            domProps: {
              innerHTML: '&#xe601;'
            },
            class: 'uni-btn-icon',
            style: { color: this.navigationColor, fontSize: '26px' }
          }
        )

        const navigationEvent = {
          mouseover: event => this._navigationHover(event, 'over'),
          mouseout: event => this._navigationHover(event, 'out')
        }

        slidesWrapperChild.push(
          createElement(
            'div',
            {
              on: {
D
DCloud_LXH 已提交
801
                click: (e) => this._navigationClick(e, 'prev', this.prevDisabled),
D
DCloud_LXH 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
                ...navigationEvent
              },
              class: [
                'uni-swiper-navigation',
                'uni-swiper-navigation-prev',
                {
                  'uni-swiper-navigation-disabled': this.prevDisabled,
                  ...navigationClass
                }
              ]
            },
            [iconElement]
          ),
          createElement(
            'div',
            {
              on: {
D
DCloud_LXH 已提交
819
                click: (e) => this._navigationClick(e, 'next', this.nextDisabled),
D
DCloud_LXH 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
                ...navigationEvent
              },
              class: [
                'uni-swiper-navigation',
                'uni-swiper-navigation-next',
                {
                  'uni-swiper-navigation-disabled': this.nextDisabled,
                  ...navigationClass
                }
              ]
            },
            [iconElement]
          )
        )
      }
    }

fxy060608's avatar
fxy060608 已提交
837
    return createElement(
Q
qiang 已提交
838
      'uni-swiper', {
fxy060608's avatar
fxy060608 已提交
839
        on: this.$listeners
Q
qiang 已提交
840 841
      }, [createElement('div', {
        ref: 'slidesWrapper',
fxy060608's avatar
fxy060608 已提交
842
        class: 'uni-swiper-wrapper'
fxy060608's avatar
fxy060608 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
      }, slidesWrapperChild)]
    )
  }
}
</script>
<style>
uni-swiper {
  display: block;
  height: 150px;
}

uni-swiper[hidden] {
  display: none;
}

uni-swiper .uni-swiper-wrapper {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

uni-swiper .uni-swiper-slides {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}

uni-swiper .uni-swiper-slide-frame {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  will-change: transform;
}

uni-swiper .uni-swiper-dots {
  position: absolute;
  font-size: 0;
}

uni-swiper .uni-swiper-dots-horizontal {
  left: 50%;
  bottom: 10px;
  text-align: center;
  white-space: nowrap;
  -webkit-transform: translate(-50%, 0);
  transform: translate(-50%, 0);
}

uni-swiper .uni-swiper-dots-horizontal .uni-swiper-dot {
  margin-right: 8px;
}

uni-swiper .uni-swiper-dots-horizontal .uni-swiper-dot:last-child {
  margin-right: 0;
}

uni-swiper .uni-swiper-dots-vertical {
  right: 10px;
  top: 50%;
  text-align: right;
  -webkit-transform: translate(0, -50%);
  transform: translate(0, -50%);
}

uni-swiper .uni-swiper-dots-vertical .uni-swiper-dot {
  display: block;
  margin-bottom: 9px;
}

uni-swiper .uni-swiper-dots-vertical .uni-swiper-dot:last-child {
  margin-bottom: 0;
}

uni-swiper .uni-swiper-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  cursor: pointer;
  transition-property: background-color;
  transition-timing-function: ease;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 50%;
}

uni-swiper .uni-swiper-dot-active {
  background-color: #000000;
}
D
DCloud_LXH 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

uni-swiper .uni-swiper-navigation {
  width: 26px;
  height: 26px;
  cursor: pointer;
  position: absolute;
  top: 50%;
  margin-top: -13px;
  display: flex;
  align-items: center;
  transition: all 0.2s;
  border-radius: 50%;
  opacity: 1;
}

uni-swiper .uni-swiper-navigation-disabled {
  opacity: 0.35;
D
DCloud_LXH 已提交
954
  cursor: not-allowed;
D
DCloud_LXH 已提交
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
}

uni-swiper .uni-swiper-navigation-hide {
  opacity: 0;
  cursor: auto;
  pointer-events: none;
}

uni-swiper .uni-swiper-navigation-prev {
  left: 10px;
}

uni-swiper .uni-swiper-navigation-prev i {
  margin-left: -1px;
  left: 10px;
}

uni-swiper .uni-swiper-navigation-prev.uni-swiper-navigation-vertical {
  top: 18px;
  left: 50%;
  margin-left: -13px;
}

uni-swiper .uni-swiper-navigation-prev.uni-swiper-navigation-vertical i {
  transform: rotate(90deg);
  margin-left: auto;
  margin-top: -2px;
}

uni-swiper .uni-swiper-navigation-next {
  right: 10px;
}

uni-swiper .uni-swiper-navigation-next i {
  transform: rotate(180deg);
}

uni-swiper .uni-swiper-navigation-next.uni-swiper-navigation-vertical {
  top: auto;
  bottom: 5px;
  left: 50%;
  margin-left: -13px;
}

uni-swiper .uni-swiper-navigation-next.uni-swiper-navigation-vertical i {
  margin-top: 2px;
  transform: rotate(270deg);
}
fxy060608's avatar
fxy060608 已提交
1003
</style>