Skip to content

fix(sheet): 固定速查表页面左侧目录,滚动时目录不随页面滑走 - #14

Merged
Kepler2024 merged 1 commit into
mainfrom
fix/sheet-sticky-toc
Nov 28, 2026
Merged

Kepler2024 merged 1 commit into
mainfrom
fix/sheet-sticky-toc

Conversation

@Kepler2024

Copy link
Copy Markdown
Owner

问题原因

src/styles/global.css 在 html 和 body 上同时设置了 overflow-x: hidden。按 CSS Overflow 规范,单轴设置 hidden 会让另一轴计算为 auto,导致 body 变成一个与整页等高的滚动容器,从而破坏了 .site-header 和 .doc-sidebar 的 position: sticky。点击左侧目录跳转或向下滚动时,整个页面与目录会一起向上滑出视口。

改动内容

  • src/styles/global.css
    • 将 html 与 body 的 overflow-x: hidden 替换为 overflow-x: clip,在保持防止横向溢出的同时恢复 position: sticky 行为。
    • 将桌面端 .doc-sidebar 的 top 对齐 .doc-layout 顶部内边距(calc(var(--header-height) + 2rem)),补充 100dvh 高度上限与 overscroll-behavior: contain,避免初始滚动抖动和侧边栏滚动穿透。
    • 移动端(<= 900px)恢复顶部目录条吸顶后,提高 scroll-padding-top 至 calc(var(--header-height) + 4.5rem),防止锚点标题被吸顶目录条遮挡;将 .doc-sidebar 的 overflow-y 还原为 visible,为展开后的 .toc-body.expanded 增加 overscroll-behavior: contain。
  • src/components/SearchModal.astro
    • 打开搜索弹窗时改为锁定 document.documentElement.style.overflowY = 'hidden' 并在关闭时立即恢复,避免通过 body.style.overflow 临时制造滚动容器导致吸顶导航/目录闪烁或影响搜索跳转滚动。
  • src/layouts/Layout.astro
    • 当视口较矮导致左侧目录自身出现滚动条时,滚动正文自动保持当前高亮目录项在侧边栏可视范围内(仅滚动侧边栏自身,不影响主页面滚动)。

验证结果

  • 前端构建与验收:根路径(BASE_PATH=/)与子路径(BASE_PATH=/Codesheet)均通过 npm run build && npm run check(22 个中英文页面、链接、锚点、OG、sitemap 全部通过)。
  • 浏览器实测(Headless Chrome):
    • 桌面端 1440×900(/python/)与较矮屏 1280×720(/zh/cpp/):点击目录项(含最后一项)及通过搜索弹窗跳转后,顶部导航固定在 top: 0、左侧目录固定在 top: 96px,仅右侧速查表滚动。
    • 禁用 JavaScript(/cpp/):锚点跳转和目录固定均正常工作。
    • 移动端 390×844(/rust/)与 375×667(/):无横向溢出,顶部导航与目录条吸顶,跳转标题完整露在目录条下方。
  • 示例检查:本次未修改任何 sheets/*.md,未运行 sheet 示例检查器。

`html` and `body` both set `overflow-x: hidden`, which computes
`overflow-y: auto` on `body` and turns it into a full-height scroll
container. As a result, `position: sticky` on `.site-header` and
`.doc-sidebar` stuck to `body` instead of the viewport, causing the
left TOC (and header) to scroll out of view when navigating sections.

- Replace `overflow-x: hidden` with `overflow-x: clip` on `html` and
  `body` so horizontal overflow stays clipped without breaking sticky
  positioning.
- Align desktop `.doc-sidebar` `top` with `.doc-layout` top padding
  (`calc(var(--header-height) + 2rem)`), add `100dvh` max-height and
  `overscroll-behavior: contain`, and keep the active TOC link scrolled
  into view inside overflowing sidebars.
- Increase mobile (`<= 900px`) `scroll-padding-top` so anchor jumps
  land below the restored sticky mobile TOC bar, and reset mobile
  sidebar `overflow-y` to `visible`.
- Lock scrolling on `documentElement.style.overflowY` instead of
  `body.style.overflow` in `SearchModal`, and restore it immediately on
  close so sticky elements do not flicker and result jumps work
  smoothly.
@Kepler2024
Kepler2024 merged commit 7a29fe1 into main Nov 28, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant