Skip to content

Commit c25fb4a

Browse files
handsomezywkagol
authored andcommitted
fix(tabs): 修复tabs溢出鼠标拖动tab不会滑动的问题
1 parent 718d1b9 commit c25fb4a

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

packages/devui-vue/devui/tabs/src/components/tab-nav/tab-nav.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ $devui-tab-slider-bg: $devui-list-item-hover-bg;
295295
padding-left: 0;
296296
overflow-y: hidden;
297297
overflow-x: scroll;
298+
user-select: none;
298299
// 隐藏滚动条
299300
scrollbar-width: none;
300301
-ms-overflow-style: none;

packages/devui-vue/devui/tabs/src/components/tab-nav/tab-nav.tsx

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
onBeforeMount,
66
onMounted,
77
onUpdated,
8+
onUnmounted,
89
reactive,
910
SetupContext,
1011
shallowRef,
@@ -33,9 +34,60 @@ export default defineComponent({
3334
const { update, beforeMount, mounted, activeClick, tabCanClose } = useTabNavFunction(props, tabs, tabsList, data, ctx, tabsEle);
3435
const { onTabRemove, onTabAdd } = useTabNavEvent(ctx);
3536

37+
// 添加新的tab选项
38+
const handleTabAdd = () => {
39+
onTabAdd();
40+
nextTick(() => {
41+
// 使每次添加新tab后,滚动条都在最右侧
42+
if (tabsEle.value) {
43+
tabsEle.value.scrollLeft = tabsEle.value.scrollWidth;
44+
}
45+
});
46+
};
47+
48+
// 鼠标是否在滑动
49+
let isSlide = false;
50+
// tab滑动
51+
const handleSlideTab = (mousedownEvent: MouseEvent) => {
52+
if (tabsEle.value) {
53+
// 鼠标按下x坐标
54+
const mousedownX = mousedownEvent.clientX;
55+
// 当前滚动条距离
56+
const scrollLeft = tabsEle.value.scrollLeft;
57+
isSlide = true;
58+
// 监听鼠标滑动
59+
tabsEle.value.addEventListener('mousemove', (mousemoveEvent: MouseEvent) => {
60+
if (isSlide && tabsEle.value) {
61+
// 当前鼠标移动x坐标
62+
const mousemoveX = mousemoveEvent.clientX;
63+
// 滑动距离
64+
const scrollWidth = mousemoveX - mousedownX;
65+
tabsEle.value.scrollLeft = scrollLeft - scrollWidth;
66+
}
67+
});
68+
tabsEle.value.addEventListener('mouseup', () => {
69+
isSlide = false;
70+
});
71+
tabsEle.value.addEventListener('mouseleave', () => {
72+
isSlide = false;
73+
});
74+
}
75+
};
76+
3677
onUpdated(() => update());
3778
onBeforeMount(() => beforeMount());
38-
onMounted(() => mounted());
79+
onMounted(() => {
80+
mounted();
81+
// tab超出容器后监听滑动
82+
if (tabsEle.value) {
83+
tabsEle.value.addEventListener('mousedown', handleSlideTab);
84+
}
85+
});
86+
onUnmounted(() => {
87+
if (tabsEle.value) {
88+
tabsEle.value.removeEventListener('mousedown', handleSlideTab);
89+
}
90+
});
3991

4092
watch(
4193
() => props.modelValue,
@@ -47,16 +99,6 @@ export default defineComponent({
4799
}
48100
);
49101

50-
const handleTabAdd = () => {
51-
onTabAdd();
52-
nextTick(() => {
53-
// 使每次添加新tab后,滚动条都在最右侧
54-
if (tabsEle.value) {
55-
tabsEle.value.scrollLeft = tabsEle.value.scrollWidth;
56-
}
57-
});
58-
};
59-
60102
return () => {
61103
const closeIconEl = (item: TabsStateData) => {
62104
return tabCanClose(item) ? (

0 commit comments

Comments
 (0)