he wei
2025-04-23 b9bd29a1a81f6f7de479e3cc3fdfe3d85fc660bf
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
<script setup name="ScrollPane">
import { ref, computed, onMounted, onBeforeUnmount, nextTick } from "vue";
 
const $emit = defineEmits(["scroll"]);
const tagAndTagSpacing = 0; // tagAndTagSpacing
const left = ref(0);
const scrollContainer = ref();
const props = defineProps({
  tagRefs: {
    type: Array,
    default: () => [],
  },
});
 
const wrap = computed(() => {
  return scrollContainer.value.wrapRef;
});
 
function handleScroll(e) {
  const eventDelta = e.wheelDelta || -e.deltaY * 40;
  scrollContainer.value.scrollLeft = wrap.scrollLeft + eventDelta / 4;
}
function emitScroll() {
  $emit("scroll");
}
function moveToTarget(currentTag) {
  // const $container = $refs.scrollContainer.$el;
  const $container = scrollContainer.value.$el;
  const $containerWidth = $container.offsetWidth;
  // const $scrollWrapper = scrollWrapper;
  const $scrollWrapper = scrollContainer.value;
  // const tagList = $parent.$refs.tag;
  const tagList = props.tagRefs;
 
  let firstTag = null;
  let lastTag = null;
 
  // find first tag and last tag
  if (tagList.length > 0) {
    firstTag = tagList[0];
    lastTag = tagList[tagList.length - 1];
  }
 
  if (firstTag === currentTag) {
    $scrollWrapper.scrollLeft = 0;
  } else if (lastTag === currentTag) {
    $scrollWrapper.scrollLeft = $scrollWrapper.scrollWidth - $containerWidth;
  } else {
    // find preTag and nextTag
    const currentIndex = tagList.findIndex((item) => item === currentTag);
    const prevTag = tagList[currentIndex - 1];
    const nextTag = tagList[currentIndex + 1];
 
    // the tag's offsetLeft after of nextTag
    const afterNextTagOffsetLeft =
      nextTag.$el.offsetLeft + nextTag.$el.offsetWidth + tagAndTagSpacing;
 
    // the tag's offsetLeft before of prevTag
    const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing;
 
    if (afterNextTagOffsetLeft > $scrollWrapper.scrollLeft + $containerWidth) {
      $scrollWrapper.scrollLeft = afterNextTagOffsetLeft - $containerWidth;
    } else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) {
      $scrollWrapper.scrollLeft = beforePrevTagOffsetLeft;
    }
  }
}
defineExpose({
  moveToTarget,
});
</script>
<template>
  <el-scrollbar
    ref="scrollContainer"
    :vertical="false"
    class="scroll-container"
  >
    <!-- @wheel.prevent="handleScroll" -->
    <slot />
  </el-scrollbar>
</template>
 
<style lang="less" scoped>
.scroll-container {
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  width: 100%;
  :deep(.el-scrollbar__bar) {
    bottom: 0px;
  }
  :deep(.el-scrollbar__wrap) {
    overflow-x: hidden;
  }
}
 
.scroll-container::-webkit-scrollbar {
  height: 0;
}
 
.scroll-container::-webkit-scrollbar-thumb {
  /* 滚动条里面小方块 */
  border-radius: 10px;
  -webkit-box-shadow: none;
  background: none;
}
.scroll-container::-webkit-scrollbar-track {
  /* 滚动条里面轨道 */
  -webkit-box-shadow: none;
  border-radius: 10px;
  background: none;
}
</style>