all files / src/slides/ slides.vue

59.68% Statements 37/62
56.67% Branches 17/30
70.83% Functions 17/24
60.71% Lines 34/56
1 2 3 4 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 111 112 113 114 115 116 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                                                                                                  18×     30×                                                                             22× 22× 22× 22×               25× 25× 25× 25×     34× 34×     12× 12× 36× 36×           36× 36× 36×                                                                                      
<template>
  <div class="w-slides" @mouseenter="onMouseEnter"
    @mouseleave="onMouseLeave"
    @touchstart="onTouchStart"
    @touchend="onTouchEnd"
    >
    <div class="w-slides-window" ref="window">
      <div class="w-slides-wrapper">
        <slot></slot>
      </div>
    </div>
    <div class="w-slides-dots">
      <span @click="onClickPrev" data-action="prev">
        <w-icon name="left"></w-icon>
      </span>
      <span v-for="n in childrenLength" :class="{active: selectedIndex === n-1}"
        @click="select(n-1)" :key="n" :data-index="n-1">
        {{n}}
      </span>
      <span @click="onClickNext" data-action="next">
        <w-icon name="right"></w-icon>
      </span>
    </div>
  </div>
</template>
 
<script>
  import WIcon from '../icon/icon'
 
  export default {
    components: {
      WIcon
    },
    props: {
      selected: {
        type: String,
      },
      autoPlay: {
        type: Boolean,
        default: true
      },
      autoPlayDelay: {
        type: Number,
        default: 3000
      }
    },
    data () {
      return {
        childrenLength: 0,
        lastSelectedIndex: undefined,
        timerId: undefined,
        startTouch: undefined
      }
    },
    mounted () {
      this.updateChildren()
      if(this.autoPlay) {
        this.playAutomatically()
      }
      this.childrenLength = this.items.length
    },
    updated () {
      this.updateChildren()
    },
    beforeDestroy() {
      this.pause()
    },
    computed: {
      selectedIndex () {
        let index = this.names.indexOf(this.selected)
        return index === -1 ? 0 : index
      },
      names () {
        return this.items.map(vm => vm.name)
      },
      items() {
        return this.$children.filter(vm=>vm.$options.name === 'WheelSlidesItem')
      }
    },
    methods: {
      onClickPrev () {
        this.select(this.selectedIndex - 1)
      },
      onClickNext () {
        this.select(this.selectedIndex + 1)
      },
      onMouseEnter () {
        this.pause()
      },
      onMouseLeave () {
        this.playAutomatically()
      },
      onTouchStart(e) {
        this.pause()
        if(e.touches.lenth > 1) {return} // 多点触控不认为在滑动
        this.startTouch = e.touches[0]
      },
      onTouchEnd(e) {
        let endTouch = e.changedTouches[0]
        let {clientX: x1, clientY: y1} = this.startTouch
        let {clientX: x2, clientY: y2} = endTouch
        let distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
        let deltaY = Math.abs(y2 - y1)
        let rate = distance / deltaY
        if (rate > 2) { // 小于30度角才认为在滑动
          if (x2 > x1) {
            this.select(this.selectedIndex - 1)
          } else {
            this.select(this.selectedIndex + 1)
          }
        }
        // 滑动完再自动播放
        this.$nextTick(() => {
          this.playAutomatically()
        })
      },
      playAutomatically () {
      I  if (this.timerId) { return }
        let run = () => {
          let index = this.names.indexOf(this.getSelected())
          let newIndex = index + 1
          this.select(newIndex) // 告诉外界选中 newIndex
          this.timerId = setTimeout(run, this.autoPlayDelay)
        }
        this.timerId = setTimeout(run, this.autoPlayDelay)
      },
      pause () {
        window.clearTimeout(this.timerId)
        this.timerId = undefined
      },
      select (newIndex) {
        this.lastSelectedIndex = this.selectedIndex
        if (newIndex === -1) {newIndex = this.names.length - 1}
      I  if (newIndex === this.names.length) { newIndex = 0 }
        this.$emit('update:selected', this.names[newIndex])
      },
      getSelected () {
        let first = this.items[0]
        return this.selected || first.name
      },
      updateChildren () {
        let selected = this.getSelected()
        this.items.forEach((vm) => {
          let reverse = this.selectedIndex > this.lastSelectedIndex ? false : true
          if (this.timerId) {
          I  if (this.lastSelectedIndex === this.items.length - 1 && this.selectedIndex === 0) {
              reverse = false
            }
          I  if (this.lastSelectedIndex === 0 && this.selectedIndex === this.items.length - 1) {
              reverse = true
            }
          }
          vm.reverse = reverse
          this.$nextTick(() => {
            vm.selected = selected
          })
        })
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  .w-slides {
    &-window {overflow: hidden;}
    &-wrapper {
      position: relative;
    }
    &-dots {
      padding: 8px 0;
      display: flex;
      justify-content: center;
      align-items: center;
      > span {
        width: 20px;
        height: 20px;
        display: inline-flex;
        justify-content: center;
        align-items: center;
        background: #ddd;
        border-radius: 50%;
        margin: 0 8px;
        font-size: 12px;
        &:hover {
          cursor: pointer;
        }
        &.active {
          background: red;
          background: black;
          color: white;
          &:hover {
            cursor: default;
          }
        }
      }
    }
  }
</style>