自定义侧边栏博客目录

参考:

<!-- 目录索引列表生成 -->
<script language="javascript" type="text/javascript">

function GenerateContentList()
{
    if ($('#cnblogs_post_body').length == 0) { return; }
    var jquery_h1_list = $('#cnblogs_post_body h1');
    var jquery_h2_list = $('#cnblogs_post_body h2');
    var jquery_h3_list = $('#cnblogs_post_body h3');

    if (jquery_h1_list.length != 0)
    {
        var content = '<a name="_labelTop"></a>';
        content    += '<div id="navCategory">';
        content    += '<p style="font-size:18px"><b>目录</b></p>';
        content += '<ul class="first_class_ul">';

        for (var i = 0; i < jquery_h1_list.length; i++)
        {
            var go_to_top = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label' + i + '"></a></div>';
            $(jquery_h1_list[i]).before(go_to_top);
            var li_content = '<li><a href="#_label' + i + '">' + $(jquery_h1_list[i]).text() + '</a></li>';
            var nextH1Index = i + 1;
            if (nextH1Index == jquery_h1_list.length) { nextH1Index = 0; }
            var jquery_h2_list = $(jquery_h1_list[i]).nextUntil(jquery_h1_list[nextH1Index], "h2");
            if (jquery_h2_list.length > 0)
            {
                li_content += '<ul class="second_class_ul">';
            }
            for (var j = 0; j < jquery_h2_list.length; j++)
            {
                var go_to_top2 = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_lab2_'+ i + '_' + j + '"></a></div>';
                $(jquery_h2_list[j]).before(go_to_top2);
                li_content +='<li><a href="#_lab2_'+ i +'_' + j + '">' + $(jquery_h2_list[j]).text() + '</a></li>';
                var nextH2Index = j + 1;
                var next;
                if (nextH2Index == jquery_h2_list.length) 
                {
                    if (i + 1 == jquery_h1_list.length)
                    {
                        next = jquery_h1_list[0];
                    }
                    else
                    {
                        next = jquery_h1_list[i + 1];
                    }
                }
                else
                {
                    next = jquery_h2_list[nextH2Index];
                }
                var jquery_h3_list = $(jquery_h2_list[j]).nextUntil(next, "h3");
                if (jquery_h3_list.length > 0)
                {
                    li_content += '<ul class="third_class_ul">';
                }
                for (var k = 0; k < jquery_h3_list.length; k++)
                {
                    var go_to_third_Content = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label3_' + i + '_' + j + '_' + k + '"></a></div>';
                    $(jquery_h3_list[k]).before(go_to_third_Content);
                    li_content += '<li><a href="#_label3_' + i + '_' + j + '_' + k + '">' + $(jquery_h3_list[k]).text() + '</a></li>';
                }               
                if (jquery_h3_list.length > 0)
                {
                    li_content += '</ul>';
                }
                li_content += '</li>';
            }
            if (jquery_h2_list.length > 0)
            {
                li_content +='</ul>';
            }
            li_content +='</li>';
            content += li_content;
        }
        content    += '</ul>';
        content    += '</div>';
    }
    else if (jquery_h2_list.length != 0)
    {
        var content = '<a name="_labelTop"></a>';
        content    += '<div id="navCategory">';
        content    += '<p style="font-size:18px"><b>目录</b></p>';
        content    += '<ul class="second_class_ul">';
        for(var i =0; i < jquery_h2_list.length; i++)
        {
            var go_to_top2 = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_lab2_'+ i + '_' + j + '"></a></div>';
            $(jquery_h2_list[j]).before(go_to_top2);
            var li_content = '<li><a href="#_lab' + i + '">' + $(jquery_h2_list[i]).text() + '</a></li>';
            var nextH1Index = i + 1;
            if (nextH1Index == jquery_h2_list.length) { nextH1Index = 0; }
            var jquery_h3_list = $(jquery_h2_list[i]).nextUntil(jquery_h2_list[i+1], "h3");
            if(jquery_h3_list.length>0)
            {
                li_content +='<ul class="third_class_ul">';
            }
            for(var j = 0;j < jquery_h3_list.length;j++)
            {                  
                var go_to_third_Content = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label3_' + i + '_' + j + '_' + k + '"></a></div>';
                $(jquery_h3_list[k]).before(go_to_third_Content);
                li_content +='<li><a href="#_lab2'+ i +'_' + j + '">' + $(jquery_h3_list[j]).text() + '</a></li>';
            }
            if(jquery_h3_list.length>0)
            {
                li_content +='</ul>';
            }
            li_content +='</li>';
            content += li_content;
        }
        content    += '</ul>';
        content    += '</div>'; 
    }
    else if (jquery_h3_list.length != 0) 
    {
        var content = '<a name="_labelTop"></a>';
        content    += '<div id="navCategory">';
        content    += '<p style="font-size:18px"><b>目录</b></p>';
        content    += '<ul>';
        for(var i = 0; i < jquery_h3_list.length; i++)
        {
            var go_to_third_Content = '<div style="text-align: right"><a href="#_labelTop">返回目录</a><a name="_label3_' + i + '_' + j + '_' + k + '"></a></div>';
            $(jquery_h3_list[k]).before(go_to_third_Content);
            var li_content = '<li><a href="#_label' + i + '">' + $(jquery_h3_list[i]).text() + '</a></li>';
            content += li_content;
        }
        content    += '</ul>';
        content    += '</div>';
    }
    else
    {
        return;
    }
$($('#cnblogs_post_body')[0]).prepend(content);
}
GenerateContentList();
</script>

自定义CSS:(位置和背景颜色,以及超链接的下划线)

div#navCategory {
    position: fixed;
    left: 0;
    top: 20px;
    width: 300px;
    background-color:white;

}

#cnblogs_post_body a:link {
    text-decoration: none;
}

效果:

1566474449955

1566474460106


补丁 2019/8/28

侧边栏目录有个问题,在15.6的显示器上会出现遮挡情况。(当初设置的时候是用23寸显示器来写的)

于是在目录旁边加了一个隐藏显示的切换功能。

贴一下代码

<b>目录/</b><b class="hide">隐藏</b></p>';

然后JS设置一个滑动滑出的功能:


<script>
$(document).ready(function(){
  $(".hide").click(function(){
    $("#navCategory ul").slideToggle();
    if($(".hide").text()=="隐藏")
{
$(".hide").text("显示");
}
else 
{
$(".hide").text("隐藏");
}
    
  });
});
</script>
posted @ 2019-08-22 19:49  笑谈渴饮匈奴血  阅读(708)  评论(0编辑  收藏  举报