File tree Expand file tree Collapse file tree 1 file changed +17
-6
lines changed
Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Original file line number Diff line number Diff 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
3748const handleMouseLeave = () => {
3849 hoveredItem .value = null
You can’t perform that action at this time.
0 commit comments