概览
本博客基于 Hugo 静态站点生成器 + Stack 主题构建,托管于 GitHub Pages。在主题基础上扩展了以下功能:
| 功能 | 实现方式 | 核心文件 |
|---|---|---|
| 音乐播放器 | APlayer + Hugo Data 模板 | layouts/_partials/footer/custom.html |
| 无刷新导航 | 自实现 PJAX (fetch + DOMParser) | layouts/_partials/footer/custom.html |
| 双重歌词 | 自定义 LRC 解析 + CSS 毛玻璃 | layouts/_partials/footer/custom.html + assets/scss/custom.scss |
| 动态粒子背景 | particles.js + 半透明 body 层 | layouts/_partials/footer/custom.html + assets/background/particlesjs-config.json |
| 暗色模式 | Stack 主题原生 + PJAX 持久化 + autofill 修复 | layouts/_partials/footer/custom.html + assets/scss/custom.scss |
| 语言切换 | i18n 下拉菜单 + Hugo 多语言 | layouts/_partials/footer/custom.html + assets/scss/custom.scss |
| 搜索框暗色适配 | CSS autofill 防护 | assets/scss/custom.scss |
| 归档双列布局 | 自定义模板 + CSS Grid | layouts/archives.html + assets/scss/custom.scss |
所有功能的核心代码集中在 layouts/_partials/footer/custom.html(约 560 行)和 assets/scss/custom.scss(约 1050 行)两个文件中,没有引入任何第三方 JS 框架。
一、音乐播放器
1.1 架构设计
1data/music.toml ──→ Hugo 模板渲染 ──→ APlayer JS 初始化 ──→ DOM
2 │ │
3 歌曲元数据 自定义歌词系统
4 (URL/封面/LRC) (LRC fetch → parseLRC → tick loop)
三层分离:
- 数据层
data/music.toml:纯数据,修改歌曲不需要碰代码 - 模板层
_partials/footer/custom.html:Hugo 构建时将 TOML 数据注入 JS 变量 - 交互层 APlayer JS API + 自实现 LRC 系统
1.2 歌曲配置(data/music.toml)
每首歌一个 [[music]] 块,支持 5 个字段:
1[[music]]
2name = "K歌之王" # 必填:歌曲名
3artist = "陈奕迅" # 必填:艺术家
4url = "https://oss.example.com/xxx.mp3" # 必填:音频 URL
5cover = "/img/K歌之王.jpg" # 可选:封面图
6lrc = "/music/K歌之王.lrc" # 可选:LRC 歌词文件路径
数据结构被 Hugo 的 jsonify 过滤器序列化后注入 JS:
1// 模板中的注入代码
2var musicList = {{ $songs | jsonify | safeJS }};
注入后浏览器端收到的 JS 代码:
1var musicList = [
2 {"name":"K歌之王","artist":"陈奕迅","url":"https://...","cover":"/img/...","lrc":"/music/..."},
3 // ...更多歌曲
4];
1.3 LRC 歌词系统实现
APlayer 默认的歌词功能有局限:无 LRC 的歌曲显示 “Not available” 文字,且样式不可控。因此项目自建了一套双行歌词系统。
LRC 解析算法
1function parseLRC(lrcText) {
2 var lines = lrcText.split('\n');
3 var result = [];
4 var seenTimes = {}; // 同时间戳去重
5 var tagRe = /\[(\d{2}):(\d{2})(?:[.:](\d{2,3}))?\]/;
6
7 for (var i = 0; i < lines.length; i++) {
8 var match = tagRe.exec(lines[i]);
9 if (!match) continue;
10
11 var min = parseInt(match[1], 10);
12 var sec = parseInt(match[2], 10);
13 var ms = match[3] ? parseInt(match[3], 10) : 0;
14 if (match[3] && match[3].length === 2) ms = ms * 10; // 兼容 [00:12.34] 和 [00:12.345]
15
16 var time = min * 60 + sec + ms / 1000;
17 var text = lines[i].replace(tagRe, '').replace(/^\s+/, '').trim();
18
19 if (text.length > 0 && !seenTimes[time.toFixed(3)]) {
20 seenTimes[time.toFixed(3)] = true;
21 result.push({time: time, text: text});
22 }
23 }
24 result.sort(function(a, b) { return a.time - b.time; });
25 return result;
26}
关键细节:
- 时间戳精确到毫秒(
toFixed(3)),避免浮点误差 seenTimes去重:同一时间戳多行(原歌词 + 拼音/翻译)时保留第一行- 兼容
[00:12.34]和[00:12.345]两种 LRC 格式
实时歌词匹配(findCurrentAndNext)
1function findCurrentAndNext(lyrics, currentTime) {
2 if (!lyrics || lyrics.length === 0) return { current: null, next: null };
3 var idx = 0;
4 for (var i = 0; i < lyrics.length; i++) {
5 if (currentTime >= lyrics[i].time) idx = i;
6 else break;
7 }
8 var next = (idx + 1 < lyrics.length) ? lyrics[idx + 1] : null;
9 return { current: lyrics[idx], next: next };
10}
线性扫描 O(n),但因 LRC 文件通常只有几十行,性能足够。
渲染循环(requestAnimationFrame)
1function tick() {
2 try {
3 if (ap.audio && !ap.audio.paused && !lrcManuallyHidden) {
4 var songName = getCurrentSongName();
5 if (songName && lrcData[songName] && lrcData[songName].length > 0) {
6 if (currentLrc !== lrcData[songName]) {
7 currentLrc = lrcData[songName]; // 自愈:自动关联当前歌曲
8 }
9 var lines = findCurrentAndNext(currentLrc, ap.audio.currentTime);
10 if (lines.current && lines.current.text !== lastCurrentText) {
11 lastCurrentText = lines.current.text;
12 lrcLineEl.textContent = lines.current.text;
13 flashFade(lrcLineEl);
14 }
15 var nextText = lines.next ? lines.next.text : '';
16 if (nextText !== lastNextText) {
17 lastNextText = nextText;
18 lrcNextEl.textContent = nextText;
19 if (nextText) flashFade(lrcNextEl);
20 }
21 }
22 }
23 } catch(e) {}
24 requestAnimationFrame(tick);
25}
26requestAnimationFrame(tick);
每帧检查歌词变化,文本切换时触发 CSS flashFade 动画(opacity: 0.4 → 1 过渡)。
APlayer API 注意事项
易错点:ap.audio 是 HTML5 <audio> 元素,只有 src/currentTime/paused 等原生属性。歌曲元数据(name/artist/cover)存储在 ap.list.audios[ap.list.index] 中。
1// ✅ 正确:从播放列表获取歌曲名
2function getCurrentSongName() {
3 try {
4 return ap.list.audios[ap.list.index].name;
5 } catch(e) { return null; }
6}
7
8// ❌ 错误:ap.audio 没有 name 属性
9function getCurrentSongName() {
10 return ap.audio.name; // undefined!
11}
1.4 播放状态持久化
localStorage 键名 aplayer_state_v2,保存/恢复当前歌曲索引、播放进度、播放状态:
1function saveState() {
2 var state = {
3 index: ap.list.index,
4 currentTime: ap.audio.currentTime,
5 isPlaying: !ap.audio.paused
6 };
7 localStorage.setItem('aplayer_state_v2', JSON.stringify(state));
8}
恢复时用 ap.list.switch(saved.index) 切到上次歌曲,通过 loadedmetadata + canplay 双事件 + 1.5s 兜底定时器确保进度恢复。
二、PJAX 无刷新导航
2.1 原理
不依赖任何 PJAX 库,纯 fetch + DOMParser 实现:
1用户点击内部链接 → e.preventDefault()
2 ↓
3fetch(url) → response.text()
4 ↓
5new DOMParser().parseFromString(html, 'text/html')
6 ↓
7提取 doc.querySelector('main.main')
8提取 doc.querySelector('aside.right-sidebar')
9 ↓
10替换 DOM 节点 → 同步 body.className / document.title
11 ↓
12history.pushState(url)
13 ↓
14window.Stack.init() 重新绑定主题功能
2.2 为什么不用 innerHTML 替换整个页面
只替换 main.main 和 aside.right-sidebar 两个区域,这样:
- 播放器
<div id="aplayer">和歌词<div id="custom-lrc">保持不变(在 main 外部) - particles.js 画布
<div id="particles-js">保持不变 - 所有事件监听器 保持在同一个 JS 运行环境中
2.3 暗色模式持久化
关键时序问题:window.Stack.init() 会读取 localStorage 中的 StackColorScheme 并设置 data-scheme,可能覆盖用户当前选择。因此 PJAX 必须在 Stack.init() 之后立即恢复:
1var savedScheme = document.documentElement.getAttribute('data-scheme');
2setTimeout(function() {
3 window.Stack.init();
4 if (savedScheme) {
5 document.documentElement.setAttribute('data-scheme', savedScheme);
6 }
7}, 100);
setTimeout(100ms) 是因为 Stack 主题初始化需要等待 DOM 更新完成。
2.4 排除规则
1function shouldIntercept(link) {
2 if (!link.getAttribute('href').startsWith('/')) return false; // 外链
3 if (link.closest('.aplayer')) return false; // 播放器控件
4 if (link.closest('#i18n-switch')) return false; // 语言切换
5 if (link.target || link.hasAttribute('download')) return false; // 新窗口/下载
6 return true;
7}
三、动态粒子背景
3.1 配置方式
assets/background/particlesjs-config.json 是 particles.js (Vincent Garreau, 2017) 的标准配置。Hugo 构建时 JSON 和 JS 库一同输出到 public/background/。
1{
2 "particles": {
3 "number": { "value": 80, "density": { "enable": true, "value_area": 800 } },
4 "color": { "value": "#f4d2e2" },
5 "opacity": { "value": 1, "random": false },
6 "size": { "value": 4.5, "random": true },
7 "line_linked": { "enable": true, "distance": 150, "color": "#f4d2e2", "opacity": 0.72, "width": 1 },
8 "move": { "enable": true, "speed": 1.2, "direction": "none", "out_mode": "out" }
9 },
10 "interactivity": {
11 "detect_on": "window",
12 "events": { "onhover": { "enable": true, "mode": "grab" }, "onclick": { "enable": true, "mode": "repulse" } },
13 "modes": { "grab": { "distance": 250, "line_linked": { "opacity": 1 } }, "repulse": { "distance": 285 } }
14 },
15 "retina_detect": true
16}
修改方法:访问 vincentgarreau.com/particles.js 生成 JSON → 覆盖 assets/background/particlesjs-config.json → hugo 重新构建。
3.2 CSS 层叠上下文挑战
这是整个项目中最棘手的 CSS 问题。粒子画布(<canvas>)需要:
- 在页面内容下面(z-index 低)
- 在 body 背景上面(否则被 body 的不透明背景挡住)
解决方案:不让 body 完全不透明,而是用 85% 不透明度的半透明背景:
1body {
2 background: rgba(245, 245, 250, 0.85) !important;
3}
4[data-scheme="dark"] body {
5 background: rgba(48, 48, 48, 0.85) !important;
6}
7#particles-js {
8 position: fixed;
9 z-index: -1; /* 在 body 背景下方,通过 body 半透明可见 */
10 pointer-events: none;
11}
rgba(245,245,250,0.85) 代表浅色模式下 15% 的透明度让粒子透出;卡片(article)有 100% 不透明背景色,内容完全不受影响。
3.3 加载时序
particles.js 在 <script src="..."> 同步加载后立即调用 particlesJS.load(),不等待 DOMContentLoaded。
1<div id="particles-js"></div>
2<script src="/background/particles.min.js"></script>
3<script>
4 particlesJS.load('particles-js', '/background/particlesjs-config.json', function() {
5 console.log('particles.js loaded');
6 });
7</script>
四、暗色模式
4.1 CSS 变量机制
Stack 主题在 <html> 上设置 data-scheme 属性,通过 CSS 属性选择器切换变量:
1:root {
2 --body-background: #f5f5fa; /* 浅色 */
3 --card-background: #fff;
4 --body-text-color: #707070;
5}
6:root[data-scheme="dark"] {
7 --body-background: #303030; /* 深色 */
8 --card-background: #424242;
9 --body-text-color: rgba(255,255,255,0.7);
10}
所有 UI 组件使用 var(--body-background) 等变量,自动适配明暗切换。
4.2 搜索框 Chrome autofill 修复
Chrome 在识别到 <input name="keyword"> 后会注入 autofill 样式,覆盖自定义背景色。解决方案:
1input[name="keyword"] {
2 -webkit-text-fill-color: var(--card-text-color-main);
3
4 &:-webkit-autofill,
5 &:-webkit-autofill:hover,
6 &:-webkit-autofill:focus,
7 &:-webkit-autofill:active {
8 -webkit-text-fill-color: var(--card-text-color-main) !important;
9 -webkit-box-shadow: 0 0 0 1000px var(--card-background) inset !important;
10 transition: background-color 5000s ease-in-out 0s;
11 }
12}
核心技巧:Chrome autofill 不认 background CSS 属性,但认 -webkit-box-shadow: inset。用 1000px 的内阴影模拟背景色填充整个输入框。transition: 5000s 阻止 autofill 的白色→主题色的过渡动画闪烁。
4.3 暗色模式按钮的 CSS 层级
1#dark-mode-toggle {
2 width: auto !important;
3 align-self: flex-start !important; // 阻止 flex 容器横向拉伸
4}
align-self: flex-start 是因为主题 CSS 中 #main-menu li { width: 100% } 会导致按钮占满整行。
五、i18n 语言切换
5.1 下拉菜单实现
1#i18n-switch {
2 position: relative;
3 z-index: 10; // 关键:创建独立层叠上下文,防止暗色模式按钮穿透
4
5 .i18n-menu {
6 position: absolute;
7 z-index: 100;
8 opacity: 0;
9 visibility: hidden;
10 transform: translateY(10px) scale(0.95);
11 transition: opacity 0.2s, visibility 0.2s, transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
12 }
13
14 .i18n-dropdown.open .i18n-menu {
15 opacity: 1;
16 visibility: visible;
17 transform: translateY(0) scale(1);
18 }
19}
动画使用 cubic-bezier(0.34, 1.56, 0.64, 1) 产生弹性回弹效果。opacity + visibility 双属性控制是为了让隐藏状态下完全不可交互。
5.2 JS 控制
1var dropdown = document.querySelector('.i18n-dropdown');
2var toggleBtn = dropdown.querySelector('[data-toggle]');
3toggleBtn.addEventListener('click', function(e) {
4 e.stopPropagation();
5 dropdown.classList.toggle('open');
6});
7// 点击外部或按 Escape 关闭
⚠️ 此处踩过一个坑:最初用
document.getElementById('i18n-dropdown')获取元素,但.i18n-dropdown是 class 不是 id,导致 JS 完全无效。改为querySelector('.i18n-dropdown')解决。
六、项目结构速查
1dev/
2├── assets/
3│ ├── background/
4│ │ ├── particles.min.js # particles.js 库 (23KB)
5│ │ └── particlesjs-config.json # 粒子配置 ← 修改粒子效果
6│ └── scss/
7│ └── custom.scss # 全部自定义样式 ← 修改 UI
8├── config/_default/
9│ ├── config.toml # Hugo 主配置
10│ └── languages.toml # 多语言配置
11├── content/post/ # 博客文章 (.md)
12├── data/
13│ └── music.toml # 歌曲列表 ← 加歌/改歌
14├── layouts/
15│ ├── _partials/
16│ │ ├── footer/custom.html # ← 核心文件 (560行)
17│ │ └── sidebar/left.html # 侧边栏覆盖
18│ ├── partials/
19│ │ ├── head/custom.html # APlayer CSS 引用
20│ │ └── head/custom-font.html # 系统字体栈 (替代 Google Fonts)
21│ ├── archives.html # 归档页 (双列 Grid)
22│ └── _default/terms.html # 分类页
23├── static/
24│ ├── lib/aplayer/ # APlayer v1.10.1 本地库
25│ │ ├── APlayer.min.css
26│ │ └── APlayer.min.js
27│ ├── img/ # 封面图等
28│ └── music/ # LRC 歌词文件
29├── themes/hugo-theme-stack/ # 主题 (git submodule)
30└── deploy.bat # 一键部署脚本
修改速查表
| 需求 | 文件 | 怎么改 |
|---|---|---|
| 加一首歌 | data/music.toml |
追加 [[music]] 块 |
| 改粒子颜色/密度 | assets/background/particlesjs-config.json |
改 JSON 字段 |
| 调播放器样式 | assets/scss/custom.scss |
APlayer 块 |
| 调歌词样式 | assets/scss/custom.scss |
#custom-lrc 块 |
| 改 PJAX 行为 | layouts/_partials/footer/custom.html |
PJAX 块 |
| 改粒子透明度 | layouts/_partials/footer/custom.html |
<style> 中 body background rgba |
| 加新侧边栏功能 | layouts/_partials/footer/custom.html |
追加 <script> |
| 更换背景类型 | layouts/_partials/footer/custom.html |
替换 particles.js 部分 |
| 改搜索框样式 | assets/scss/custom.scss |
.search-form 块 |
七、调试经验总结
- Hugo 模板路径:Stack 主题使用
_partials目录,自定义模板必须放在layouts/_partials/(带下划线),不是layouts/partials/ - Hugo 0.156+ 弃用:
site.Data替换为hugo.Data,避免构建警告 - APlayer API:
ap.audio是 HTML5 Audio 元素(有 currentTime/paused/volume),歌曲元数据在ap.list.audios[index](有 name/artist/cover/lrc) - CSS 层叠上下文:
z-index: -1的 fixed 元素在 body 背景下方,需要 body 半透明才能看见 - Chrome autofill:用
-webkit-box-shadow: inset替代background设置输入框背景色 - 所有功能集中在两个文件——便于维护,但修改时要小心不要破坏其他功能
本文由 Claude (Anthropic) 对博客项目进行全面审计与优化后撰写,记录项目的技术架构和实现细节。
2026 年 6 月 4 日