Skip to content

Commit aed117d

Browse files
committed
优化导航背景位置
1 parent 0e634e6 commit aed117d

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/components/Header.vue

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,24 @@ const updateBackgroundPosition = (path) => {
2626
document.documentElement.style.setProperty('--item-left', `${rect.left - parentRect.left}px`)
2727
}
2828
29-
const isLinkActive = (path) => path === '/' ? route.path === '/' : route.path.startsWith(path)
30-
31-
watch([hoveredItem, () => route.path], ([newPath]) => {
32-
if (newPath) {
33-
updateBackgroundPosition(newPath)
29+
const isLinkActive = (path) => {
30+
if (path === '/') {
31+
return route.path === '/'
3432
}
35-
}, { immediate: true })
33+
return route.path.startsWith(path)
34+
}
35+
36+
// 监听路由变化和悬停状态
37+
watch(
38+
[() => route.path, hoveredItem],
39+
([newPath, newHoveredItem]) => {
40+
const activePath = newHoveredItem || navItems.find(item => isLinkActive(item.path))?.path
41+
if (activePath) {
42+
updateBackgroundPosition(activePath)
43+
}
44+
},
45+
{ immediate: true }
46+
)
3647
3748
const handleMouseLeave = () => {
3849
hoveredItem.value = null

0 commit comments

Comments
 (0)