fix(components): 修复多页签Tab自动滚动问题

This commit is contained in:
Soybean 2021-11-25 21:48:39 +08:00
parent 93f9aa9584
commit 20aa39f14e
3 changed files with 34 additions and 8 deletions

View File

@ -1,5 +1,5 @@
<template> <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 }"> <div ref="scrollbarContent" class="inline-block" :class="{ 'h-full': !isScrollY }">
<slot></slot> <slot></slot>
</div> </div>

View File

@ -1,5 +1,5 @@
<template> <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 <chrome-tab
v-for="(item, index) in app.multiTab.routes" v-for="(item, index) in app.multiTab.routes"
:key="item.path" :key="item.path"
@ -8,7 +8,7 @@
:closable="item.name !== ROUTE_HOME.name" :closable="item.name !== ROUTE_HOME.name"
:dark-mode="theme.darkMode" :dark-mode="theme.darkMode"
:is-last="index === app.multiTab.routes.length - 1" :is-last="index === app.multiTab.routes.length - 1"
@click="handleClickChromeTab($event, item.fullPath)" @click="handleClickTab(item.fullPath)"
@close="removeMultiTab(item.fullPath)" @close="removeMultiTab(item.fullPath)"
@contextmenu="handleContextMenu($event, item.fullPath)" @contextmenu="handleContextMenu($event, item.fullPath)"
> >
@ -40,7 +40,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { reactive, nextTick } from 'vue'; import { ref, reactive, nextTick, watch } from 'vue';
import { useEventListener } from '@vueuse/core'; import { useEventListener } from '@vueuse/core';
import { useThemeStore, useAppStore } from '@/store'; import { useThemeStore, useAppStore } from '@/store';
import { ROUTE_HOME } from '@/router'; import { ROUTE_HOME } from '@/router';
@ -69,11 +69,22 @@ function setDropdownConfig(x: number, y: number, currentPath: string) {
Object.assign(dropdownConfig, { x, y, currentPath }); Object.assign(dropdownConfig, { x, y, currentPath });
} }
function handleClickChromeTab(e: MouseEvent, fullPath: string) { // tabclientX
emit('scroll', e.clientX); const chromeTabRef = ref<HTMLElement | null>(null);
handleClickTab(fullPath); 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) { function handleContextMenu(e: MouseEvent, fullPath: string) {
e.preventDefault(); e.preventDefault();
const { clientX, clientY } = e; const { clientX, clientY } = e;
@ -88,5 +99,15 @@ function handleContextMenu(e: MouseEvent, fullPath: string) {
useEventListener(window, 'beforeunload', () => { useEventListener(window, 'beforeunload', () => {
setTabRouteStorage(app.multiTab.routes); setTabRouteStorage(app.multiTab.routes);
}); });
watch(
() => app.activeMultiTabIndex,
() => {
getActiveChromeTabClientX();
},
{
immediate: true
}
);
</script> </script>
<style scoped></style> <style scoped></style>

View File

@ -32,7 +32,12 @@ const bsScroll = ref<ExposeBetterScroll | null>(null);
function handleScroll(clientX: number) { function handleScroll(clientX: number) {
const currentX = clientX - bsWrapperLeft.value; const currentX = clientX - bsWrapperLeft.value;
const deltaX = currentX - bsWrapperWidth.value / 2; 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() { function init() {