• 博客园之生成侧边目录


    说明:

    只能适配二、三、四级目录,当存在二级标题时,以二级标题为起始,否则以三级标题为起始,向下展示一级目录.

    JS代码:

    <script language="javascript" type="text/javascript">
        function GenerateContentList(h23) {
            var mainContent = $('#cnblogs_post_body');
            var h2_list = $('#cnblogs_post_body').children(h23);
    
            if (mainContent.length < 1)
                return;
    
            if (h2_list.length > 0) {
                var content = '<a name="_labelTop"></a>';
                content += '<div id="navCategory">';
                content += '<p id="mylist" style="font-size:30px"><b>目录</b></p>';
                content += '<ul id="myul"><li style="text-align: right"><b id="guanbi" style="cursor: pointer">x</b></li>';
                for (var i = 0; i < h2_list.length; i++) {
                    var go_to_top = '<div style="text-align: right"><a href="#_labelTop">Top~~</a><a name="_label' + i + '"></a></div>';
                    $(h2_list[i]).before(go_to_top);
    
                    var h3_list = $(h2_list[i]).nextAll("h3");
                    var li3_content = '';
                    for (var j = 0; j < h3_list.length; j++) {
                        var tmp = $(h3_list[j]).prevAll('h2').first();
                        if (!tmp.is(h2_list[i]))
                            break;
                        var li3_anchor = '<a name="_label' + i + '_' + j + '"></a>';
                        $(h3_list[j]).before(li3_anchor);
                        li3_content += '<li><a href="#_label' + i + '_' + j + '">' + $(h3_list[j]).text() + '</a></li>';
                    }
    
                    var li2_content = '';
                    if (li3_content.length > 0)
                        li2_content = '<li><a href="#_label' + i + '">' + $(h2_list[i]).text() + '</a><ul>' + li3_content + '</ul></li>';
                    else
                        li2_content = '<li><a href="#_label' + i + '">' + $(h2_list[i]).text() + '</a></li>';
                    content += li2_content;
                }
                content += '</ul>';
                if ($('#cnblogs_post_body').length != 0) {
                    $($('#cnblogs_post_body')[0]).prepend(content);
                }
            }
        }
    
        if ($('#cnblogs_post_body h2').length > 0) {
            GenerateContentList('h2');
        } else {
            GenerateContentList('h3');
        }
        jQuery('#mylist').click(function () {
            var my_list = jQuery(this);
            var my_mulu = jQuery('#myul');
            my_list.stop();
            my_mulu.stop();
            my_list.slideUp(400);
            my_mulu.fadeIn(600);
        });
        $('#guanbi').click(function () {
            var my_list = jQuery('#mylist');
            var my_mulu = jQuery('#myul');
            my_list.stop();
            my_mulu.stop();
            my_list.slideDown(400);
            my_mulu.fadeOut(600);
        })
    </script>
    

    CSS样式:

    #navCategory {
        position: fixed;
        right: 0;
        top: 100px;
        list-style: none;
        z-index: 99999;
    }
    
    #navCategory > #mylist {
        position: absolute;
        background-color: white;
         32px;
        left: -32px;
        font-size: 30px;
        font-weight: bold;
        border-radius: 5px;
        opacity: 0.8;
    }
    
    #navCategory > #myul {
        background-color: white;
        height: 400px;
         230px;
        padding-top: 5px;
        overflow: auto;
        display: none;
    }
    
    #navCategory ul, #navCategory li {
        list-style: none !important;
        margin: 5px !important;
    }
    
  • 相关阅读:
    通过另外一个应用程序给多个文本框赋值, 模拟单击事件
    AngularJS
    九章算法
    实现继承
    二分查找
    NET Core依赖注入解读&使用Autofac替代实现
    NET SignalR 与 LayIM2.0
    WebVR
    依赖注入
    如何实现配置与源文件的同步
  • 原文地址:https://www.cnblogs.com/zyyhxbs/p/11421975.html
Copyright © 2020-2023  润新知