From 9a333fd8b920141715183216dca9ed524be2378e Mon Sep 17 00:00:00 2001 From: cloverwwang Date: Sun, 1 Dec 2024 19:57:51 +0800 Subject: [PATCH] feat(tabs): scroll the active tab to the center position #1648 --- src/tabs/tabs.tsx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/tabs/tabs.tsx b/src/tabs/tabs.tsx index 5fdae8e57..1fb152f35 100644 --- a/src/tabs/tabs.tsx +++ b/src/tabs/tabs.tsx @@ -124,13 +124,9 @@ export default defineComponent({ if (!tab) return; const tabLeft = tab?.offsetLeft; const tabWidth = tab?.offsetWidth; - const navScrollScrollLeft = navScroll.value.scrollLeft; const navScrollWidth = navScroll.value.offsetWidth; - if (tabLeft + tabWidth - navScrollScrollLeft > navScrollWidth) { - navScroll.value.scrollTo({ left: tabLeft + tabWidth - navScrollWidth, behavior: 'smooth' }); - } else if (tabLeft < navScrollScrollLeft) { - navScroll.value.scrollTo({ left: tabLeft, behavior: 'smooth' }); - } + const scrollDistance = tabLeft - navScrollWidth / 2 + tabWidth / 2; + navScroll.value.scrollTo({ left: scrollDistance, behavior: 'smooth' }); } };