Skip to content

Commit 3930c50

Browse files
committed
cnblogs-article-nav add toc;
1 parent acadf07 commit 3930c50

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

cnblogs-article-nav.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,29 @@
167167
}
168168
#sideToolbar-up:hover {
169169
background: url("http://files.cnblogs.com/files/zhaoqingqing/sideToolbar.gif") no-repeat scroll -74px -62px transparent
170+
}
171+
172+
/*====== toc =======*/
173+
.cnblogs_toc {
174+
float:left;
175+
min-width:200px;
176+
padding: 4px 10px;
177+
font-size: 12px;
178+
background-color: #eee;
179+
border: 1px solid #ccc;
180+
}
181+
182+
.cnblogs_toc a {
183+
color: #369;
184+
border-bottom: 0px;
185+
}
186+
187+
.cnblogs_toc ol {
188+
189+
margin: 5px 14px 5px;
190+
line-height: 160%;
191+
}
192+
193+
.cnblogs_toc li {
194+
list-style: decimal;
170195
}

cnblogs-article-nav.js

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
http://www.cnblogs.com/asxinyu/p/Bolg_Category_For_BlogBeauty.html
66
*/
77

8-
var a = $(document);
9-
a.ready(function() {
8+
//cnblogs生成右侧目录
9+
$(document).ready(function() {
1010
var b = $('body'),
1111
cnblogs_post_body = 'cnblogs_post_body',
1212
sideNavBody = 'sideToolbar',
@@ -124,4 +124,74 @@ a.ready(function() {
124124
$sideToolbar.css('display', 'none')
125125
} */
126126
// })
127-
});
127+
});
128+
129+
// 在文章的内容页生成TOC
130+
$(document).ready(function() {
131+
buildTocTable();
132+
});
133+
134+
function buildTocTable() {
135+
var hs = $('#cnblogs_post_body').find('h1,h2,h3,h4,h5,h6');
136+
if (hs.length < 2)
137+
return;
138+
var s = '';
139+
s += '<div style="clear:both"></div>';
140+
s += '<div class="cnblogs_toc">';
141+
s += '<p style="text-align:right;margin:0;"><span style="float:left; text-indent:0;">文章目录<a href="#" title="系统根据文章中H1到H6标签自动生成文章目录">(?)</a></span><a href="#" onclick="javascript:return openct(this);" title="展开">[+]</a></p>';
142+
//.cnblogs_toc ol 控制默认展开或关闭
143+
s += '<ol style="display:none;margin-left:14px;padding-left:14px;line-height:160%;">';
144+
var old_h = 0, ol_cnt = 0;
145+
for (var i = 0; i < hs.length; i++) {
146+
var h = parseInt(hs[i].tagName.substr(1), 10);
147+
if (!old_h)
148+
old_h = h;
149+
if (h > old_h) {
150+
s += '<ol>';
151+
ol_cnt++;
152+
}
153+
else if (h < old_h && ol_cnt > 0) {
154+
s += '</ol>';
155+
ol_cnt--;
156+
}
157+
if (h == 1) {
158+
while (ol_cnt > 0) {
159+
s += '</ol>';
160+
ol_cnt--;
161+
}
162+
}
163+
old_h = h;
164+
var tit = hs.eq(i).text().replace(/^\d+[.\s]+/g, '');
165+
tit = tit.replace(/[^a-zA-Z0-9_\-\s\u4e00-\u9fa5]+/g, '');
166+
167+
if (tit.length < 100) {
168+
s += '<li><a href="#t' + i + '">' + tit + '</a></li>';
169+
hs.eq(i).html('<a name="t' + i + '"></a>' + hs.eq(i).html());
170+
}
171+
}
172+
while (ol_cnt > 0) {
173+
s += '</ol>';
174+
ol_cnt--;
175+
}
176+
s += '</ol></div>';
177+
s += '<div style="clear:both"><br></div>';
178+
$(s).insertBefore($('#cnblogs_post_body'));
179+
180+
//mobile detect
181+
var md = new MobileDetect(window.navigator.userAgent);
182+
if (md.mobile()) {
183+
$('.cnblogs_toc ol').css('display','none');
184+
}else{
185+
$('.cnblogs_toc ol').css('display','block');
186+
}
187+
}
188+
189+
function openct(e) {
190+
if (e.innerHTML == '[+]') {
191+
$(e).attr('title', '收起').html('[-]').parent().next().show();
192+
} else {
193+
$(e).attr('title', '展开').html('[+]').parent().next().hide();
194+
}
195+
e.blur();
196+
return false;
197+
}

0 commit comments

Comments
 (0)