
<style scoped>
.bIndexList-container {
  position: relative;
  padding: 0;
}
.bIndexList-container .controller-container {
  display: flex;
  position: fixed;
  top: 130px;
  max-width: 40px;
  right: 10px;
  padding: 8px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: white;
  box-shadow: 0 4px 10px 0 rgba(0,0,0,0.1);
  border-radius: 8px;
  z-index: 999;
}
.bIndexList-container .controller-container .controller:not(:first-child) {
  padding-top: 5px;
}
.bIndexList-container .controller {
  width: 100%;
  height: 35px;
  text-align: center;
  line-height: 35px;
  font-size: 26px;
  color: #666;
  font-family: Georgia, 'Times New Roman', Times, serif;
}
.bIndexList-container .controller-touched {
  position: absolute;
  width: 80px;
  height: 80px;
  top: 5%;
  right: 120%;
  text-align: center;
  line-height: 80px;
  font-size: 28px;
  color: #222;
  background: #8B8989;
  transition: all 0.5s ease-in-out;
  border-radius: 8px;
}
.bIndexList-container .list-container {
  position: relative;
  width: 100vw;
  background:rgb(255, 255, 252);
  z-index: 1;
}
.bIndexList-container .list-title {
  display: flex;
  min-height: 70px;
  padding: 5px 10px;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 30px;
  color: #222;
  background: #DCDCDC;
}
.bIndexList-container .list-content {
  display: flex;
  min-height: 50px;
  padding: 5px 10px;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 30px;
  color: #666;
}
</style>

<template>
  <div class="bIndexList-container">
    <div class="list-container">
      <div
        v-for="(item ,index) in data"
        :key="index" class="list"
      >
        <div class="list-title">{{item.title}}</div>
        <div @click.stop="listClick(_item, _index, index)" v-for="(_item, _index) in item.list" :key="_index" class="list-content">
          {{_item}}
        </div>
      </div>
    </div>
    <div
      @touchmove.stop="controllerTouchmove"
      @touchend.stop="controllerTouchend"
      @click="controllerClick"
      class="controller-container"
    >
      <div v-show="controllerTouched && barHeight && barOffset" class="controller-touched">
        {{controllerTouched && data[controllerTouched - 1].title}}
      </div>
      <div
        :data-touchIndex="index"
        v-for="(item ,index) in data" :key="index"
        class="controller"
      >
        {{item.title}}
      </div>
    </div>
  </div>
</template>
<script>
export default {
  /**
   * data结构：
   * [
   *  {
   *    title: String
   *    list: [
   *      item1: String,
   *      item2: String
   *    ]
   *  }
   * ]
  */
  props: {
    data: {
      type: Array,
      default: () => []
    }
  },
  data: () => {
    return {
      barOffset: undefined,
      barHeight: undefined,
      controllerTouched: undefined,
      listOffset: 0,
      listOffsets: []
    }
  },
  mounted () {
    const that = this
    // 获取整体容器长度、控制条长度、距离顶部高度
    wx.createSelectorQuery().select('.controller-container').boundingClientRect(function (res) {
      that.barOffset = res.top
    }).exec()
    wx.createSelectorQuery().select('.controller').boundingClientRect(function (res) {
      that.barHeight = res.height
    }).exec()
    wx.createSelectorQuery().selectAll('.list-title').boundingClientRect(function (res) {
      that.listOffsets = res.map(item => item.top)
    }).exec()
  },
  methods: {
    controllerTouchstart (e) {
      return false
    },
    controllerTouchmove (e) {
      if (this.barOffset && this.barHeight) {
        this.controllerTouched = Math.ceil((e.clientY - this.barOffset) / this.barHeight) >= this.data.length
          ? this.data.length
          : Math.ceil((e.clientY - this.barOffset) / this.barHeight)
        wx.pageScrollTo({
          scrollTop: this.listOffsets[this.controllerTouched - 1]
        })
      }
    },
    controllerTouchend (e) {
      setTimeout(() => {
        this.controllerTouched = 0
      }, 500)
    },
    controllerClick (e) {
      this.controllerTouched = Math.ceil((e.clientY - this.barOffset) / this.barHeight) >= this.data.length
        ? this.data.length
        : Math.ceil((e.clientY - this.barOffset) / this.barHeight)
      wx.pageScrollTo({
        scrollTop: this.listOffsets[this.controllerTouched - 1]
      })
    },
    listClick (item, index, totalIndex) {
      this.$emit('click', item, index, totalIndex)
    }
  },
  computed: {
  }
}
</script>

