diff --git a/src/router/index.ts b/src/router/index.ts index e60814c..483145e 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -762,12 +762,28 @@ const routes: RouteRecordRaw[] = [ const router = createRouter({ history: createWebHistory(), routes, - scrollBehavior(_to, _from, savedPosition) { + scrollBehavior(to, from, savedPosition) { + console.log('🔍 scrollBehavior 触发:', { + to: to.path, + from: from.path, + savedPosition, + currentScrollY: window.scrollY + }) + + // 如果是浏览器前进/后退,使用保存的位置 if (savedPosition) { + console.log('📍 使用保存的位置:', savedPosition) return savedPosition - } else { - return { top: 0 } } + + // 对于所有新的路由跳转,都滚动到顶部 + console.log('⬆️ 准备滚动到顶部') + return new Promise((resolve) => { + setTimeout(() => { + console.log('⬆️ 执行滚动到顶部') + resolve({ top: 0, behavior: 'smooth' }) + }, 100) // 延迟确保页面已渲染 + }) } }) @@ -801,4 +817,47 @@ router.beforeEach((to, from, next) => { }) }) +// 路由跳转完成后的处理 +router.afterEach((to, from) => { + console.log('🔍 router.afterEach 触发:', { + to: to.path, + from: from.path, + windowScrollY: window.scrollY, + documentScrollTop: document.documentElement.scrollTop, + bodyScrollTop: document.body.scrollTop + }) + + // 检查所有可能的滚动容器 + setTimeout(() => { + console.log('🔍 检查滚动容器:') + console.log('- window.scrollY:', window.scrollY) + console.log('- document.documentElement.scrollTop:', document.documentElement.scrollTop) + console.log('- document.body.scrollTop:', document.body.scrollTop) + + // 查找所有可能的滚动容器 + const scrollableElements = document.querySelectorAll('.n-layout, .n-layout-content, .content, .app-layout') + scrollableElements.forEach((el, index) => { + console.log(`- 滚动容器 ${index}:`, el.className, 'scrollTop:', el.scrollTop) + }) + + // 强制滚动所有容器到顶部 + console.log('⬆️ 强制滚动所有容器到顶部') + window.scrollTo(0, 0) + document.documentElement.scrollTop = 0 + document.body.scrollTop = 0 + + scrollableElements.forEach((el) => { + el.scrollTop = 0 + }) + + // 最后检查 + setTimeout(() => { + console.log('⬆️ 最终检查所有滚动位置:') + console.log('- window.scrollY:', window.scrollY) + console.log('- document.documentElement.scrollTop:', document.documentElement.scrollTop) + console.log('- document.body.scrollTop:', document.body.scrollTop) + }, 100) + }, 100) +}) + export default router \ No newline at end of file diff --git a/src/views/CourseExchanged.vue b/src/views/CourseExchanged.vue index ff8ad59..69417c2 100644 --- a/src/views/CourseExchanged.vue +++ b/src/views/CourseExchanged.vue @@ -64,7 +64,7 @@