// 边注定位:宽屏时把 .post-note 放进正文两侧的空白处(不改变正文布局), // 空白不够或窄屏时交还给 CSS(虚线框内嵌)。 // // 三种模式: // 1. 两侧留白都够宽 → 左右交替 // 2. 只有左侧够宽 → 全部放左侧(复用空着的侧栏占位区,不动正文) // 3. 都不够宽 / 窄屏 → 不收边注,CSS 虚线框内嵌 (function () { var MIN_W = 160; // 低于这个宽度就不做边注 var MAX_W = 288; // 注释最大宽度(18rem) var GAP = 16; // 注释与正文之间的间距 var EDGE = 8; // 注释与视口边缘的最小距离 var MIN_STACK = 12; // 同一侧两条注释之间的最小垂直间距 function reset(notes) { document.getElementById("content").classList.remove("notes-active"); notes.forEach(function (note) { note.classList.remove("placed"); note.removeAttribute("data-side"); note.style.top = ""; note.style.left = ""; note.style.width = ""; }); } function layout() { var main = document.getElementById("content"); if (!main) return; var notes = Array.prototype.slice.call( main.querySelectorAll(".post-note"), ); if (!notes.length) return; var container = document.querySelector(".hextra-max-page-width") || main; var cr = container.getBoundingClientRect(); var vw = document.documentElement.clientWidth; // 左侧可用空白 = 正文左边到视口左边(含空着的侧栏占位,它在本页没有内容) var leftAvail = main.getBoundingClientRect().left; // 右侧可用空白 = 视口右边到内容容器右边(TOC 之后) var rightAvail = vw - cr.right; var bothW = Math.min(MAX_W, Math.min(leftAvail - GAP - EDGE, rightAvail - GAP - EDGE)); var leftW = Math.min(MAX_W, leftAvail - GAP - EDGE); var mode = bothW >= MIN_W ? "both" : leftW >= MIN_W ? "left" : "none"; if (mode === "none") { reset(notes); return; } main.classList.add("notes-active"); var mr = main.getBoundingClientRect(); var width = mode === "both" ? bothW : leftW; var leftX = leftAvail - GAP - width; var rightX = cr.right + GAP; var sideBottom = { left: -Infinity, right: -Infinity }; notes.forEach(function (note, i) { var mark = note.previousElementSibling; if (!mark || !mark.classList.contains("post-note-mark")) { mark = note; } var side = mode === "both" && i % 2 === 1 ? "left" : "right"; if (mode === "left") side = "left"; note.setAttribute("data-side", side); note.classList.add("placed"); note.style.width = width + "px"; var height = note.offsetHeight; var anchorTop = mark.getBoundingClientRect().top - mr.top; var top = Math.max(0, anchorTop - 6); if (top < sideBottom[side] + MIN_STACK) { top = sideBottom[side] + MIN_STACK; } note.style.top = top + "px"; note.style.left = (side === "right" ? rightX : leftX) - mr.left + "px"; sideBottom[side] = top + height; }); } var timer; function schedule() { clearTimeout(timer); timer = setTimeout(layout, 100); } document.addEventListener("DOMContentLoaded", layout); window.addEventListener("load", layout); window.addEventListener("resize", schedule); if (document.fonts && document.fonts.ready) { document.fonts.ready.then(layout); } if (window.ResizeObserver) { document.addEventListener("DOMContentLoaded", function () { var content = document.querySelector("#content .content"); if (content) new ResizeObserver(schedule).observe(content); }); } })();