博客园博客目录自动生成(页面目录)

1. 页面CSS代码区中CSS代码:

#sideCatalog a{
  font-size:12px;
  font-weight:normal !important;
}

2. 侧边栏公告区中js代码:

<script type="text/javascript">
//以下是锚点JS,自动生成目录
var a = $(document);
a.ready(function() {
    var commentDiv = $("#blog-comments-placeholder");
    if (commentDiv.length <= 0) {
      return;
    }
    var b = $('body'),
        d = 'sideToolbar',
        e = 'sideCatalog',
        f = 'sideCatalog-catalog',
        g = 'sideCatalogBtn',
        h = 'sideToolbar-up',
        i = '<div id="sideToolbar"style="display:none;bottom:150px;">\<div class="sideCatalogBg"id="sideCatalog">\<div id="sideCatalog-sidebar">\<div class="sideCatalog-sidebar-top"></div>\<div class="sideCatalog-sidebar-bottom"></div>\</div>\<div id="sideCatalog-catalog">\<ul class="nav"style="width:225px;zoom:1;list-style: none;">\</ul>\</div>\</div>\<a href="javascript:void(0);"id="sideCatalogBtn"class="sideCatalogBtnDisable" style="position:absolute;bottom:10px;"></a>\</div>',
        j = '',
        k = 500,
        l = 0,
        m = 0,
        n = 0,
        //限制存在个数,如数量过多,则只显示h2,不显示h3
        //o, p = 13,
        o, p = 100,
        q = true,
        r = true,
        s = b;
    if(s.length === 0) {
        return
    };
    b.append(i);
    //指定获取目录的范围-------------这一点非常重要,因为每个人指定的范围都不一样,所以这是要修改的地方
    //o = s.find(':header');
    o = $('#cnblogs_post_body').find(':header');
    if(o.length > p) {
        r = false;
        var t = s.find('h3');
        var u = s.find('h4');
        if(t.length + u.length > p) {
            q = false
        }
    };
    o.each(function(t) {
        var u = $(this),
            v = u[0];

        var title = u.text();
        var text = u.text();

        u.attr('id', 'autoid-' + l + '-' + m + '-' + n)
        //if (!u.attr('id')) {
        //    u.attr('id', 'autoid-' + l + '-' + m + '-' + n)
        //};
        if(text.length > 12) text = text.substr(0, 12) + "...";
        if(v.localName === 'h3') {
            l++;
            m = 0;
            //if(text.length > 12) text = text.substr(0, 12) + "...";
            j += '<li><a href="#' + u.attr('id') + '" title="' + title + '">' + text + '</a><span class="sideCatalog-dot" style="top:8px;"></span></li>';
        } else if(v.localName === 'h4') {
            m++;
            n = 0;
            if(q) {
                //if(text.length > 12) text = text.substr(0, 12) + "...";
                j += '<li class="h2Offset"><a href="#' + u.attr('id') + '" title="' + title + '">' + text + '</a></li>';
            }
        } else if(v.localName === 'h5') {
            n++;
            if(r) {
                j += '<li class="h3Offset"><a href="#' + u.attr('id') + '" title="' + title + '">' + u.text() + '</a></li>';
            }
        }
    });
    $('#' + f + '>ul').html(j);
    b.data('spy', 'scroll');
    b.data('target', '.sideCatalogBg');
    $('body').scrollspy({
        target: '.sideCatalogBg'
    });
    $sideCatelog = $('#' + e);
    $sideToolbar = $('#' + d);
    $('#sideCatalogBtn').hover(function () {
        $sideCatelog.css('display', 'block');
    });
    $sideToolbar.hover(function(){}, function(){
        $sideCatelog.css('display', 'none');
    });
    $('#' + h).on('click', function() {
        $("html,body").animate({
            scrollTop: 0
        }, 500)
    });
    a.on('scroll', function() {
        var t = a.scrollTop();
        if(t > k) {
            $sideToolbar.css('display', 'block');
            $('#gotop').show()
        } else {
            $sideToolbar.css('display', 'none')
            $('#gotop').hide()
        }
    })
});
//以上是锚点JS
</script>
View Code

3.页首Html代码中CSS引用:

<link  type="text/css" rel="stylesheet" href="https://files.cnblogs.com/files/miangao/maodian.css">

4. 页脚Html代码中JS引用:

<script src="https://files.cnblogs.com/files/miangao/bootstrap.min.js"></script>

5.效果展示:

转自来源:阿豪聊干货

博文地址:http://www.cnblogs.com/hafiz 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白菜!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值