Skip to content

Commit 999e809

Browse files
committed
Fix fixed header and sidebar layout
1 parent 0fed631 commit 999e809

3 files changed

Lines changed: 56 additions & 17 deletions

File tree

assets/site.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ const state = {
520520

521521
const els = {
522522
body: document.body,
523+
topbar: document.querySelector(".topbar"),
524+
sidebar: document.querySelector(".sidebar"),
523525
brandLine: document.querySelector("#brand-line"),
524526
plannerModeLabel: document.querySelector("#planner-mode-label"),
525527
scheduleModeLabel: document.querySelector("#schedule-mode-label"),
@@ -842,6 +844,8 @@ function setLanguageChrome(lang) {
842844
button.classList.toggle("is-active", isActive);
843845
button.setAttribute("aria-pressed", String(isActive));
844846
});
847+
848+
updateFixedChromeMetrics();
845849
}
846850

847851
function setMode(mode, shouldUpdateUrl = true) {
@@ -863,6 +867,23 @@ function setMode(mode, shouldUpdateUrl = true) {
863867
if (shouldUpdateUrl) updateUrl();
864868
updateReadingProgress();
865869
window.lucide?.createIcons();
870+
updateFixedChromeMetrics();
871+
}
872+
873+
function updateFixedChromeMetrics() {
874+
window.requestAnimationFrame(() => {
875+
const topbarHeight = Math.ceil(els.topbar?.getBoundingClientRect().height || 0);
876+
const sidebarHeight = window.matchMedia("(max-width: 920px)").matches
877+
? Math.ceil(els.sidebar?.getBoundingClientRect().height || 0)
878+
: 0;
879+
880+
if (topbarHeight > 0) {
881+
document.documentElement.style.setProperty("--topbar-offset", `${topbarHeight}px`);
882+
}
883+
if (sidebarHeight > 0) {
884+
document.documentElement.style.setProperty("--sidebar-offset", `${sidebarHeight}px`);
885+
}
886+
});
866887
}
867888

868889
function updateUrl() {
@@ -1747,7 +1768,16 @@ els.resetMatrix.addEventListener("click", resetMatrixProgress);
17471768
els.search.addEventListener("input", renderToc);
17481769
els.top.addEventListener("click", () => window.scrollTo({ top: 0, behavior: "smooth" }));
17491770
window.addEventListener("scroll", updateReadingProgress, { passive: true });
1750-
window.addEventListener("resize", updateReadingProgress);
1771+
window.addEventListener("resize", () => {
1772+
updateFixedChromeMetrics();
1773+
updateReadingProgress();
1774+
});
1775+
window.visualViewport?.addEventListener("resize", updateFixedChromeMetrics);
1776+
1777+
if (window.ResizeObserver) {
1778+
const fixedChromeObserver = new ResizeObserver(updateFixedChromeMetrics);
1779+
[els.topbar, els.sidebar].filter(Boolean).forEach((node) => fixedChromeObserver.observe(node));
1780+
}
17511781

17521782
state.lang = preferredLanguage();
17531783
state.mode = preferredMode();

assets/styles.css

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
--code-ink: #eef6f7;
2121
--shadow: 0 18px 46px rgb(29 37 40 / 10%);
2222
--sidebar: clamp(17rem, 24vw, 21rem);
23+
--topbar-offset: 4.4rem;
24+
--sidebar-offset: 0px;
2325
--content: 66rem;
2426
--reader-content: 55rem;
2527
--radius: 8px;
@@ -38,6 +40,7 @@ html {
3840
body {
3941
margin: 0;
4042
min-width: 320px;
43+
padding-top: var(--topbar-offset);
4144
overflow-x: hidden;
4245
background:
4346
linear-gradient(180deg, rgb(15 118 110 / 8%), transparent 28rem),
@@ -85,9 +88,9 @@ button:disabled {
8588
}
8689

8790
.topbar {
88-
position: sticky;
89-
top: 0;
90-
z-index: 30;
91+
position: fixed;
92+
inset: 0 0 auto;
93+
z-index: 40;
9194
display: flex;
9295
align-items: center;
9396
justify-content: space-between;
@@ -247,16 +250,17 @@ button:disabled {
247250
.app-shell {
248251
display: grid;
249252
grid-template-columns: var(--sidebar) minmax(0, 1fr);
250-
min-height: calc(100vh - 4.4rem);
253+
min-height: calc(100dvh - var(--topbar-offset));
251254
}
252255

253256
.sidebar {
254257
position: fixed;
255-
top: 4.4rem;
258+
top: var(--topbar-offset);
256259
bottom: 0;
257260
left: 0;
258261
z-index: 20;
259262
width: var(--sidebar);
263+
height: calc(100dvh - var(--topbar-offset));
260264
overflow: hidden;
261265
border-right: 1px solid var(--line);
262266
background: rgb(255 255 255 / 72%);
@@ -1059,7 +1063,7 @@ button:disabled {
10591063
}
10601064

10611065
.prose :target {
1062-
scroll-margin-top: 5.4rem;
1066+
scroll-margin-top: calc(var(--topbar-offset) + var(--sidebar-offset) + 1rem);
10631067
}
10641068

10651069
.prose.is-chapter-focused h2.is-focused-chapter-start {
@@ -1231,6 +1235,10 @@ button:disabled {
12311235
}
12321236

12331237
@media (max-width: 1040px) {
1238+
:root {
1239+
--topbar-offset: 7.25rem;
1240+
}
1241+
12341242
.topbar {
12351243
align-items: flex-start;
12361244
flex-wrap: wrap;
@@ -1257,14 +1265,15 @@ button:disabled {
12571265

12581266
.sidebar {
12591267
position: fixed;
1260-
top: 4.4rem;
1268+
top: var(--topbar-offset);
12611269
right: 0;
12621270
bottom: auto;
12631271
left: 0;
12641272
z-index: 20;
12651273
width: auto;
12661274
height: auto;
1267-
overflow: visible;
1275+
max-height: calc(100dvh - var(--topbar-offset));
1276+
overflow: hidden;
12681277
border-right: 0;
12691278
border-bottom: 1px solid var(--line);
12701279
background: rgb(246 250 248 / 95%);
@@ -1341,7 +1350,7 @@ button:disabled {
13411350

13421351
.workspace {
13431352
grid-column: auto;
1344-
padding-top: 6.9rem;
1353+
padding-top: calc(var(--sidebar-offset) + 1rem);
13451354
}
13461355
}
13471356

@@ -1357,6 +1366,10 @@ button:disabled {
13571366
}
13581367

13591368
@media (max-width: 640px) {
1369+
:root {
1370+
--topbar-offset: 7.7rem;
1371+
}
1372+
13601373
.topbar {
13611374
min-height: 6rem;
13621375
padding: 0.7rem 0.85rem;
@@ -1396,12 +1409,8 @@ button:disabled {
13961409
height: 2.2rem;
13971410
}
13981411

1399-
.sidebar {
1400-
top: 6rem;
1401-
}
1402-
14031412
.workspace {
1404-
padding-top: 7.1rem;
1413+
padding-top: calc(var(--sidebar-offset) + 0.9rem);
14051414
}
14061415

14071416
.learning-hero h1,

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
<meta name="twitter:card" content="summary" />
2020
<link rel="canonical" href="https://lling0000.github.io/Vibe_coding_guide/" />
2121
<title>Vibe Coding Guide | AI Coding 工程工作流手册</title>
22-
<link rel="stylesheet" href="./assets/styles.css?v=20260617-sidebar-review" />
22+
<link rel="stylesheet" href="./assets/styles.css?v=20260617-fixed-chrome" />
2323
<script defer src="https://cdn.jsdelivr.net/npm/dompurify@3.2.6/dist/purify.min.js"></script>
2424
<script defer src="https://cdn.jsdelivr.net/npm/marked@15.0.12/marked.min.js"></script>
2525
<script defer src="https://cdn.jsdelivr.net/npm/mermaid@11.4.1/dist/mermaid.min.js"></script>
2626
<script defer src="https://cdn.jsdelivr.net/npm/lucide@0.475.0/dist/umd/lucide.min.js"></script>
27-
<script defer src="./assets/site.js?v=20260617-sidebar-review"></script>
27+
<script defer src="./assets/site.js?v=20260617-fixed-chrome"></script>
2828
</head>
2929
<body data-mode="planner">
3030
<div class="site-progress" aria-hidden="true">

0 commit comments

Comments
 (0)