dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
360
361
<template>
  <!-- 支付宝小程序使用_tGetRect()获取组件的根元素尺寸,所以在外面套一个"壳" -->
  <view>
    <view class="tn-index-list-class tn-index-list">
      <slot></slot>
      
      <!-- 侧边栏 -->
      <view
        v-if="showSidebar"
        class="tn-index-list__sidebar"
        @touchstart.stop.prevent="onTouchMove"
        @touchmove.stop.prevent="onTouchMove"
        @touchend.stop.prevent="onTouchStop"
        @touchcancel.stop.prevent="onTouchStop"
      >
        <view
          v-for="(item, index) in indexList"
          :key="index"
          class="tn-index-list__sidebar__item"
          :style="{
            zIndex: zIndex + 1,
            color: activeAnchorIndex === index ? activeColor : ''
          }"
        >
          {{ item }}
        </view>
      </view>
      
      <!-- 选中弹出框 -->
      <view
        v-if="touchMove && indexList[touchMoveIndex]"
        class="tn-index-list__alert"
        :style="{
          zIndex: selectAlertZIndex
        }"
      >
        <text>{{ indexList[touchMoveIndex] }}</text>
      </view>
    </view>
  </view>
</template>
 
<script>
  // 生成 A-Z的字母列表
  let indexList = function() {
    let indexList = []
    let charCodeOfA = 'A'.charCodeAt(0)
    for (var i = 0; i < 26; i++) {
      indexList.push(String.fromCharCode(charCodeOfA + i))
    }
    return indexList
  }
  
  export default {
    name: 'tn-index-list',
    props: {
      // 索引列表
      indexList: {
        type: Array,
        default() {
          return indexList()
        }
      },
      // 是否自动吸顶
      sticky: {
        type: Boolean,
        default: true
      },
      // 自动吸顶时距离顶部的距离,单位px
      stickyTop: {
        type: Number,
        default: 0
      },
      // 自定义顶栏的高度,单位px
      customBarHeight: {
        type: Number,
        default: 0
      },
      // 当前滚动的高度
      // 由于自定义组件无法获取滚动高度,所以依赖传入
      scrollTop: {
        type: Number,
        default: 0
      },
      // 选中索引时的颜色
      activeColor: {
        type: String,
        default: '#01BEFF'
      },
      // 吸顶时的z-index
      zIndex: {
        type: Number,
        default: 0
      }
    },
    computed: {
      // 选中索引列表弹出提示框的z-index
      selectAlertZIndex() {
        return this.$t.zIndex.toast
      },
      // 吸顶的偏移高度
      stickyOffsetTop() {
        // #ifdef H5
        return this.stickyTop !== '' ? this.stickyTop : 44
        // #endif
        // #ifndef H5
        return this.stickyTop !== '' ? this.stickyTop : 0
        // #endif
      }
    },
    data() {
      return {
        // 当前激活的列表锚点的序号
        activeAnchorIndex: 0,
        // 显示侧边索引栏
        showSidebar: true,
        // 标记是否开始触摸移动
        touchMove: false,
        // 当前触摸移动到对应索引的序号
        touchMoveIndex: 0,
        // 滚动到对应锚点的序号
        scrollToAnchorIndex: 0,
        // 侧边栏的信息
        sidebar: {
          height: 0,
          top: 0
        },
        // 内容区域高度
        height: 0,
        // 内容区域top
        top: 0
      }
    },
    watch: {
      scrollTop() {
        this.updateData()
      }
    },
    created() {
      // 只能在created生命周期定义childrens,如果在data定义,会因为循环引用而报错
      this.childrens = []
    },
    methods: {
      // 更新数据
      updateData() {
        this.timer && clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.showSidebar = !!this.childrens.length
          this.getRect().then(() => {
            this.onScroll()
          })
        }, 0)
      },
      // 获取对应的信息
      getRect() {
        return Promise.all([
          this.getAnchorRect(),
          this.getListRect(),
          this.getSidebarRect()
        ])
      },
      // 获取列表内容子元素信息
      getAnchorRect() {
        return Promise.all(this.childrens.map((child, index) => {
          child._tGetRect('.tn-index-anchor__wrap').then((rect) => {
            Object.assign(child, {
              height: rect.height,
              top: rect.top - this.customBarHeight
            })
          })
        }))
      },
      // 获取列表信息
      getListRect() {
        return this._tGetRect('.tn-index-list').then(rect => {
          Object.assign(this, {
            height: rect.height,
            top: rect.top + this.scrollTop
          })
        })
      },
      // 获取侧边滚动栏信息
      getSidebarRect() {
        return this._tGetRect('.tn-index-list__sidebar').then(rect => {
          this.sidebar = {
            height: rect.height,
            top: rect.top
          }
        })
      },
      // 滚动事件
      onScroll() {
        const {
          childrens = []
        } = this
        if (!childrens.length) {
          return
        }
        const {
          sticky,
          stickyOffsetTop,
          zIndex,
          scrollTop,
          activeColor
        } = this
        const active = this.getActiveAnchorIndex()
        this.activeAnchorIndex = active
        if (sticky) {
          let isActiveAnchorSticky = false
          if (active !== -1) {
            isActiveAnchorSticky = childrens[active].top <= 0
          }
          childrens.forEach((item, index) => {
            if (index === active) {
              let wrapperStyle = ''
              let anchorStyle = {
                color: `${activeColor}`
              }
              if (isActiveAnchorSticky) {
                wrapperStyle = {
                  height: `${childrens[index].height}px`
                }
                anchorStyle = {
                  position: 'fixed',
                  top: `${stickyOffsetTop}px`,
                  zIndex: `${zIndex ? zIndex : this.$t.zIndex.indexListSticky}`,
                  color: `${activeColor}`
                }
              }
              item.active = true
              item.wrapperStyle = wrapperStyle
              item.anchorStyle = anchorStyle
            } else if (index === active - 1) {
              const currentAnchor = childrens[index]
              const currentOffsetTop = currentAnchor.top
              const targetOffsetTop = index === childrens.length - 1 ? this.top : childrens[index + 1].top
              const parentOffsetHeight = targetOffsetTop - currentOffsetTop
              const translateY = parentOffsetHeight - currentAnchor.height
              const anchorStyle = {
                position: 'relative',
                transform: `translate3d(0, ${translateY}px, 0)`,
                zIndex: `${zIndex ? zIndex : this.$t.zIndex.indexListSticky}`,
                color: `${activeColor}`
              }
              item.active = false
              item.anchorStyle = anchorStyle
            } else {
              item.active = false
              item.wrapperStyle = ''
              item.anchorStyle = ''
            }
          })
        }
      },
      // 触摸移动
      onTouchMove(event) {
        this.touchMove = true
        const sidebarLength = this.childrens.length
        const touch = event.touches[0]
        const itemHeight = this.sidebar.height / sidebarLength
        let clientY = touch.clientY
        let index = Math.floor((clientY - this.sidebar.top) / itemHeight)
        if (index < 0) {
          index = 0
        } else if (index > sidebarLength - 1) {
          index = sidebarLength - 1
        }
        this.touchMoveIndex = index
        this.scrollToAnchor(index)
      },
      // 触摸停止
      onTouchStop() {
        this.touchMove = false
        this.scrollToAnchorIndex = null
      },
      // 获取当前的锚点序号
      getActiveAnchorIndex() {
        const {
          childrens,
          sticky
        } = this
        for (let i = this.childrens.length - 1; i >= 0; i--) {
          const preAnchorHeight = i > 0 ? childrens[i - 1].height : 0
          const reachTop = sticky ? preAnchorHeight : 0
          if (reachTop >= childrens[i].top) {
            return i
          }
        }
        return -1
      },
      // 滚动到对应的锚点
      scrollToAnchor(index) {
        if (this.scrollToAnchorIndex === index) {
          return
        }
        this.scrollToAnchorIndex = index
        const anchor = this.childrens.find(item => item.index === this.indexList[index])
        if (anchor) {
          const scrollTop = anchor.top + this.scrollTop
          this.$emit('select', {
            index: anchor.index,
            scrollTop: scrollTop
          })
          uni.pageScrollTo({
            duration:0,
            scrollTop: scrollTop
          })
        }
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  
  .tn-index-list {
    position: relative;
    
    &__sidebar {
      display: flex;
      flex-direction: column;
      position: fixed;
      top: 50%;
      right: 0;
      text-align: center;
      transform: translateY(-50%);
      user-select: none;
      z-index: 99;
      
      &__item {
        font-weight: 500;
        padding: 8rpx 18rpx;
        font-size: 22rpx;
        line-height: 1;
      }
    }
    
    &__alert {
      display: flex;
      flex-direction: row;
      position: fixed;
      width: 120rpx;
      height: 120rpx;
      top: 50%;
      right: 90rpx;
      align-items: center;
      justify-content: center;
      margin-top: -60rpx;
      border-radius: 24rpx;
      font-size: 50rpx;
      color: #FFFFFF;
      background-color: $tn-font-sub-color;
      padding: 0;
      z-index: 9999999;
      
      text {
        line-height: 50rpx;
      }
    }
  }
</style>