From dca5b3cab0d8487d2e2b9629ddce1644715ac2c0 Mon Sep 17 00:00:00 2001 From: albert4719 Date: Sun, 20 Sep 2026 20:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E7=B4=A2=E9=A1=B5=EF=BC=9A#e-N-1=20?= =?UTF-8?q?=E8=90=BD=E5=9C=B0=E6=97=B6=E6=8A=8A=E8=8A=82=E6=A0=87=E9=A2=98?= =?UTF-8?q?=E4=B8=80=E8=B5=B7=E5=B8=A6=E8=BF=9B=E8=A7=86=E9=87=8E=EF=BC=8C?= =?UTF-8?q?=E7=82=B9=E5=92=8C=E5=88=B7=E6=96=B0=E7=9C=8B=E5=88=B0=E5=90=8C?= =?UTF-8?q?=E4=B8=80=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 点「见第 13 节」跳过去看得见节标题,但地址栏写的是 #e-13-1,刷新或者把 链接发给别人时走的是另一条路径(直接滚那张卡片),标题在视口上面,画面 从第一条开始。同一个 hash 给了两种画面。 - 新增 scrollAnchor():目标是某节的第一条时,滚它所在的节块、顶部对齐 - 跳转、刷新、分享三条路径都过这个函数,「第 13 节」和「第 13 节第 1 条」 两种指路也落在同一屏(闪的仍分别是节标题和那张卡片) - scrollToEl 加 renderEl:滚节块时预渲染的是节里第一张卡片,不是节块本身 - 无头实测:点击与带 #e-13-1 打开,节标题都在距顶 72px;中间条目仍居中 --- index.html | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 8a45eeb..3d4843c 100644 --- a/index.html +++ b/index.html @@ -744,9 +744,9 @@ addEventListener('scroll', spy, { passive: true }); // 在节当中,跳过去等于没跳(2026-09-20 用户点第 13 节撞到的)。顶部对齐靠 html 上 // 那条 scroll-padding-top 让开吸顶导航。flash 也要分开:.flash 的样式只挂在 .card // 上,整块 section 加了不显示,闪的是节标题那一行。 -function scrollToEl(el, { align = 'center', flashEl = el, spyId = el.id } = {}){ +function scrollToEl(el, { align = 'center', flashEl = el, spyId = el.id, renderEl = el } = {}){ spyOff = true; // 校正期间别让 spy 把地址栏改成沿途的条目 - const card = CARD_OF.get(el); + const card = CARD_OF.get(renderEl); // 滚到节块时要预渲染的是节里第一张卡片 if (card && card.key !== CUR_KEY) renderCard(card, CUR_TERMS, CUR_KEY); let tries = 0, lastY = NaN; const settle = () => { @@ -758,6 +758,13 @@ function scrollToEl(el, { align = 'center', flashEl = el, spyId = el.id } = {}){ }; settle(); } +// 一节的第一条,滚的时候连它上面的节标题一起带进视野:整节指路落地就是这个位置, +// 刷新、把链接发给别人也该看到同一屏。不然地址栏一样、画面不一样——点过去看得见 +// 「13. 紧急情况」,刷新之后标题在视口上面、直接从第一条开始(2026-09-20 用户提的)。 +function scrollAnchor(el){ + const block = el.closest?.('.sec-block'); + return block && el.id === `e-${block.dataset.sec}-1` ? block : el; +} function gotoItem(key, fromId){ const sec = key[0] === 's' ? key.slice(1) : ''; const id = sec ? 'sec-' + sec : `e-${key.split('-')[0]}-${key.split('-')[1]}`; @@ -776,7 +783,14 @@ function gotoItem(key, fromId){ // 形状——分享出去的链接也就和页面自己产生的链接对得上了。 const first = sec ? document.getElementById(`e-${sec}-1`) : null; history.pushState(null, '', '#' + (first?.id || id)); - scrollToEl(el, sec ? { align: 'start', flashEl: el.querySelector('.sec-h') || el, spyId: first?.id || id } : {}); + if (sec){ + scrollToEl(el, { align: 'start', flashEl: el.querySelector('.sec-h') || el, spyId: first?.id || id, renderEl: first || el }); + return; + } + // 条目一律居中看得清上下文,只有一节的第一条例外:滚它所在的节块、顶部对齐, + // 这样「第 13 节」和「第 13 节第 1 条」两种指路落在同一屏,闪的还是那张卡片。 + const anchor = scrollAnchor(el); + scrollToEl(anchor, anchor === el ? {} : { align: 'start', flashEl: el, spyId: el.id, renderEl: el }); } addEventListener('popstate', ev => { POP.hidden = true; @@ -1052,9 +1066,12 @@ async function readText(path){ state.dispute = false; state.todo = false; syncControls(); apply(); writeUrl(); } - target?.scrollIntoView(); + // 落到一节的第一条时滚它所在的节块,节标题跟着进视野——和点「见第 13 节」 + // 跳过去看到的是同一屏,地址栏也是同一个 #e-13-1 + const land = target ? scrollAnchor(target) : null; + land?.scrollIntoView(); // 卡片是 content-visibility:auto,首次定位后高度才准,下一帧再对一次 - if (target) requestAnimationFrame(() => target.scrollIntoView()); + if (land) requestAnimationFrame(() => land.scrollIntoView()); } } catch (err) { status.innerHTML = err.message === 'file'