wd-index-bar.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="wd-index-bar" :id="indexBarId">
  3. <!-- #ifdef MP-DINGTALK -->
  4. <view class="wd-index-bar" :id="indexBarId">
  5. <!-- #endif -->
  6. <scroll-view :scrollTop="scrollState.scrollTop" :scroll-y="true" class="wd-index-bar__content" @scroll="hanleScroll">
  7. <slot></slot>
  8. </scroll-view>
  9. <view
  10. class="wd-index-bar__sidebar"
  11. @touchstart.stop.prevent="handleTouchStart"
  12. @touchmove.stop.prevent="handleTouchMove"
  13. @touchend.stop.prevent="handleTouchEnd"
  14. @touchcancel.stop.prevent="handleTouchEnd"
  15. >
  16. <view class="wd-index-bar__index" :class="{ 'is-active': item.index === state.activeIndex }" v-for="item in children" :key="item.index">
  17. {{ item.index }}
  18. </view>
  19. </view>
  20. <!-- #ifdef MP-DINGTALK -->
  21. </view>
  22. <!-- #endif -->
  23. </view>
  24. </template>
  25. <script setup lang="ts">
  26. import type { AnchorIndex } from './type'
  27. import { indexBarInjectionKey, indexBarProps } from './type'
  28. import { ref, getCurrentInstance, onMounted, reactive, nextTick, watch } from 'vue'
  29. import { getRect, isDef, uuid, pause } from '../common/util'
  30. import { useChildren } from '../composables/useChildren'
  31. const props = defineProps(indexBarProps)
  32. const indexBarId = ref<string>(`indexBar${uuid()}`)
  33. const { proxy } = getCurrentInstance()!
  34. const state = reactive({
  35. activeIndex: null as AnchorIndex | null
  36. })
  37. const { linkChildren, children } = useChildren(indexBarInjectionKey)
  38. linkChildren({ props, anchorState: state })
  39. watch(
  40. () => children,
  41. (newValue) => {
  42. if (!newValue.length) {
  43. state.activeIndex = null // 或者设置为一个默认值,如第一个子项的索引
  44. return
  45. }
  46. if (!isDef(state.activeIndex) || !newValue.find((item) => item.index === state.activeIndex)) {
  47. state.activeIndex = newValue[0].index
  48. }
  49. },
  50. { deep: true, immediate: true }
  51. )
  52. const scrollState = reactive({
  53. scrollTop: 0, // 即将滚动到的位置
  54. prevScrollTop: 0, // 上次记录的位置
  55. // 滚动距离
  56. touching: false
  57. })
  58. // 组件距离页面顶部的高度
  59. let offsetTop = 0
  60. let sidebarInfo = {
  61. // 侧边栏距离顶部的高度
  62. offsetTop: 0,
  63. // 高度固定24px
  64. indexHeight: 24
  65. }
  66. function init() {
  67. setTimeout(() => {
  68. Promise.all([
  69. getRect(`#${indexBarId.value}`, false, proxy),
  70. getRect('.wd-index-bar__sidebar', false, proxy),
  71. getRect('.wd-index-bar__index', false, proxy)
  72. ]).then(([bar, sidebar, index]) => {
  73. offsetTop = bar.top!
  74. sidebarInfo.offsetTop = sidebar.top!
  75. sidebarInfo.indexHeight = index.height!
  76. })
  77. }, 100)
  78. }
  79. onMounted(() => {
  80. init()
  81. })
  82. function hanleScroll(scrollEvent: any) {
  83. if (scrollState.touching) {
  84. return
  85. }
  86. const { detail } = scrollEvent
  87. const scrolltop = Math.floor(detail.scrollTop)
  88. const anchor = children.find((item, index) => {
  89. if (!isDef(children[index + 1])) return true
  90. if (item.$.exposed!.top.value - offsetTop <= scrolltop && children[index + 1].$.exposed!.top.value - offsetTop > scrolltop) return true
  91. return false
  92. })
  93. if (isDef(anchor) && state.activeIndex !== anchor.index) {
  94. state.activeIndex = anchor.index
  95. }
  96. scrollState.prevScrollTop = scrolltop
  97. }
  98. function getAnchorByPageY(pageY: number) {
  99. const y = pageY - sidebarInfo.offsetTop
  100. let idx = Math.floor(y / sidebarInfo.indexHeight)
  101. if (idx < 0) idx = 0
  102. else if (idx > children.length - 1) idx = children.length - 1
  103. return children[idx]
  104. }
  105. function handleTouchStart() {
  106. scrollState.touching = true
  107. }
  108. function handleTouchMove(e: TouchEvent) {
  109. const clientY = e.touches[0].pageY
  110. if (state.activeIndex === getAnchorByPageY(clientY).index) {
  111. return
  112. }
  113. state.activeIndex = getAnchorByPageY(clientY).index
  114. setScrollTop(getAnchorByPageY(clientY).$.exposed!.top.value - offsetTop)
  115. }
  116. async function handleTouchEnd(e: TouchEvent) {
  117. const clientY = e.changedTouches[0].pageY
  118. state.activeIndex = getAnchorByPageY(clientY).index
  119. setScrollTop(getAnchorByPageY(clientY).$.exposed!.top.value - offsetTop)
  120. await pause()
  121. scrollState.touching = false
  122. }
  123. function setScrollTop(top: number) {
  124. if (scrollState.scrollTop === top) {
  125. scrollState.scrollTop = scrollState.prevScrollTop
  126. nextTick(() => {
  127. scrollState.scrollTop = top
  128. })
  129. } else {
  130. scrollState.scrollTop = top
  131. }
  132. }
  133. </script>
  134. <style lang="scss" scoped>
  135. @import './index.scss';
  136. </style>