From 98c3c5a6e20378e5ebd8de1a043cbc94249cf495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Fri, 31 Jul 2026 00:06:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B7=AF=E7=94=B1=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E5=90=8E=E6=BB=9A=E5=8A=A8=E5=88=B0=E9=A1=B6=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 详情页→列表→另一个详情页时滚动位置保留在前一页的偏移, 用 router.afterEach 统一处理,每次导航回到顶部。 --- src/main.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main.ts b/src/main.ts index cb256f5..e808d31 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,6 +34,18 @@ export const createApp = ViteSSG( ({ router }) => { /** 显式使用 history 模式,避免 hash 模式下 SSG 路由不生效 */ router.options.history = createWebHistory(import.meta.env.BASE_URL); + + /** + * 路由切换时滚动到顶部 + * + * 默认 Vue Router 不处理滚动位置,SPA 内导航会保留前一页的滚动偏移, + * 从详情页→列表→另一个详情页时用户看到的不是页面顶部而是中间位置。 + */ + router.afterEach((_to, _from) => { + if (typeof window === "undefined") return; + /** nextTick 确保 DOM 已更新到新页面后再滚动 */ + window.scrollTo({ top: 0 }); + }); }, );