咕咕了很久啊,终于找到时间把这个小功能搓出来了

代码

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
document.addEventListener("pjax:complete", tonav);
document.addEventListener("DOMContentLoaded", tonav);

function tonav() {
if (document.querySelector(".code-expand-btn")) {
document
.querySelector(".code-expand-btn")
.addEventListener("click", function () {
const table = this.nextElementSibling;
// 展开状态
const isExpanded = this.classList.contains("expand-done");
// 展开高度
const tabBodyHeight = table.querySelector("tbody").offsetHeight;
// 默认高度
const mrHeight = "300px";

if (isExpanded) {
table.style.maxHeight = mrHeight;

setTimeout(() => {
table.style.maxHeight = tabBodyHeight + "px";
}, 1);
} else {
table.style.maxHeight = tabBodyHeight + "px";

setTimeout(() => {
table.style.maxHeight = mrHeight;
}, 1);
}
});
}
}

新建一个 js 文件,引入就行了