Skip to content

Commit 2f6f07d

Browse files
committed
修复激活标签页定位可能不居中的问题
1 parent e87b72f commit 2f6f07d

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/layouts/components/Topbar/Tabbar/index.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,20 @@ const tabRef = useTemplateRef<HTMLElement[]>('tabRef')
3030
3131
watch(() => route, (val) => {
3232
if (settingsStore.settings.tabbar.enable) {
33-
tabbarStore.add(val).then(() => {
33+
tabbarStore.add(val)
34+
nextTick(() => {
3435
const index = tabbarStore.list.findIndex(item => item.tabId === activedTabId.value)
3536
if (index !== -1) {
36-
tabRef.value && tabsRef.value?.scrollTo(tabRef.value[index].offsetLeft - tabsRef.value.ref?.$el.clientWidth * 0.4)
37+
const tabEl = tabRef.value?.find(item => Number.parseInt(item.dataset.index!) === index)
38+
const containerEl = tabsRef.value?.ref?.$el
39+
if (tabEl && containerEl) {
40+
const tabLeft = tabEl.offsetLeft
41+
const tabWidth = tabEl.offsetWidth
42+
const containerWidth = containerEl.clientWidth
43+
// 计算滚动位置,使标签页居中
44+
const scrollLeft = tabLeft - (containerWidth - tabWidth) / 2
45+
tabsRef.value?.scrollTo(scrollLeft)
46+
}
3747
tabbarScrollTip()
3848
}
3949
})

src/store/modules/tabbar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const useTabbarStore = defineStore(
1212
const leaveIndex = ref(-1)
1313

1414
// 添加标签页
15-
async function add(route: RouteLocationNormalized) {
15+
function add(route: RouteLocationNormalized) {
1616
const names: string[] = []
1717
route.matched.forEach((v, i) => {
1818
if (i > 0) {

0 commit comments

Comments
 (0)