1
mirror of https://github.com/interstellar750/hexo_s synced 2024-10-19 00:23:51 +08:00
hexo_s/themes/Chic/layout/_partial/toc.ejs

58 lines
1.8 KiB
Plaintext

<div class="post-toc">
<div class="tocbot-list">
</div>
<div class="tocbot-list-menu">
<a class="tocbot-toc-expand" onclick="expand_toc()">展开全部</a>
<a onclick="go_top()">回到顶端</a>
<a onclick="go_bottom()">查看底部</a>
</div>
</div>
<script>
var tocbot_timer;
var DEPTH_MAX = 6; // 为 6 时展开所有
var tocbot_default_config = {
tocSelector: '.tocbot-list',
contentSelector: '.post-content',
headingSelector: 'h1, h2, h3',
orderedList: false,
scrollSmooth: true,
onClick: extend_click,
};
function extend_click() {
clearTimeout(tocbot_timer);
tocbot_timer = setTimeout(function () {
tocbot.refresh(obj_merge(tocbot_default_config, { hasInnerContainers: true }));
}, 420); // 这个值是由 tocbot 源码里定义的 scrollSmoothDuration 得来的
}
document.ready(function () {
tocbot.init(obj_merge(tocbot_default_config, { collapseDepth: 1 }));
});
function expandToc() {
var b = document.querySelector('.tocbot-toc-expand');
var expanded = b.getAttribute('data-expanded');
expanded ? b.removeAttribute('data-expanded') : b.setAttribute('data-expanded', true);
tocbot.refresh(obj_merge(tocbot_default_config, { collapseDepth: expanded ? 1 : DEPTH_MAX }));
b.innerText = expanded ? 'Expand all' : 'Collapse all';
}
function go_top() {
window.scrollTo(0, 0);
}
function go_bottom() {
window.scrollTo(0, document.body.scrollHeight);
}
function obj_merge(target, source) {
for (var item in source) {
if (source.hasOwnProperty(item)) {
target[item] = source[item];
}
}
return target;
}
</script>