十月主要围绕博客的视觉效果和交互体验做了四项更新:自定义加载动画、搭建随笔页、接入 Giscus 评论区,以及添加网易云音乐播放器,并启用了 PJAX。

一、加载动画

1. 准备背景图和字体

先准备一张横向背景图,压缩后保存为:

1
source/image/entry-bg.png

字体可以到 Google Fonts 搜索。我在加载标语和首页英文标题中使用的是 Sora:

  1. 打开 Sora 页面预览不同字重;
  2. 只选择实际会用到的 400、500、600,避免加载过多字体文件;
  3. 点击 Get font,再进入 Get embed code;
  4. 复制 Web 中的 <link> 代码。

把字体链接写入 _config.butterfly.yml 的 inject.head。preconnect 可以提前建立连接,display=swap 会先显示备用字体,字体下载完成后再替换:

1
2
3
4
5
inject:
head:
- '<link rel="preconnect" href="https://fonts.googleapis.com">'
- '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>'
- '<link href="https://fonts.googleapis.com/css2?family=Sora:wght@400;500;600&display=swap" rel="stylesheet">'

Sora 主要用于英文内容,中文仍需要准备系统字体作为回退:

1
2
3
[data-gsap-effect="textFallDown"] {
font-family: "Sora", "Microsoft YaHei", "PingFang SC", sans-serif;
}

如果访问 Google Fonts 较慢,也可以下载字体后自行托管,但需要额外编写 @font-face。这次只在少量标题上使用,所以直接采用在线 CSS。

2. 开启 Preloader、PJAX 并注入文件

在 _config.butterfly.yml 中继续加入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
preloader:
enable: true
source: 1

pjax:
enable: true

inject:
head:
- '<link rel="preload" as="image" href="/image/entry-bg.png">'
- '<link rel="stylesheet" href="/css/entry-loading.css?v=8">'
bottom:
- '<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.13.0/gsap.min.js"></script>'
- '<script src="/js/fall-down.js?v=8"></script>'

这里使用 Butterfly 自带的 #loading-box,不再创建第二套全屏遮罩。GSAP 必须放在 fall-down.js 前面,否则执行动画时会出现 gsap is not defined。文件更新后可以递增 ?v=8 中的数字,让浏览器重新拉取资源。

3. 创建文字动画

在 source/js/fall-down.js 中找到主题加载层,把默认内容替换成自己的动画舞台:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const TEXT = "nothing but yourself";

function createEffectContainer() {
const loadingBox = document.querySelector("#loading-box");
const spinnerBox = loadingBox?.querySelector(".spinner-box");

if (!loadingBox || !spinnerBox) return null;

spinnerBox.innerHTML =
'<div data-gsap-effect-stage="textFallDown">' +
'<div data-gsap-effect="textFallDown"></div>' +
'</div>';

return spinnerBox.querySelector('[data-gsap-effect="textFallDown"]');
}

然后将句子按单词和字母拆开。单词使用独立容器,可以避免英文单词从中间断行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function setup(element) {
const words = TEXT.split(" ").map(word => {
const chars = [...word]
.map(char => `<span class="textFallDown-char">${char}</span>`)
.join("");

return `<span class="textFallDown-word">${chars}</span>`;
}).join('<span class="textFallDown-space"></span>');

element.innerHTML = `<div><div>${words}</div></div>`;
}

function play(element) {
const targets = element.querySelectorAll(".textFallDown-char");
if (!targets.length || typeof gsap === "undefined") return;

gsap.set(targets, { y: -50, opacity: 0 });
gsap.to(targets, {
y: 0,
opacity: 1,
duration: 0.5,
stagger: 0.05,
ease: "power2.out",
repeat: 0
});
}

首次进入使用图片背景和白字,站内切页则改成白底黑字。在 source/css/entry-loading.css 中写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#loading-box {
position: fixed;
inset: 0;
z-index: 1000;
background: #8d96a6 url("/image/entry-bg.png") center / cover no-repeat;
transition: opacity 0.6s ease 2s, visibility 0s linear 2.6s;
}

#loading-box.pjax-transition {
background: #fff !important;
transition: opacity 0.6s ease, visibility 0s linear 0.6s;
}

#loading-box.pjax-transition [data-gsap-effect="textFallDown"] {
color: #000;
}

.textFallDown-word,
.textFallDown-char {
display: inline-block;
}

#loading-box.loaded {
opacity: 0;
visibility: hidden;
pointer-events: none;
}

4. 修复 PJAX 与浏览器回退

只监听首次加载时,站内跳转不会重新播放动画。另一方面,如果点击链接后显示了遮罩,浏览器通过 BFCache 回退时可能恢复离开前的 DOM,导致页面一直被遮挡。

因此要同时处理 pjax:send、pjax:complete 和 pageshow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let transitionActive = false;
let transitionTimer;

function startPageTransition() {
if (transitionActive) return;
transitionActive = true;

const loadingBox = document.querySelector("#loading-box");
loadingBox?.classList.remove("loaded");
loadingBox?.classList.add("pjax-transition");
loadingBox?.style.setProperty("opacity", "1");
loadingBox?.style.setProperty("visibility", "visible");
}

function endPageTransition() {
clearTimeout(transitionTimer);
transitionTimer = setTimeout(() => {
const loadingBox = document.querySelector("#loading-box");
loadingBox?.classList.add("loaded");
document.body.style.overflow = "";
transitionActive = false;
}, 2600);
}

document.addEventListener("pjax:send", startPageTransition);
document.addEventListener("pjax:complete", endPageTransition);

window.addEventListener("pageshow", event => {
if (!event.persisted) return;

clearTimeout(transitionTimer);
const loadingBox = document.querySelector("#loading-box");
loadingBox?.classList.remove("pjax-transition");
loadingBox?.classList.add("loaded");
document.body.style.overflow = "";
transitionActive = false;
});

最后增加 prefers-reduced-motion 判断。用户关闭动画时直接显示完整文字,不再强制播放逐字效果。

二、随笔页搭建

1. 增加菜单和列表页

先在 _config.butterfly.yml 的菜单中增加随笔入口:

1
2
3
4
5
menu:
主页: / || fas fa-home
生活 || fas fa-list:
随笔: /suibi/ || fa fa-feather-alt
音乐: /music/ || fa fa-music

然后创建 source/suibi/index.md:

1
2
3
4
5
6
7
8
---
title: 随笔
date: 2026-10-05 21:00:00
type: page
comments: false
aside: false
top_img: false
---

这里关闭侧栏和评论,让卡片有更大的横向空间。

2. 为每篇随笔建立独立文件

随笔不放在 _posts 中,而是统一保存到 source/essays。例如新建 source/essays/雨落竹帘.md:

1
2
3
4
5
6
7
8
9
10
11
12
---
title: 雨落竹帘
date: 2024-07-30 01:07:00
layout: page
type: essay
aside: false
comments: false
permalink: /essays/yuluozhulian/
cover: /image/yuluozhulian.jpg
top_img: /image/yuluozhulian.jpg
description: 雨落竹帘 桃源小亭 素笔洒墨
---

permalink 建议只使用英文、数字和短横线。这样分享链接更稳定,也能避免部分环境对中文路径编码不一致。封面图放入 source/image,在 Front Matter 中统一使用站点绝对路径。

3. 添加卡片结构

回到 source/suibi/index.md,在 Front Matter 后加入卡片。新增随笔时,复制一张卡片并替换链接、图片、日期和摘要即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div class="essay-carousel" data-essay-carousel>
<button class="essay-carousel-button-prev" aria-label="上一张随笔">‹</button>
<div class="essay-grid" tabindex="0">
<a class="essay-card" href="/essays/yuluozhulian/">
<div class="essay-card-cover">
<img src="/image/yuluozhulian.jpg" alt="雨落竹帘" loading="lazy">
<time datetime="2024-07-30">2024.07.30</time>
</div>
<div class="essay-card-body">
<h3>雨落竹帘</h3>
<p class="essay-card-excerpt">雨落竹帘,桃源小亭,素笔洒墨……</p>
</div>
</a>
</div>
<button class="essay-carousel-button-next" aria-label="下一张随笔">›</button>
</div>

4. 实现横向滑动

在 source/css/essays.css 中使用 Flex 和 Scroll Snap。桌面端每屏显示两张卡片,移动端显示一张:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.essay-grid {
display: flex;
gap: 24px;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
scrollbar-width: none;
}

.essay-card {
flex: 0 0 calc((100% - 24px) / 2);
min-width: 300px;
overflow: hidden;
border-radius: 18px;
scroll-snap-align: start;
}

@media (max-width: 768px) {
.essay-card {
flex-basis: 100%;
min-width: 100%;
}
}

在 source/js/essays.js 中让按钮每次移动一张卡片,并在 PJAX 切页后重新初始化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function setupCarousel(carousel) {
if (carousel.dataset.essayReady === "1") return;

const track = carousel.querySelector(".essay-grid");
const card = track.querySelector(".essay-card");
const gap = parseFloat(getComputedStyle(track).gap || "0");
const step = () => card.getBoundingClientRect().width + gap;

carousel.querySelector(".essay-carousel-button-prev")
.addEventListener("click", () =>
track.scrollBy({ left: -step(), behavior: "smooth" }));

carousel.querySelector(".essay-carousel-button-next")
.addEventListener("click", () =>
track.scrollBy({ left: step(), behavior: "smooth" }));

carousel.dataset.essayReady = "1";
}

const initEssays = () =>
document.querySelectorAll("[data-essay-carousel]").forEach(setupCarousel);

document.addEventListener("DOMContentLoaded", initEssays);
document.addEventListener("pjax:complete", initEssays);

最后将文件加入主题配置:

1
2
3
4
5
inject:
head:
- '<link rel="stylesheet" href="/css/essays.css?v=15">'
bottom:
- '<script src="/js/essays.js?v=3"></script>'

实际使用中还需要区分“拖动”和“点击”:触摸拖动距离超过约 12px 后,拦截松手产生的那次点击,避免用户滑动卡片时误入文章;鼠标点击卡片则保留原生链接行为。

三、评论区

1. 在 GitHub 开启 Discussions

Giscus 把评论保存到 GitHub Discussions,不需要单独维护数据库。开始前需要完成以下操作:

  1. 评论所用的 GitHub 仓库必须是公开仓库;
  2. 进入仓库的 Settings → General → Features,勾选 Discussions;
  3. 打开 giscus.app 中文配置页;
  4. 按页面提示安装 Giscus App,并授权它访问评论仓库;
  5. 在“仓库”输入框填写 用户名/仓库名,例如 Lamb-fish/Lamb-fish.github.io。

2. 在 Giscus 页面生成参数

仓库检查通过后,继续选择:

  • 页面与 Discussion 的映射使用 pathname,每个页面路径对应一个讨论;
  • Discussion 分类选择 Announcements;
  • 开启反应,将评论框放在评论下方;
  • 语言选择简体中文,主题分别选择浅色和深色。

页面底部会生成一段 <script>。不需要把整段脚本复制到 Markdown,只需从中找到 data-repo-id 和 data-category-id,再填入 _config.butterfly.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
comments:
use: Giscus
text: true
lazyload: false
count: false
card_post_count: false

giscus:
repo: Lamb-fish/Lamb-fish.github.io
repo_id: R_kgDOQNUguw
category_id: DIC_kwDOQNUgu84DHEqy
light_theme: light
dark_theme: dark
option:
"data-category": Announcements
"data-mapping": pathname
"data-strict": "0"
"data-reactions-enabled": "1"
"data-input-position": bottom
"data-lang": zh-CN

repo_id 和 category_id 不是仓库名与分类名,不要手动猜测,直接复制 Giscus 页面生成的值。

3. 创建留言板并排查空白

可以先执行命令创建普通页面:

1
npx hexo new page comment

再将 source/comment/index.md 保持为:

1
2
3
4
---
title: 留言板
date: 2026-02-15 21:00:00
---

不要在这里设置 comments: false,否则主题不会生成评论容器。若容器存在但内容为空,按以下顺序检查:

  1. 仓库是否公开;
  2. Discussions 是否开启;
  3. Giscus App 是否已安装并授权到这个仓库;
  4. repo_id 和 category_id 是否复制完整;
  5. 浏览器控制台是否出现网络或跨域错误。

留言板与全站背景不一致时,让内容块直接复用侧栏背景变量:

1
2
3
4
5
6
7
8
9
body:has(#giscus-wrap) #page {
box-sizing: border-box;
padding: 28px 32px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 14px;
background: var(--sidebar-bg) !important;
background-image: none !important;
backdrop-filter: none !important;
}

使用 pathname 后,如果以后修改了文章永久链接,旧 Discussion 不会自动迁移,需要在 GitHub Discussions 中调整对应关系。

四、网易云音乐

1. 获取歌曲 ID

先在网易云音乐网页版打开目标单曲。歌曲链接通常包含:

1
https://music.163.com/song?id=1487620937

其中 1487620937 就是歌曲 ID,把它替换到外链播放器地址:

1
https://music.163.com/outchain/player?type=2&id=1487620937&auto=0&height=66

type=2 表示单曲,auto=0 表示不自动播放。现代浏览器通常会限制未经用户操作的自动播放,因此这里保留手动播放;受版权限制的歌曲也可能无法通过外链播放器播放。

2. 创建播放器

新建 source/js/music-sidebar.js。最初我把播放器插入了侧栏,普通访问没有问题,但 PJAX 会替换 #body-wrap,其中的 iframe 会被销毁,切页后音乐也随之停止。

最终把播放器挂到 body 下,使它脱离 PJAX 替换区域,并使用固定 ID 防止重复创建:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(() => {
const insertMusicCard = () => {
if (document.querySelector("#persistent-music-player")) return;

const card = document.createElement("div");
card.className = "card-widget card-music";
card.id = "persistent-music-player";
card.innerHTML = `
<iframe
class="music-player-frame"
title="网易云音乐播放器"
src="https://music.163.com/outchain/player?type=2&id=1487620937&auto=0&height=66"
frameborder="0"
loading="lazy"
allow="autoplay">
</iframe>`;

document.body.appendChild(card);
};

if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", insertMusicCard);
} else {
insertMusicCard();
}
})();

3. 固定位置并适配移动端

新建 source/css/music-sidebar.css。播放器固定在右下角,去掉 Butterfly 卡片默认的背景和阴影:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
.card-music {
position: fixed;
right: 20px;
bottom: 20px;
z-index: 999;
width: min(270px, calc(100vw - 32px));
padding: 0 !important;
border: 0;
background: transparent !important;
box-shadow: none !important;
}

.music-player-frame {
display: block;
width: 100%;
height: 86px;
border: 0;
border-radius: 8px;
}

@media (max-width: 768px) {
.card-music {
right: 12px;
bottom: 12px;
width: min(240px, calc(100vw - 24px));
}
}

最后注入这两个文件:

1
2
3
4
5
inject:
head:
- '<link rel="stylesheet" href="/css/music-sidebar.css?v=2">'
bottom:
- '<script src="/js/music-sidebar.js?v=3"></script>'

五、其他调整

同时还完成了几项小优化:

  • 更换首页与全站背景,滚动超过半个视口后为背景增加 6px 模糊;
  • 首页标题改为静态展示,增加随机文案和打字机效果;
  • 微信、QQ 图标支持悬停显示二维码;
  • 分类、标签、归档、友链和留言板统一使用侧栏背景变量。