轮子狂魔手把手教你用JS给博客动态增加目录 - 超级懒人版

Posted 鬼谷子的程序人生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了轮子狂魔手把手教你用JS给博客动态增加目录 - 超级懒人版相关的知识,希望对你有一定的参考价值。

动态显示目录的作用

不用每次写博客的时候繁琐的人工整理目录,又可以动态浮动在右下角,方便快速跳到感兴趣的位置同时也可以快速的对文章内容有一个大概的了解。

 

实现原理

首先根据个人喜好,我习惯了用 h1 来做分类。所以本篇内容也主要是针对h1来提取目录。

如何提取出来h1呢?

先来看这张图,以猎豹浏览器为例:

首先在博客内容第一行点击鼠标右键,然后选择检查。这时会弹出右边的框,直接定位到我的h1标签,就这么简单的找到了它的父级 cnblogs_post_body 。

然后使用 jquery 选择器 来获取到这些h1,对jquery选择器不熟的直接跳这个链接温习一下:http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp

$(\'#cnblogs_post_body h1\')

就这么简单的一个括号就完成了对 h1 的提取。

在遍历所有的h1,取出内容之前,我们需要一个目录的容器。

$(\'#cnblogs_post_body\').append(\'<div id="blog_catalog" class="blog_catalog"><ul><li><a id="blog_catalog_close" class="blog_catalog_close">>折叠目录</a></li></ul></div>\');

这句简单解释就是在博客内容最末尾插入了一个 div,里面包含ul和一个默认的li 用于折叠目录。

接下来就要提取h1的内容了,但在这个过程中我们还要做一件事,就是自动给h1增加一个id,作为一个标准的懒人,我肯定连h1的id都不想写的,自动生成神马的最好了。

var id = 1;
$(\'#cnblogs_post_body h1\').each(function(){
    $(this).attr(\'id\',\'blog_catalog_id_\'+id);
    $(\'#blog_catalog ul\').append(\'<li><a href="#blog_catalog_id_\'+id+\'">\'+$(this).text()+\'</a></li>\');
    id++;
});

有了折叠,当然少不了展开。

$(\'#cnblogs_post_body\').append(\'<div id="blog_catalog_open" class="blog_catalog_open">展开目录</div>\');

最后一步,让展开目录和折叠目录联动起来

$(\'#blog_catalog_open\').click(function(){
$(\'#blog_catalog\').show();
$(\'#blog_catalog_open\').hide();
});

$(\'#blog_catalog_close\').click(function(){
$(\'#blog_catalog\').hide();
$(\'#blog_catalog_open\').show();
});

整个制作过程其实并不复杂,还有一些css样式应用上就完工了。

 

不管原理,我想直接用

 怎么自定义皮肤我就不多说了,一抓一大把。下面直接给你们代码。

css:

.blog_catalog {
    display: none;
    width: auto;
    height: auto;
    float: right;
    position: fixed;
    right: 180px;
    bottom: 200px;
    z-index: 9999;
    background-color: #fff;
    font-size: 12px;
    margin: 10px 0 0 0;
    padding: 5px;
    text-align: center;
    border: 3px solid #55895b;
    border-radius: 5px;
    -webkit-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13);
    -moz-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13);
    box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13);
}

    .blog_catalog > li > a {
        background-color: #616975;
        background-image: -webkit-gradient(linear, left top, left bottom, from(rgb(114, 122, 134)),to(rgb(80, 88, 100)));
        background-image: -webkit-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
        background-image: -moz-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
        background-image: -o-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
        background-image: -ms-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
        background-image: linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
        filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,StartColorStr=\'#727a86\', EndColorStr=\'#505864\');
        -webkit-box-shadow: inset 0px 1px 0px 0px #878e98;
        -moz-box-shadow: inset 0px 1px 0px 0px #878e98;
        box-shadow: inset 0px 1px 0px 0px #878e98;
        width: 100%;
        height: 2.75em;
        line-height: 2.75em;
        text-indent: 2.75em;
        display: block;
        position: relative;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-weight: 600;
        color: #fff;
        text-shadow: 0px 1px 0px rgba(0,0,0,.5);
    }

    .blog_catalog ul li a {
        background: #fff;
        border-bottom: 1px solid #efeff0;
        width: 100%;
        height: 2.75em;
        line-height: 2.75em;
        display: block;
        position: relative;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 0.923em;
        font-weight: 400;
        color: #878d95;
    }

        .blog_catalog ul li a:hover {
            cursor: pointer;
        }

    .blog_catalog > li > a:hover, .blog_catalog > li > a.active, .blog_catalog > li:target > a; /*add this*/ {
        background-color: #35afe3;
        background-image: -webkit-gradient(linear, left top, left bottom, from(rgb(69, 199, 235)),to(rgb(38, 152, 219)));
        background-image: -webkit-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
        background-image: -moz-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
        background-image: -o-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
        background-image: -ms-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
        background-image: linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
        filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,StartColorStr=\'#45c7eb\', EndColorStr=\'#2698db\');
        border-bottom: 1px solid #103c56;
        -webkit-box-shadow: inset 0px 1px 0px 0px #6ad2ef;
        -moz-box-shadow: inset 0px 1px 0px 0px #6ad2ef;
        box-shadow: inset 0px 1px 0px 0px #6ad2ef;
    }

    .blog_catalog > li > a.active {
        border-bottom: 1px solid #1a638f;
    }

    .blog_catalog > li > a:before {
        content: \'\';
        background-image: url(../images/sprite.png);
        background-repeat: no-repeat;
        font-size: 36px;
        height: 1em;
        width: 1em;
        position: absolute;
        left: 0;
        top: 50%;
        margin: -.5em 0 0 0;
    }

.item1 > a:before {
    background-position: 0 0;
}

.item2 > a:before {
    background-position: -38px 0;
}

.item3 > a:before {
    background-position: 0 -38px;
}

.item4 > a:before {
    background-position: -38px -38px;
}

.item5 > a:before {
    background-position: -76px 0;
}

.blog_catalog > li > a span {
    font-size: 0.857em;
    display: inline-block;
    position: absolute;
    right: 1em;
    top: 50%;
    background: #48515c;
    line-height: 1em;
    height: 1em;
    padding: .4em .6em;
    margin: -.8em 0 0 0;
    color: #fff;
    text-indent: 0;
    text-align: center;
    -webkit-border-radius: .769em;
    -moz-border-radius: .769em;
    border-radius: .769em;
    -webkit-box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, .26), 0px 1px 0px 0px rgba(255, 255, 255, .15);
    -moz-box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, .26), 0px 1px 0px 0px rgba(255, 255, 255, .15);
    box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, .26), 0px 1px 0px 0px rgba(255, 255, 255, .15);
    text-shadow: 0px 1px 0px rgba(0,0,0,.5);
    font-weight: 500;
}

.blog_catalog > li > a:hover span,
.blog_catalog > li a.active span,
.blog_catalog > li:target > a span /*add this*/ {
    background: #2173a1;
}

.blog_catalog > li > ul li a:before {
    font-size: 8px;
    color: #bcbcbf;
    position: absolute;
    width: 1em;
    height: 1em;
    top: 0;
    left: -2.7em;
}

.blog_catalog > li > ul li:hover a,
.blog_catalog > li > ul li:hover a span,
.blog_catalog > li > ul li:hover a:before {
    color: #32373D;
}


.blog_catalog ul > li > a span {
    font-size: 0.857em;
    display: inline-block;
    position: absolute;
    right: 1em;
    top: 50%;
    / background: #fff;
    border: 1px solid #d0d0d3;
    line-height: 1em;
    height: 1em;
    padding: .4em .7em;
    margin: -.9em 0 0 0;
    color: #878d95;
    text-indent: 0;
    text-align: center;
    -webkit-border-radius: .769em;
    -moz-border-radius: 769em;
    border-radius: 769em;
    text-shadow: 0px 0px 0px rgba(255,255,255,.01));
}

/*additional*/

.blog_catalog > li > ul {
    height: 0;
    overflow: hidden;
    opacity: 0;
    filter: alpha(opacity=0); /* IE6-IE8 */
    -webkit-transition: opacity 0.9s ease-in-out;
    -moz-transition: opacity 0.9s ease-in-out;
    -o-transition: opacity 0.9s ease-in-out;
    -ms-transition: opacity 0.9s ease-in-out;
    transition: opacity 0.9s ease-in-out;
}

.blog_catalog > li:target > ul {
    height: auto; /*using auto nullifies the height transitions, but it makes things flexible which is more important*/
    border-bottom: 1px solid #51555a;
    opacity: 1;
    filter: alpha(opacity=100); /* IE6-IE8 */
}

#cnblogs_post_body ul li {
    list-style-type: none;
    margin-left: -30px;
}

.blog_catalog_open {
    width: auto;
    height: auto;
    float: right;
    position: fixed;
    right: 180px;
    bottom: 200px;
    z-index: 9999;
    background-color: #fff;
    font-size: 12px;
    width: 125px;
    margin: 10px 0 0 0;
    padding: 5px;
    text-align: center;
    border: 3px solid #55895b;
    border-radius: 5px;
    -webkit-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13);
    -moz-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13);
    box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13);
    cursor: pointer;
}
View Code

js:

$(\'#cnblogs_post_body\').append(\'<div id="blog_catalog_open" class="blog_catalog_open">展开目录</div>\');
$(\'#cnblogs_post_body\').append(\'<div id="blog_catalog" class="blog_catalog"><ul><li><a id="blog_catalog_close" class="blog_catalog_close">>折叠目录</a></li></ul></div>\');

var id = 1;
$(\'#cnblogs_post_body h1\').each(function(){
    $(this).attr(\'id\',\'blog_catalog_id_\'+id);
    $(\'#blog_catalog ul\').append(\'<li><a href="#blog_catalog_id_\'+id+\'">\'+$(this).text()+\'</a></li>\');
    id++;
});

$(\'#blog_catalog_open\').click(function(){
$(\'#blog_catalog\').show();
$(\'#blog_catalog_open\').hide();
});

$(\'#blog_catalog_close\').click(function(){
$(\'#blog_catalog\').hide();
$(\'#blog_catalog_open\').show();
});
View Code

 

如何使用

<h1>你可以随意设置你的标题</h1>

没错,就是这么简单,其他什么都不需要。

我不只要用目录,我还要完整皮肤

先声明,我这套皮肤是从别人那扒下来的一个雏形然后自己再改了不少地方。但是从哪里弄来的,忘了。。。很尴尬。

具体设置皮肤的也是一抓一大把就不多说了,上干货。

1.博客皮肤 选择 Custom

2.页面定制css代码

@charset "utf-8";
/* CSS Document */
/* By rhinoc.cnblogs.com*/

/*第一部分*/
#EntryTag {
    margin-top: 20px;
    font-size: 9pt;
    color: gray;
}

.topicListFooter {
    text-align: right;
    margin-right: 10px;
    margin-top: 10px;
}

#divRefreshComments {
    text-align: right;
    margin-right: 10px;
    margin-bottom: 5px;
    font-size: 9pt;
}

/*全局样式*/
{
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
}

body {
    background-image: url(http://images0.cnblogs.com/blog2015/618672/201508/201613472223856.png);
    background-repeat: repeat;
    font-family: \'Lucida Console\',Georgia,\'Microsoft YaHei\',Microsoft YaHei;
    \\5B8B\\4F53, sans-serif;
    font: \'Lucida Console\',Georgia,\'Microsoft YaHei\',Microsoft YaHei;
    font-size: 11.5px;
    min-height: 101%;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}

fieldset, img {
    border: 0;
}

ul {
    word-break: break-all;
}

li {
    list-style: none;
}

h1, h2, h3, h4, h5, h6 {
    font-size: 100%;
    font-weight: normal;
}

a {
    outline: none;
    color: #21759b;
}

address, cite, dfn, em, var {
    font-style: normal;
}

code, kbd, pre, samp, tt {
    font-family: "Courier New", Courier,Microsoft Yahei, monospace;
}

.clear {
    clear: both;
}

/*第三部分*/
/*home和头部*/
#home {
    margin: 0 auto;
    width: 65%;
    min-width: 1000px;
    background-color: #fff;
    padding: 30px;
    margin-top: 50px;
    margin-bottom: 50px;
    box-shadow: 0px 1px 10px #999;
    -moz-box-shadow: 0px 1px 10px #999;
    -web-kit-shadow: 0px 1px 10px #999;
}

#header {
    padding-bottom: 5px;
    margin-top: 20px;
}

#blogTitle {
    height: 50px;
    clear: both;
    font-family: Georgia,Serif;
}

#InkBlogLogo {
    display: none;
}

/*博客名称*/
#blogTitle h1 {
    font-size: 28px;
    font-weight: bold;
    line-height: 0.2em;
    margin-top: 20px;
}

    #blogTitle h1 a {
        color: #515151;
    }

        #blogTitle h1 a:hover {
            color: #21759b;
        }

#blogTitle h2 {
    font-weight: normal;
    font-size: 14.5px;
    line-height: 0.3em;
    color: #515151;
    float: left;
    margin-left: 2em;
    margin-bottom: 2em;
}

#blogLogo {
    float: right;
}

/*导航栏*/
#navigator {
    text-decoration: none;
    font-size: 14px;
    font-family: \'Lucida Console\',Georgia,\'FZYaoTi\',Microsoft YaHei;
    \\5B8B\\4F53, sans-serif;
    font: \'Lucida Console\',Georgia,\'FZYaoTi\',Microsoft YaHei;
    border-bottom: 1px solid #515151;
    border-top: 1px solid #515151;
    height: 80px;
    clear: both;
    margin-top: 20px;
}

#navList {
    width: 1200px;
    min-height: 30px;
    float: left;
}

    #navList .border {
        height: 28px;
        position: absolute;
        width: 5px;
        left: 0px;
        top: 0px;
        overflow: hidden;
        opacity: 0;
        background: #F90;
        -webkit-transition: 0.3s all ease;
        -moz-transition: 0.3s all ease;
        -ms-transition: 0.3s all ease;
        -o-transition: 0.3s all ease;
        -webkit-transition: .5s left ease;
    }

    #navList li {
        float: left;
        margin: 0px,40px,0px,0px;
        -webkit-transition: 0.3s all ease;
        -moz-transition: 0.3s all ease;
        -ms-transition: 0.3s all ease;
        -o-transition: 0.3s all ease;
        transition: 0.3s all ease;
        overflow: hidden;
        position: relative;
    }

        #navList li:hover {
            background: #000;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.4);
        }

            #navList li:hover .border {
                opacity: 1;
                left: 65px;
            }

            #navList li:hover a {
                color: #FFF;
                text-shadow: 1px 2px 4px #333;
            }

            #navList li:hover .menu {
                -webkit-animation-name: shake;
                -moz-animation-name: shake;
            }

.menu {
    -webkit-animation: .5s .2s ease both;
    -moz-animation: 1s .2s ease both;
}

@-webkit-keyframes shake {
    0%,100% {
        -webkit-transform: translateX(0);
    }

    20%,60% {
        -webkit-transform: translateX(-10px);
    }

    40%,80% {
        -webkit-transform: translateX(10px);
    }
}

@-moz-keyframes shake {
    0%,100% {
        -moz-transform: translateX(0);
    }

    20%,60% {
        -moz-transform: translateX(-10px);
    }

    40%,80% {
        -moz-transform: translateX(10px);
    }
}

#navList a {
    text-decoration: none;
    display: block;
    width: 5em;
    height: 20px;
    float: left;
    text-align: center;
    font-weight: bold;
    padding-top: 8px;
    color: #515151;
}

.blogStats {
    float: right;
    font-style: italic;
    font-family: Georgia,\'FZYaoTi\',Microsoft YaHei;
    \\5B8B\\4F53, sans-serif;
    color: #757575;
    margin-right: 1px;
    text-align: right;
}

/*主页文章列表*/
#main {
    width: 100%;
    text-align: left;
    margin-top: 10px;
}

#mainContent .forFlow {
    margin-left: 22em;
    float: none;
    width: auto;
}

#mainContent {
    min-height: 200px;
    padding: 0px 0px 10px 0;
    -o-text-overflow: ellipsis;
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
    float: left;
    margin-left: -22em;
    margin-top: 0;
    width: 100%;
}

/*日期*/
.day {
    text-decoration: none;
    background: #FFF;
    padding: 20px;
    margin-bottom: -1px;
    color: #515151;
    font-size: 21px;
    line-height: 1.5em;
    float: left;
    clear: right;
}

    .day:hover {
        border: 1px solid #21759B;
        position: relative;
        z-index: 10;
    }

        .day:hover .postSeparator {
            border-top: 1px dashed #515151;
        }

.dayTitle {
    text-decoration: none;
}

    .dayTitle a {
        text-decoration: none;
        color: #515151;
        font-size: 13px;
        font-weight: bold;
        font-family: Georgia,Consolas,Microsoft YaHei, monospace;
    }

/*文章标题*/
.postTitle {
    font-family: Georgia,\'Consolas\',\'FZYaoTi\',\'STHeiti\',Microsoft YaHei;
    \\5B8B\\4F53, sans-serif;
    font: \'Lucida Console\',Georgia,\'Microsoft YaHei\',Microsoft YaHei;
    margin-bottom: 10px;
    font-size: 20px;
    font-weight: bold;
    float: right;
    width: 100%;
    clear: both;
}

    .postTitle a:link, .postTitle a:visited, .postTitle a:active {
        color: #21759b;
        font-weight: bold;
        transition: all 0.4s linear 0s;
    }

    .postTitle a:hover {
        text-decoration: none;
        margin-left: 30px;
        font-weight: bold;
        color: #45bcf9;
    }

.postTitle2 {
    text-decoration: none;
    font-size: 20px;
    font-weight: bold;
    font-family: Georgia,\'Consolas\',\'FZYaoTi\',\'STHeiti\',Microsoft YaHei;
    \\5B8B\\4F53, sans-serif;
    font: \'Lucida Console\',Georgia,\'Microsoft YaHei\',Microsoft YaHei;
    padding-right: 64px;
    padding-left: 10px;
    border-left-style: solid;
    border-left-width: 3px;
    border-left-color: #515151;
}

.postCon {
    float: right;
    line-height: 1.5em;
    width: 100%;
    clear: both;
    padding: 10px 0;
}

.day .postTitle a {
    padding-left: 10px;
}

.postDesc {
    border-right: 3px solid #21759b;
    font-size: 12px;
    color: #21759b;
    float: right;
    width: 100%;
    clear: both;
    text-align: right;
    padding-left: 20px;
    padding-right: 5px;
    margin-top: 20px;
    line-height: 1.5;
}

    .postDesc a:link, .postDesc a:visited, .postDesc a:active {
        color: #666;
    }

    .postDesc a:hover {
        color: #21759b;
        text-decoration: none;
    }

.postSeparator {
    clear: both;
    height: 1px;
    width: 100%;
    clear: both;
    float: right;
    margin: 0 auto 15px auto;
}

/*侧边栏*/
#sideBar {
    margin-top: -15px;
    width: 240px;
    min-height: 200px;
    padding: 0px 0 0px 5px;
    float: right;
    -o-text-overflow: ellipsis;
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
}

    #sideBar a {
        color: #757575;
    }

        #sideBar a:hover {
            color: #21759b;
        }

.mySearch {
    background: #FFF;
}

.catListTitle {
    font-size: 16px;
    background-color: #169FE6;
    color: white;
    font-weight: normal;
    margin-bottom: 5px;
}

.catListEssay ul li {
    font-size: 12px;
    font-手把手教你用Pyecharts绘制这3种超经典图表

手把手教你用canvas画动态直线

手把手教你用canvas画动态直线

❤️手把手教你用Android Studio做一个超好玩的拼图游戏,0基础Android小白也能包你学会,附送超详细注释的源码,建议收藏!❤️

javascript基础修炼(12)——手把手教你造一个简易的require.js

手把手教你用npm发布一个包,详细教程