检索页滚动高亮改成只看标题:从上往下第一个没被顶栏挡住的条目标题就是正在读的,节标题算该节第 1 条;屏幕里没有标题时取标题已滚过顶栏的最后一条。原来按视口三成处的线判,上一条推上去一半还压着线,侧边栏不切。长文弹窗目录的判定线从顶部 90px 改成视口上部三成

This commit is contained in:
albert4719
2026-10-05 10:07:19 +08:00
parent dcccdd961f
commit bc149af3a0
+21 -7
View File
@@ -814,7 +814,7 @@ function spy(){
clearTimeout(spyT);
spyT = setTimeout(() => {
const line = innerHeight * 0.3; // 判定线取视口偏上三成,和人读的位置差不多
let hit = '', near = Infinity;
let hit = '';
// 还在页首读导读和筛选器时,没有「正在看的那一条」,地址栏就该是干净的。
// 否则停在最上面也会被记成 #e-1-1,一刷新就滚到第 1 节标题、导读全没了
// (2026-09-20 用户提的)。判据是第一个节块的顶还在判定线以下。
@@ -825,15 +825,28 @@ function spy(){
history.replaceState(history.state, '', location.pathname + location.search);
return;
}
// 看标题:从上往下第一个没被顶栏挡住的标题,就是正在读的那条。一屏只放得下一条半,
// 按「压着视口三成处那条线」「露出得多」「中线还在屏幕里」三种判法都试过,都有被用户
// 抓到的情况(2026-10-05),最后按用户的提议只看标题。节标题算作这一节第 1 条的标题,
// 不然读节首引言和导览时会挑到上一节最后一条。屏幕里一个标题都没有(一条比一屏还高),
// 就取标题已经滚到顶栏上面的最后一条,也就是正在读的这条。
const top = Math.max(0, document.querySelector('header.nav').getBoundingClientRect().bottom);
const heads = []; // [标题上沿, 落到哪条]
for (const b of BLOCKS){
const r = b.el.getBoundingClientRect();
if (!r.height) continue;
const first = CARDS.find(c => c.el.closest('.sec-block') === b.el && c.el.getBoundingClientRect().height);
if (first) heads.push([r.top, first.el.id]);
}
for (const c of CARDS){
// 不看 card.vis:那是 CARD_IO 维护的渲染标记,滞后于滚动。直接量一次位置,
// 高度为 0 的就是被筛选藏起来的,顺带一并跳过。
const r = c.el.getBoundingClientRect();
if (!r.height || r.bottom <= 0 || r.top >= innerHeight) continue;
if (r.top <= line && r.bottom > line){ hit = c.el.id; break; } // 跨过判定线的优先
const d = Math.abs(r.top - line);
if (d < near){ near = d; hit = c.el.id; }
if (!c.el.getBoundingClientRect().height) continue;
heads.push([c.el.querySelector('.card-h').getBoundingClientRect().top, c.el.id]);
}
heads.sort((x, y) => x[0] - y[0]);
hit = heads.find(([t]) => t >= top && t < innerHeight)?.[1]
|| heads.filter(([t]) => t < top).pop()?.[1] || '';
if (hit === spyLast) return;
spyLast = hit; tocSync(hit);
history.replaceState(history.state, '', location.pathname + location.search + (hit ? '#' + hit : ''));
@@ -1326,7 +1339,8 @@ function dmTopOf(el){
return el.getBoundingClientRect().top - DM.doc.getBoundingClientRect().top + DM.doc.scrollTop;
}
function dmSpy(){
const top = DM.doc.scrollTop + 90; let cur = DM.heads[0];
// 判定线和主页的 spy 一样取视口偏上三成。原来是顶部 90px,下一节已经占了大半屏,目录还亮着上一节
const top = DM.doc.scrollTop + Math.max(90, DM.doc.clientHeight * 0.3); let cur = DM.heads[0];
for (const h of DM.heads){ const el = document.getElementById(h.id); if (el && dmTopOf(el) <= top) cur = h; }
for (const a of DM.toc.children) a.classList.toggle('on', !!cur && a.dataset.id === cur.id);
}