博客园自定义目录

博客园自定义目录
js样式
在css中设置

TOC

博客园自定义目录

参考:https://www.cnblogs.com/xdp-gacl/p/3718879.html#2937655

参考了这篇博客,但是这个只显示h2,h3的标题,就自己处理了一下,显示了h1--h5的标题

之前使用的是这样的目录https://www.cnblogs.com/ziyue7575/p/11407354.html 但是目录隐藏之后,这块区域仍然不能点击,所以,就自己写了一下....就是丑了点

js样式

在页首html中设置

<script type="text/javascript">
    /*
    功能:生成博客目录的JS工具
    */
    var BlogDirectory = {
        /*
            获取元素位置,距浏览器左边界的距离(left)和距浏览器上边界的距离(top)
        */
        getElementPosition: function (ele) {
            var topPosition = 0;
            var leftPosition = 0;
            while (ele) {
                topPosition += ele.offsetTop;
                leftPosition += ele.offsetLeft;
                ele = ele.offsetParent;
            }
            return {top: topPosition, left: leftPosition};
        },

        /*
        获取滚动条当前位置
        */
        getScrollBarPosition: function () {
            var scrollBarPosition = document.body.scrollTop || document.documentElement.scrollTop;
            return scrollBarPosition;
        },

        /*
        移动滚动条,finalPos 为目的位置,internal 为移动速度
        */
        moveScrollBar: function (finalpos, interval) {

            //若不支持此方法,则退出
            if (!window.scrollTo) {
                return false;
            }

            //窗体滚动时,禁用鼠标滚轮
            window.onmousewheel = function () {
                return false;
            };

            //清除计时
            if (document.body.movement) {
                clearTimeout(document.body.movement);
            }

            var currentpos = BlogDirectory.getScrollBarPosition();//获取滚动条当前位置

            var dist = 0;
            if (currentpos == finalpos) {//到达预定位置,则解禁鼠标滚轮,并退出
                window.onmousewheel = function () {
                    return true;
                }
                return true;
            }
            if (currentpos < finalpos) {//未到达,则计算下一步所要移动的距离
                dist = Math.ceil((finalpos - currentpos) / 10);
                currentpos += dist;
            }
            if (currentpos > finalpos) {
                dist = Math.ceil((currentpos - finalpos) / 10);
                currentpos -= dist;
            }

            var scrTop = BlogDirectory.getScrollBarPosition();//获取滚动条当前位置
            window.scrollTo(0, currentpos);//移动窗口
            if (BlogDirectory.getScrollBarPosition() == scrTop)//若已到底部,则解禁鼠标滚轮,并退出
            {
                window.onmousewheel = function () {
                    return true;
                }
                return true;
            }

            //进行下一步移动
            var repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")";
            document.body.movement = setTimeout(repeat, interval);
        },

        htmlDecode: function (text) {
            var temp = document.createElement("div");
            temp.innerHTML = text;
            var output = temp.innerText || temp.textContent;
            temp = null;
            return output;
        },

        /*
        创建博客目录,
        id表示包含博文正文的 div 容器的 id,
        mt 和 st 分别表示主标题和次级标题的标签名称(如 H2、H3,大写或小写都可以!),
        interval 表示移动的速度
        */
        createBlogDirectory: function (id, mt, st, interval) {
            //获取博文正文div容器
            var elem = document.getElementById(id);
            if (!elem) return false;
            //获取div中所有元素结点
            var nodes = elem.getElementsByTagName("*");
            //创建博客目录的div容器
            var divSideBar = document.createElement(‘DIV‘);
            divSideBar.className = ‘sideBarml‘;
            divSideBar.setAttribute(‘id‘, ‘sideBarml‘);
            var divSideBarTab = document.createElement(‘DIV‘);
            divSideBarTab.setAttribute(‘id‘, ‘sideBarTabml‘);
            divSideBar.appendChild(divSideBarTab);
            var h2 = document.createElement(‘H2‘);
            divSideBarTab.appendChild(h2);
            var txt = document.createTextNode(‘目录导航‘);
            h2.appendChild(txt);
            var divSideBarContents = document.createElement(‘DIV‘);
            divSideBarContents.style.display = ‘none‘;
            divSideBarContents.setAttribute(‘id‘, ‘sideBarContentsml‘);
            divSideBar.appendChild(divSideBarContents);
            //创建自定义列表
            var dlist = document.createElement("dl");
            divSideBarContents.appendChild(dlist);
            var num = 0;//统计找到的mt和st
            mt = mt.toUpperCase();//转化成大写
            st = st.toUpperCase();//转化成大写
            // console.log(mt);
            var hList = ["H1", ‘H2‘, ‘H3‘, ‘H4‘, ‘H5‘];
            //遍历所有元素结点
            var next_i=0;
            var h_i = [1, 1, 1, 1, 1];
            for (var i = 0; i < nodes.length; i++) {
                var index=hList.indexOf(nodes[i].nodeName);
                if (index!=-1) {
                    // console.log(hList.indexOf(nodes[i].nodeName))

                    //获取标题文本
                    var nodetext = nodes[i].innerHTML.replace(/<\/?[^>]+>/g, "");//innerHTML里面的内容可能有HTML标签,所以用正则表达式去除HTML的标签
                    nodetext = nodetext.replace(/&nbsp;/ig, "");//替换掉所有的&nbsp;
                    nodetext = BlogDirectory.htmlDecode(nodetext);
                    //插入锚
                    nodes[i].setAttribute("id", "blogTitle" + num);
                    var item;
                    if (index==0){
                        item = document.createElement("dt");
                    } else{
                        item = document.createElement("dd");
                        item.className = ‘dd_h‘+(index+1);
                    }
                    nodetext=h_i[index]+". "+nodetext;
                    // debugger;
                    if (next_i==index){
                        //若是同级标题
                        // nodetext=h_i[index]+". "+nodetext;
                        h_i[index]++;
                    } else if(next_i<index){
                        // nodetext=h_i[index]+". "+nodetext;
                        h_i[index]++;
                        next_i=index;
                    }else{
                        next_i=index;
                        h_i[index]++;
                        for (let j = index+1; j < h_i.length; j++) {
                            h_i[j]=1;
                        }

                    }

                    //创建锚链接
                    var itemtext = document.createTextNode(nodetext);
                    item.appendChild(itemtext);
                    item.setAttribute("name", num);
                    item.onclick = function () { //添加鼠标点击触发函数
                        var pos = BlogDirectory.getElementPosition(document.getElementById("blogTitle" + this.getAttribute("name")));
                        if (!BlogDirectory.moveScrollBar(pos.top, interval)) return false;
                    };

                    //将自定义表项加入自定义列表中
                    dlist.appendChild(item);
                    num++;
                }
            }

            if (num == 0) return false;
            /*鼠标进入时的事件处理*/
            divSideBarTab.onmouseenter = function () {
                divSideBarContents.style.display = ‘block‘;
            }
            /*鼠标离开时的事件处理*/
            divSideBar.onmouseleave = function () {
                divSideBarContents.style.display = ‘none‘;
            }

            document.body.appendChild(divSideBar);
        }

    };

    window.onload = function () {
        /*页面加载完成之后生成博客目录*/
        BlogDirectory.createBlogDirectory("post_body", "h2", "h3", 20);
    }

</script>

在css中设置

#sideBarml{
    font-size: 14px;
    font-family: Arial, Helvetica, sans-serif;
    text-align: left;
    position: fixed;
    top: 40px;
    right: 0px;
    width: auto;
}
#sideBarTabml{
    float: left;
    width: 30px;
    border: 1px solid #e5e5e5;
    border-right: none;
    text-align: center;
    background: #ffffff;
}

#sideBarTabml h2 {
    margin-top: 0px!important;
    padding: 10px;
}

#sideBarContentsml {
   float: left;
    overflow: auto;
    overflow-x: hidden;
    min-height: 96px;
    max-height: 460px;
    border: 1px solid #e5e5e5;
    border-right: none;
    background: #ffffff;
    padding-left: 5px;
}
#sideBarContentsml dd {
    margin: 5px 20px;
}
#sideBarContentsml dd, dt {
    cursor: pointer;
}
#sideBarContentsml dd.dd_h3 {
    padding-left: 10px;
}
#sideBarContentsml dd.dd_h4 {
    padding-left: 20px;
}
#sideBarContentsml dd.dd_h5 {
    padding-left: 30px;
}
#sideBarContentsml dt:hover, #sideBarContentsml dd:hover {
    color: cornflowerblue;
}

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/ziyue7575/p/11409850.html

时间: 2024-08-07 06:04:06

博客园自定义目录的相关文章

博客园自定义CSS样式设置

关于博客园自定义CSS样式设置 关于博客园自定义CSS样式设置 首先,选择一个博客皮肤模板,如下 然后,F12审查元素,可对右侧的元素规则进行自定义更改,然后覆盖原CSS 譬如看看取消掉自定义背景图片是什么样子,同理也可以添加CSS样式代码 如此修改完后,就可在页面定制CSS代码框中填上你所需要修改的代码 下面是我的页面定制CSS代码: 1.html,body{2. color: #807C7C;3. font-family: "Helvetica Neue",Helvetica,Ar

博客园自定义页面风格设计 后续篇(页面设计模式及代码高亮 鼠标点击效果升级)

前言 在之前所写过的博客园自定义页面风格设计篇中,我们已经说明了其中两种风格的页面设计,鼠标图案的修改,公告栏的设置,背景音乐的制作,关于CSS以及用Canvas和requestAnimFrame做动画特效,在本文中我们将教大家制作当前简约的页面制作方法. 只要你们有需求,我会尽量帮助到大家,在此感谢各位广大粉丝的支持和理解,我会尽量做到最好,希望小主们不要吝啬你们的支持和推荐,动动小手顶一顶,非常感谢大家长久的陪伴~~ 全部过程都是在“管理->设置”中完成的,博客皮肤推荐使用Simple Me

为博客园添加目录的方法总结

博客园添加目录的方法主要涉及2个步骤: 1.申请开通js权限 2.添加js脚本到“页脚Html代码” <script language="javascript" type="text/javascript"> // 生成目录索引列表 // ref: http://www.cnblogs.com/wangqiguo/p/4355032.html // modified by: zzq function GenerateContentList() { var

【转】为博客园添加目录的方法总结

本文转自:作者:妙音天女    地址:http://www.cnblogs.com/xuehaoyue/p/6650533.html 目录 第一种:在正文上方直接添加目录 1. 申请开通js权限 2. 添加js脚本到“页脚Html代码” 3. 按格式写文章 第二种:在文章右上角添加目录导航 1. 申请开通js权限 2. 添加css代码到“页面定制CSS代码” 3. 添加js脚本到“页首Html代码” 4. 按格式写文章 参考链接:http://www.cnblogs.com/xdp-gacl/p

博客园生成目录

一.向官方申请Js权限 批准时间很快,成功有邮件提示: 二.将下段代码添加到页脚Html代码中 <script language="javascript" type="text/javascript"> //生成目录索引列表 function GenerateContentList() { var jquery_h3_list = $('#cnblogs_post_body h3'); //博客园的正文的div的id为cnblogs_post_body,

博客园自定义CSS美化 屏蔽广告等

通过自定义CSS,实现在博客园屏蔽广告.美化排版等. 需要禁用模板CSS 1 body{ 2 background: url(http://images.cnblogs.com/cnblogs_com/storyicon/1107385/o_Z.jpg); 3 font-family: "Hiragino Sans GB","Microsoft YaHei",\5FAE\8F6F\96C5\9ED1,tahoma,arial,simsun,\5B8B\4F53; 4

博客园个人主页目录

table标签中thead.tbody.tfoot的作用http://www.cnblogs.com/zhouxinfei/p/8001510.htmlpython selenium模拟滑动操作http://www.cnblogs.com/zhouxinfei/p/8001506.htmlSelenium webdriver定位iframe里面元素http://www.cnblogs.com/zhouxinfei/p/8001496.html[python3.5][PyUserInput]模拟鼠

博客园自定义地址栏logo

自定义博客园地址栏logo 一.首先自己需要下载一个logo图片,png.jpg格式的都可以 .挑选自己喜欢的图片就可以. 二.然后制作成icon图标  在线制作icon图标网站:http://www.bitbug.net/ 三.上传至博客园文件中 四.将以下代码复制到页脚Html中 <script type="text/javascript" language="javascript"> //Setting ico for cnblogs var li

博客园自定义样式

我现在的博客园首页暂时应该是一个女生背景,一些星星的悬浮飘扬 那么我们怎么来设置博客园的自定义的样式,而不是不个性的默认界面呢? 第一步:进入你的首页,打开管理 => 设置 第二步:我在博客皮肤中选择了SimpleMemory,这个皮肤相对来说要轻快.干净,更方便你来修改你的样式. 第三步:事实上你可以禁用默认CSS,这也是允许的,不过就会弄成这样 学过前端的同学应该知道不掺杂任何任何的CSS就会出现这样的情况,这是浏览器默认给定的样式,而不是博客园给的CSS默认样式(所以你已经禁用了博客园的默