Hexo 自定义代码折叠

代码折叠功能在hexo中没有提供,需要自己自定义添加css和js。

自定义CSS

将自定义的css样式加入到: Blog目录/themes/next/source/css/_custom/custom.styl

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
.highlight{
position: relative;
}
.code-fold{
overflow: hidden;
height: 1em;
}
.code-fold::before{
content: '双击展开';
display: block;
position: absolute;
right: 0em;
top: 0;
width: 10em;
height: 1em;
z-index: 10;
color: #fc0;
}
.code-unfold::before{
content: '双击关闭';
display: block;
position: absolute;
right: 0em;
top: 0;
width: 10em;
height: 1em;
z-index: 10;
color: #fc0;
}

自定义JS

将自定义的js添加到(全局生效): Blog目录/themes/next/source/js/src/post-details.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$(function(){
$(".highlight").dblclick(function(){
if ($(this).hasClass("code-fold")) {
$(this).removeClass("code-fold").addClass("code-unfold");
}else{
$(this).removeClass("code-unfold").addClass("code-fold");
}
});
$(".highlight").each(function(){
var obj = $(this);
if (!obj.hasClass("code-fold")) {
obj.addClass("code-fold");
}
});
}
)

感谢:
http://www.missfli.com/2016/10/02/hexo-customer-js/