<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>
|