-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.php
61 lines (59 loc) · 1.99 KB
/
toc.php
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<script src="<?php $this->options->themeUrl('tocbot.min.js'); ?>"></script>
<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, h4, h5',
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 expand_toc() {
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>