Skip to content

Commit

Permalink
fix(components): 修复多页签Tab自动滚动问题
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 25, 2021
1 parent 93f9aa9 commit 20aa39f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/custom/BetterScroll/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div ref="scrollbar" class="wh-full text-left">
<div ref="scrollbar" class="h-full text-left">
<div ref="scrollbarContent" class="inline-block" :class="{ 'h-full': !isScrollY }">
<slot></slot>
</div>
Expand Down
33 changes: 27 additions & 6 deletions src/layouts/common/GlobalTab/components/MultiTab/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div v-if="theme.multiTabStyle.mode === 'chrome'" class="flex items-end h-full">
<div v-if="theme.multiTabStyle.mode === 'chrome'" ref="chromeTabRef" class="flex items-end h-full">
<chrome-tab
v-for="(item, index) in app.multiTab.routes"
:key="item.path"
Expand All @@ -8,7 +8,7 @@
:closable="item.name !== ROUTE_HOME.name"
:dark-mode="theme.darkMode"
:is-last="index === app.multiTab.routes.length - 1"
@click="handleClickChromeTab($event, item.fullPath)"
@click="handleClickTab(item.fullPath)"
@close="removeMultiTab(item.fullPath)"
@contextmenu="handleContextMenu($event, item.fullPath)"
>
Expand Down Expand Up @@ -40,7 +40,7 @@
</template>

<script setup lang="ts">
import { reactive, nextTick } from 'vue';
import { ref, reactive, nextTick, watch } from 'vue';
import { useEventListener } from '@vueuse/core';
import { useThemeStore, useAppStore } from '@/store';
import { ROUTE_HOME } from '@/router';
Expand Down Expand Up @@ -69,11 +69,22 @@ function setDropdownConfig(x: number, y: number, currentPath: string) {
Object.assign(dropdownConfig, { x, y, currentPath });
}
function handleClickChromeTab(e: MouseEvent, fullPath: string) {
emit('scroll', e.clientX);
handleClickTab(fullPath);
// 获取当前激活的tab的clientX
const chromeTabRef = ref<HTMLElement | null>(null);
async function getActiveChromeTabClientX() {
await nextTick();
const index = app.activeMultiTabIndex;
if (chromeTabRef.value) {
const activeTabElement = chromeTabRef.value.children[index];
const { x, width } = activeTabElement.getBoundingClientRect();
const clientX = x + width;
setTimeout(() => {
emit('scroll', clientX);
}, 50);
}
}
// 右键菜单
function handleContextMenu(e: MouseEvent, fullPath: string) {
e.preventDefault();
const { clientX, clientY } = e;
Expand All @@ -88,5 +99,15 @@ function handleContextMenu(e: MouseEvent, fullPath: string) {
useEventListener(window, 'beforeunload', () => {
setTabRouteStorage(app.multiTab.routes);
});
watch(
() => app.activeMultiTabIndex,
() => {
getActiveChromeTabClientX();
},
{
immediate: true
}
);
</script>
<style scoped></style>
7 changes: 6 additions & 1 deletion src/layouts/common/GlobalTab/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ const bsScroll = ref<ExposeBetterScroll | null>(null);
function handleScroll(clientX: number) {
const currentX = clientX - bsWrapperLeft.value;
const deltaX = currentX - bsWrapperWidth.value / 2;
bsWrapperRef.value?.scrollBy({ left: deltaX, behavior: 'smooth' });
if (bsScroll.value) {
const { maxScrollX, x: leftX } = bsScroll.value.bsInstance;
const rightX = maxScrollX - leftX;
const update = deltaX > 0 ? Math.max(-deltaX, rightX) : Math.min(-deltaX, -leftX);
bsScroll.value?.bsInstance.scrollBy(update, 0, 300);
}
}
function init() {
Expand Down

1 comment on commit 20aa39f

@vercel
Copy link

@vercel vercel bot commented on 20aa39f Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.