index.vue 26.9 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    },
    navigation (val) {
      this.isNavigationAuto = val === 'auto'
      this.hideNavigation = val !== true || this.isNavigationAuto
      this._navigationSwiperAddMouseEvent()
    },
    items () {
      this._setNavigationState()
    },
    swiperEnabled (val) {
      if (!val) {
        this.prevDisabled = true
        this.nextDisabled = true
        this.isNavigationAuto && (this.hideNavigation = true)
      }
fxy060608's avatar
fxy060608 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    }
  },
  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 已提交
200
    this._resetLayout()
fxy060608's avatar
fxy060608 已提交
201 202 203 204 205
    this.$watch(() => {
      return this.autoplay && !this.userTracking
    }, this._inintAutoplay)
    this._inintAutoplay(this.autoplay && !this.userTracking)
    this.$watch('items.length', this._resetLayout)
D
DCloud_LXH 已提交
206
    this._navigationSwiperAddMouseEvent()
fxy060608's avatar
fxy060608 已提交
207 208 209
  },
  beforeDestroy () {
    this._cancelSchedule()
210
    cancelAnimationFrame(this._animationFrame)
fxy060608's avatar
fxy060608 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
  },
  methods: {
    _inintAutoplay (enable) {
      if (enable) {
        this._scheduleAutoplay()
      } else {
        this._cancelSchedule()
      }
    },
    /**
     * 页面变更检查和同步
     */
    _currentCheck () {
224 225 226
      var current = -1
      if (this.currentItemId) {
        for (let i = 0, items = this.items; i < items.length; i++) {
fxy060608's avatar
fxy060608 已提交
227
          const componentInstance = items[i].componentInstance
228 229 230 231 232 233
          if (componentInstance && componentInstance.itemId === this.currentItemId) {
            current = i
            break
          }
        }
      }
fxy060608's avatar
fxy060608 已提交
234 235 236 237
      if (current < 0) {
        current = Math.round(this.current) || 0
      }
      current = current < 0 ? 0 : current
238 239 240 241
      if (this.currentSync !== current) {
        this.currentChangeSource = ''
        this.currentSync = current
      }
fxy060608's avatar
fxy060608 已提交
242 243 244 245 246 247 248 249 250 251 252 253
    },
    _itemReady (vnode, callback) {
      if (vnode.componentInstance && vnode.componentInstance._isMounted) {
        callback()
      } else {
        vnode._callbacks = vnode._callbacks || []
        vnode._callbacks.push(callback)
      }
    },
    /**
     * 当前页面变更
     */
Q
qiang 已提交
254
    _currentChanged (current, history) {
fxy060608's avatar
fxy060608 已提交
255 256 257
      var source = this.currentChangeSource
      this.currentChangeSource = ''
      if (!source) {
Q
qiang 已提交
258 259
        const length = this.items.length
        this._animateViewport(current, '', this.circularEnabled && history + (length - current) % length > length / 2 ? 1 : 0)
fxy060608's avatar
fxy060608 已提交
260 261 262 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
      }
      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)
360 361 362
            if (this.autoplay) {
              this._scheduleAutoplay()
            }
fxy060608's avatar
fxy060608 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
          }
        } 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) {
390 391 392
      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 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
      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)
        }
      }

408 409 410
      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 已提交
411 412
      var slideFrame = this.$refs.slideFrame
      if (slideFrame) {
413 414
        slideFrame.style['-webkit-transform'] = transform
        slideFrame.style.transform = transform
fxy060608's avatar
fxy060608 已提交
415 416
      }
      this._viewportPosition = index
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
      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 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    },
    _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
450
        this._transitionStart = null
fxy060608's avatar
fxy060608 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        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)
467
      this._animationFrame = requestAnimationFrame(this._animateFrameFuncProto.bind(this))
fxy060608's avatar
fxy060608 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    },
    _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 已提交
489 490 491
          if (position + length - current < current - position) {
            position += length
          }
fxy060608's avatar
fxy060608 已提交
492 493 494 495 496 497 498 499 500 501 502
        } else {
          for (; position + length < current;) {
            position += length
          }
          for (; position - length > current;) {
            position -= length
          }
          if (position + length - current < current - position) {
            position += length
          }
        }
D
DCloud_LXH 已提交
503 504
      } else if (source === 'click') {
        current = current + this.displayMultipleItemsNumber - 1 < length ? current : 0
fxy060608's avatar
fxy060608 已提交
505 506 507 508 509 510 511 512 513 514
      }

      this._animating = {
        toPos: current,
        acc: 2 * (position - current) / (duration * duration),
        endTime: Date.now() + duration,
        source: source
      }
      if (!this._requestedAnimation) {
        this._requestedAnimation = true
515
        this._animationFrame = requestAnimationFrame(this._animateFrameFuncProto.bind(this))
fxy060608's avatar
fxy060608 已提交
516 517 518 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
      }
    },
    _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)
      }
564
      var time = (this._contentTrackT - contentTrackT) || 1
fxy060608's avatar
fxy060608 已提交
565
      if (this.vertical) {
566
        move(-data.dy / this.$refs.slideFrame.offsetHeight, -data.ddy / time)
fxy060608's avatar
fxy060608 已提交
567
      } else {
568
        move(-data.dx / this.$refs.slideFrame.offsetWidth, -data.ddx / time)
fxy060608's avatar
fxy060608 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
      }
    },
    _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 已提交
588
      if (this.disableTouch || !this.items.length) {
589 590
        return
      }
fxy060608's avatar
fxy060608 已提交
591 592 593
      if (!this._invalid) {
        if (e.detail.state === 'start') {
          this.userTracking = true
594
          this._userDirectionChecked = false
fxy060608's avatar
fxy060608 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
          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) {
617 618 619
              if (this.autoplay) {
                this._scheduleAutoplay()
              }
fxy060608's avatar
fxy060608 已提交
620 621 622 623 624 625 626
              return
            }
          }
          this._handleTrackMove(e.detail)
          return false
        }
      }
D
DCloud_LXH 已提交
627 628 629 630 631 632 633 634 635 636 637
    },
    /**
     * navigation
     */
    _onSwiperDotClick (index) {
      this._animateViewport(
        (this.currentSync = index),
        (this.currentChangeSource = 'click'),
        this.circularEnabled ? 1 : 0
      )
    },
638 639 640
    _navigationClick ($event, type) {
      $event.stopPropagation()

D
DCloud_LXH 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
      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) {
      const { clientX, clientY } = e
      const {
        left,
        right,
        top,
        bottom,
        width,
        height
      } = this.$refs.slidesWrapper.getBoundingClientRect()

      if (this.vertical) {
        this.hideNavigation = !(
          clientY - top < height / 3 || bottom - clientY < height / 3
        )
      } else {
        this.hideNavigation = !(
          clientX - left < width / 3 || right - clientX < width / 3
        )
      }
    },
    _navigationMouseOut () {
      this.hideNavigation = true
    },
    _navigationSwiperAddMouseEvent () {
      if (__PLATFORM__ === 'h5') {
        const rootRef = this.$refs.slidesWrapper
        if (rootRef) {
          rootRef.removeEventListener('mousemove', this._navigationMouseMove)
          rootRef.removeEventListener('mouseout', this._navigationMouseOut)

          if (this.isNavigationAuto) {
            rootRef.addEventListener('mousemove', this._navigationMouseMove)
            rootRef.addEventListener('mouseout', this._navigationMouseOut)
          }
        }
      }
    },
    _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 已提交
714 715 716 717 718
    }
  },
  render (createElement) {
    var slidesDots = []
    var swiperItems = []
719
    if (this.$slots.default) {
720
      deepClone(this.$slots.default, createElement).forEach(vnode => {
721 722 723 724 725
        if (vnode.componentOptions && vnode.componentOptions.tag === 'v-uni-swiper-item') {
          swiperItems.push(vnode)
        }
      })
    }
X
xzs02 已提交
726
    for (let index = 0, length = swiperItems.length; index < length; index++) {
fxy060608's avatar
fxy060608 已提交
727
      const currentSync = this.currentSync
X
xzs02 已提交
728
      slidesDots.push(createElement('div', {
Q
qiang 已提交
729
        on: {
D
DCloud_LXH 已提交
730
          click: () => this._onSwiperDotClick(index)
Q
qiang 已提交
731
        },
X
xzs02 已提交
732 733 734 735 736
        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 已提交
737
          background: index === currentSync ? this.indicatorActiveColor : this.indicatorColor
X
xzs02 已提交
738 739 740
        }
      }))
    }
fxy060608's avatar
fxy060608 已提交
741 742 743 744
    this.items = swiperItems
    var slidesWrapperChild = [createElement('div', {
      ref: 'slides',
      style: this.slidesStyle,
fxy060608's avatar
fxy060608 已提交
745
      class: 'uni-swiper-slides'
fxy060608's avatar
fxy060608 已提交
746
    }, [
郭胜强 已提交
747 748 749 750 751 752
      createElement('div', {
        ref: 'slideFrame',
        class: 'uni-swiper-slide-frame',
        style: this.slideFrameStyle
      }, swiperItems)
    ])]
fxy060608's avatar
fxy060608 已提交
753 754 755
    if (this.indicatorDots) {
      slidesWrapperChild.push(createElement('div', {
        ref: 'slidesDots',
fxy060608's avatar
fxy060608 已提交
756
        class: ['uni-swiper-dots', this.vertical ? 'uni-swiper-dots-vertical' : 'uni-swiper-dots-horizontal']
fxy060608's avatar
fxy060608 已提交
757 758 759
      }, slidesDots))
    }

D
DCloud_LXH 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    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: {
788
                click: (e) => this._navigationClick(e, 'prev'),
D
DCloud_LXH 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
                ...navigationEvent
              },
              class: [
                'uni-swiper-navigation',
                'uni-swiper-navigation-prev',
                {
                  'uni-swiper-navigation-disabled': this.prevDisabled,
                  ...navigationClass
                }
              ]
            },
            [iconElement]
          ),
          createElement(
            'div',
            {
              on: {
806
                click: (e) => this._navigationClick(e, 'next'),
D
DCloud_LXH 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
                ...navigationEvent
              },
              class: [
                'uni-swiper-navigation',
                'uni-swiper-navigation-next',
                {
                  'uni-swiper-navigation-disabled': this.nextDisabled,
                  ...navigationClass
                }
              ]
            },
            [iconElement]
          )
        )
      }
    }

fxy060608's avatar
fxy060608 已提交
824
    return createElement(
Q
qiang 已提交
825
      'uni-swiper', {
fxy060608's avatar
fxy060608 已提交
826
        on: this.$listeners
Q
qiang 已提交
827 828
      }, [createElement('div', {
        ref: 'slidesWrapper',
fxy060608's avatar
fxy060608 已提交
829
        class: 'uni-swiper-wrapper'
fxy060608's avatar
fxy060608 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 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
      }, 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 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 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

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;
  cursor: auto;
  pointer-events: none;
}

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 已提交
991
</style>