cl-index-bar.uvue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <template>
  2. <view class="cl-index-bar" :class="[pt.className]">
  3. <view
  4. class="cl-index-bar__list"
  5. @touchstart="onTouchStart"
  6. @touchmove.stop.prevent="onTouchMove"
  7. @touchend="onTouchEnd"
  8. >
  9. <view class="cl-index-bar__item" v-for="(item, index) in list" :key="index">
  10. <view
  11. class="cl-index-bar__item-inner"
  12. :class="{
  13. 'is-active': activeIndex == index
  14. }"
  15. >
  16. <text
  17. class="cl-index-bar__item-text"
  18. :class="{
  19. 'is-active': activeIndex == index || isDark
  20. }"
  21. >{{ item }}</text
  22. >
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="cl-index-bar__alert" v-show="showAlert">
  28. <view class="cl-index-bar__alert-icon dark:!bg-surface-800">
  29. <view class="cl-index-bar__alert-arrow dark:!bg-surface-800"></view>
  30. <text class="cl-index-bar__alert-text dark:!text-white">{{ alertText }}</text>
  31. </view>
  32. </view>
  33. </template>
  34. <script setup lang="ts">
  35. import { computed, getCurrentInstance, nextTick, onMounted, ref, watch, type PropType } from "vue";
  36. import { isDark, isEmpty, parsePt } from "@/cool";
  37. defineOptions({
  38. name: "cl-index-bar"
  39. });
  40. const props = defineProps({
  41. pt: {
  42. type: Object,
  43. default: () => ({})
  44. },
  45. modelValue: {
  46. type: Number,
  47. default: 0
  48. },
  49. list: {
  50. type: Array as PropType<string[]>,
  51. default: () => []
  52. }
  53. });
  54. const emit = defineEmits(["update:modelValue", "change"]);
  55. const { proxy } = getCurrentInstance()!;
  56. type PassThrough = {
  57. className?: string;
  58. };
  59. const pt = computed(() => parsePt<PassThrough>(props.pt));
  60. // 存储索引条整体的位置信息
  61. const barRect = ref({
  62. height: 0,
  63. width: 0,
  64. left: 0,
  65. top: 0
  66. } as NodeInfo);
  67. // 存储所有索引项的位置信息数组
  68. const itemsRect = ref<NodeInfo[]>([]);
  69. // 是否正在触摸
  70. const isTouching = ref(false);
  71. // 是否显示提示弹窗
  72. const showAlert = ref(false);
  73. // 当前提示弹窗显示的文本
  74. const alertText = ref("");
  75. // 当前触摸过程中的临时索引
  76. const activeIndex = ref(-1);
  77. /**
  78. * 获取索引条及其所有子项的位置信息
  79. * 用于后续触摸时判断手指所在的索引项
  80. */
  81. function getRect() {
  82. nextTick(() => {
  83. setTimeout(() => {
  84. uni.createSelectorQuery()
  85. .in(proxy)
  86. .select(".cl-index-bar")
  87. .boundingClientRect()
  88. .exec((bar) => {
  89. if (isEmpty(bar)) {
  90. return;
  91. }
  92. // 获取索引条整体的位置信息
  93. barRect.value = bar[0] as NodeInfo;
  94. // 获取所有索引项的位置信息
  95. uni.createSelectorQuery()
  96. .in(proxy)
  97. .selectAll(".cl-index-bar__item")
  98. .boundingClientRect()
  99. .exec((items) => {
  100. if (isEmpty(items)) {
  101. getRect();
  102. return;
  103. }
  104. itemsRect.value = items[0] as NodeInfo[];
  105. });
  106. });
  107. }, 350);
  108. });
  109. }
  110. /**
  111. * 根据触摸点的Y坐标,计算出最接近的索引项下标
  112. * @param clientY 触摸点的Y坐标(相对于屏幕)
  113. * @returns 最接近的索引项下标
  114. */
  115. function getIndex(clientY: number): number {
  116. if (itemsRect.value.length == 0) {
  117. // 没有索引项时,默认返回0
  118. return 0;
  119. }
  120. // 初始化最接近的索引和最小距离
  121. let closestIndex = 0;
  122. let minDistance = Number.MAX_VALUE;
  123. // 遍历所有索引项,找到距离触摸点最近的项
  124. for (let i = 0; i < itemsRect.value.length; i++) {
  125. const item = itemsRect.value[i];
  126. // 计算每个item的中心点Y坐标
  127. const itemCenterY = (item.top ?? 0) + (item.height ?? 0) / 2;
  128. // 计算触摸点到中心点的距离
  129. const distance = Math.abs(clientY - itemCenterY);
  130. // 更新最小距离和索引
  131. if (distance < minDistance) {
  132. minDistance = distance;
  133. closestIndex = i;
  134. }
  135. }
  136. // 边界处理,防止越界
  137. if (closestIndex < 0) {
  138. closestIndex = 0;
  139. } else if (closestIndex >= props.list.length) {
  140. closestIndex = props.list.length - 1;
  141. }
  142. return closestIndex;
  143. }
  144. /**
  145. * 更新触摸过程中的显示状态
  146. * @param index 新的索引
  147. */
  148. function updateActive(index: number) {
  149. // 更新当前触摸索引
  150. activeIndex.value = index;
  151. // 更新弹窗提示文本
  152. alertText.value = props.list[index];
  153. }
  154. /**
  155. * 触摸开始事件处理
  156. * @param e 触摸事件对象
  157. */
  158. function onTouchStart(e: TouchEvent) {
  159. // 标记为正在触摸
  160. isTouching.value = true;
  161. // 显示提示弹窗
  162. showAlert.value = true;
  163. // 获取第一个触摸点
  164. const touch = e.touches[0];
  165. // 计算对应的索引
  166. const index = getIndex(touch.clientY);
  167. // 更新显示状态
  168. updateActive(index);
  169. }
  170. /**
  171. * 触摸移动事件处理
  172. * @param e 触摸事件对象
  173. */
  174. function onTouchMove(e: TouchEvent) {
  175. // 未处于触摸状态时不处理
  176. if (!isTouching.value) return;
  177. // 获取第一个触摸点
  178. const touch = e.touches[0];
  179. // 计算对应的索引
  180. const index = getIndex(touch.clientY);
  181. // 更新显示状态
  182. updateActive(index);
  183. }
  184. /**
  185. * 触摸结束事件处理
  186. * 结束后延迟隐藏提示弹窗,并确认最终选中的索引
  187. */
  188. function onTouchEnd() {
  189. isTouching.value = false; // 标记为未触摸
  190. // 更新值
  191. if (props.modelValue != activeIndex.value) {
  192. emit("update:modelValue", activeIndex.value);
  193. emit("change", activeIndex.value);
  194. }
  195. // 延迟500ms后隐藏提示弹窗,提升用户体验
  196. setTimeout(() => {
  197. showAlert.value = false;
  198. }, 500);
  199. }
  200. watch(
  201. computed(() => props.modelValue),
  202. (val: number) => {
  203. activeIndex.value = val;
  204. },
  205. {
  206. immediate: true
  207. }
  208. );
  209. onMounted(() => {
  210. watch(
  211. computed(() => props.list),
  212. () => {
  213. getRect();
  214. },
  215. {
  216. immediate: true
  217. }
  218. );
  219. });
  220. </script>
  221. <style lang="scss" scoped>
  222. .cl-index-bar {
  223. @apply flex flex-col items-center justify-center;
  224. @apply absolute bottom-0 right-0 h-full;
  225. z-index: 110;
  226. &__item {
  227. @apply flex flex-col items-center justify-center;
  228. width: 50rpx;
  229. height: 34rpx;
  230. &-inner {
  231. @apply rounded-full flex flex-row items-center justify-center;
  232. width: 30rpx;
  233. height: 30rpx;
  234. &.is-active {
  235. @apply bg-primary-500;
  236. }
  237. }
  238. &-text {
  239. @apply text-xs text-surface-500;
  240. &.is-active {
  241. @apply text-white;
  242. }
  243. }
  244. }
  245. }
  246. .cl-index-bar__alert {
  247. @apply absolute bottom-0 right-8 h-full flex flex-col items-center justify-center;
  248. width: 120rpx;
  249. z-index: 110;
  250. &-icon {
  251. @apply rounded-full flex flex-row items-center justify-center;
  252. @apply bg-surface-300;
  253. height: 80rpx;
  254. width: 80rpx;
  255. overflow: visible;
  256. }
  257. &-arrow {
  258. @apply bg-surface-300 absolute;
  259. right: -8rpx;
  260. height: 40rpx;
  261. width: 40rpx;
  262. transform: rotate(45deg);
  263. }
  264. &-text {
  265. @apply text-white text-2xl;
  266. }
  267. }
  268. </style>