tianyun hai 1 ano
pai
achega
fef72c51c2
Modificáronse 4 ficheiros con 331 adicións e 32 borrados
  1. 71 0
      simple-demo/toc/h.css
  2. 60 0
      simple-demo/toc/jquery.autoMenu.css
  3. 166 0
      simple-demo/toc/jquery.autoMenu.js
  4. 34 32
      tmp/tmp.html

+ 71 - 0
simple-demo/toc/h.css

@@ -0,0 +1,71 @@
+body {
+    counter-reset: h1-count h2-count h3-count h4-count h5-count h6-count;
+}
+
+.t h1 {
+    counter-reset: h2-count 0;
+}
+
+.t h2 {
+    counter-reset: h3-count 0;
+}
+
+.t h3 {
+    counter-reset: h4-count 0;
+}
+
+.t h4 {
+    counter-reset: h5-count 0;
+}
+
+.t h5 {
+    counter-reset: h6-count 0;
+}
+
+.t h1:before {
+    display: block;
+    float: left;
+    font-size: 100%;
+    counter-increment: h1-count;
+    content: "C" counter(h1-count) "\00A0";
+}
+
+.t h2:before {
+    display: block;
+    float: left;
+    font-size: 100%;
+    counter-increment: h2-count;
+    content: counter(h2-count) "\00A0";
+}
+
+.t h3:before {
+    display: block;
+    float: left;
+    font-size: 100%;
+    counter-increment: h3-count;
+    content: counter(h2-count) "." counter(h3-count) "\00A0";
+}
+
+.t h4:before {
+    display: block;
+    float: left;
+    font-size: 100%;
+    counter-increment: h4-count;
+    content: counter(h2-count) "." counter(h3-count) "." counter(h4-count) "\00A0";
+}
+
+.t h5:before {
+    display: block;
+    float: left;
+    font-size: 50%;
+    counter-increment: h5-count;
+    content: counter(h2-count) "." counter(h3-count) "." counter(h4-count) "." counter(h5-count) "\00A0";
+}
+
+.t h6:before {
+    display: block;
+    float: left;
+    font-size: 50%;
+    counter-increment: h6-count;
+    content: counter(h2-count) "." counter(h3-count) "." counter(h4-count) "." counter(h5-count) "." counter(h6-count) "\00A0";
+}

+ 60 - 0
simple-demo/toc/jquery.autoMenu.css

@@ -0,0 +1,60 @@
+/* 定制生成博客目录的CSS样式 */
+#uprightsideBar {
+	font-size: 16px;
+	font-family: Arial, Helvetica, sans-serif;
+	text-align: left;
+	position: fixed;
+	/*
+    将div的位置固定到距离top:150px,right:0px的位置,
+    这样div就会处在最右边的位置,距离顶部150px,
+    当然这两个值你可以自己改。
+    */
+	top: 150px;
+	right: 0px;
+	width: auto;
+	height: auto;
+}
+
+#sideBarTab {
+	float: left;
+	width: 25px;
+	box-shadow: 0 0 8px #877788;
+	border-right: none;
+	text-align: center;
+	background: rgb(0, 220, 0);
+}
+
+#sideBarContents {
+	float: left;
+	overflow: auto;
+	overflow-x: hidden;
+!important;
+	width: 200px;
+	min-height: 101px;
+	max-height: 460px;
+	border: 1px solid #e5e5e5;
+	border-right: none;
+	background: #ffffff;
+}
+
+#sideBarContents dl {
+	margin: 0;
+	padding: 0;
+}
+
+#sideBarContents dt {
+	margin-top: 5px;
+	margin-left: 5px;
+}
+
+#sideBarContents dd, dt {
+	cursor: pointer;
+}
+
+#sideBarContents dd:hover, dt:hover {
+	color: #A7995A;
+}
+
+#sideBarContents dd {
+	margin-left: 20px;
+}

+ 166 - 0
simple-demo/toc/jquery.autoMenu.js

@@ -0,0 +1,166 @@
+/*
+       这段代码按H2、H3格式生成两级菜单
+       写博客按H2、H3格式写,不然生成不了
+       Markdown写作按##、###两级目录写
+       当然你也可以改写代码成三级菜单
+       参考:孤傲苍狼    zhang_derek
+       洪卫    2018-5-18
+   */
+let BlogDirectory = {
+    /* 获取元素位置,距浏览器左边界的距离(left)和距浏览器上边界的距离(top)*/
+    getElementPosition: function (ele) {
+        let topPosition = 0;
+        let leftPosition = 0;
+        while (ele) {
+            topPosition += ele.offsetTop;
+            leftPosition += ele.offsetLeft;
+            ele = ele.offsetParent;
+        }
+        return {top: topPosition, left: leftPosition};
+    },
+    /*获取滚动条当前位置 */
+    getScrollBarPosition: function () {
+        let 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);
+        }
+
+        //获取滚动条当前位置
+        let currentpos = BlogDirectory.getScrollBarPosition();
+
+        let 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;
+        }
+
+        let scrTop = BlogDirectory.getScrollBarPosition();//获取滚动条当前位置
+        window.scrollTo(0, currentpos);//移动窗口
+        if (BlogDirectory.getScrollBarPosition() == scrTop)//若已到底部,则解禁鼠标滚轮,并退出
+        {
+            window.onmousewheel = function () {
+                return true;
+            }
+            return true;
+        }
+
+        //进行下一步移动
+        let repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")";
+        document.body.movement = setTimeout(repeat, interval);
+    },
+
+    htmlDecode: function (text) {
+        let temp = document.createElement("div");
+        temp.innerHTML = text;
+        let output = temp.innerText || temp.textContent;
+        temp = null;
+        return output;
+    },
+
+    /*
+    创建博客目录,id表示包含博文正文的 div 容器的 id,
+    mt 和 st 分别表示主标题和次级标题的标签名称(如 H2、H3,大写或小写都可以!),
+    interval 表示移动的速度
+    */
+    createBlogDirectory: function (selector, mt, st, interval) {
+        //获取博文正文div容器
+        let elem = document.querySelector(selector);
+        if (!elem) return false;
+        //获取div中所有元素结点
+        let nodes = elem.getElementsByTagName("*");
+        //创建博客目录的div容器
+        let divSideBar = document.createElement('DIV');
+        divSideBar.className = 'uprightsideBar';
+        divSideBar.setAttribute('id', 'uprightsideBar');
+        let divSideBarTab = document.createElement('DIV');
+        divSideBarTab.setAttribute('id', 'sideBarTab');
+        divSideBar.appendChild(divSideBarTab);
+        let h2 = document.createElement('DIV');
+        divSideBarTab.appendChild(h2);
+        let txt = document.createTextNode('目录导航');
+        h2.appendChild(txt);
+        let divSideBarContents = document.createElement('DIV');
+        divSideBarContents.style.display = 'none';
+        divSideBarContents.setAttribute('id', 'sideBarContents');
+        divSideBar.appendChild(divSideBarContents);
+        //创建自定义列表
+        let dlist = document.createElement("dl");
+        divSideBarContents.appendChild(dlist);
+        let num = 0;//统计找到的mt和st
+        mt = mt.toUpperCase();//转化成大写
+        st = st.toUpperCase();//转化成大写
+        //遍历所有元素结点
+        for (let i = 0; i < nodes.length; i++) {
+            if (nodes[i].nodeName == mt || nodes[i].nodeName == st) {
+                //获取标题文本
+                let nodetext = nodes[i].innerHTML.replace(/<\/?[^>]+>/g, "");//innerHTML里面的内容可能有HTML标签,所以用正则表达式去除HTML的标签
+                nodetext = nodetext.replace(/ /ig, "");//替换掉所有的
+                nodetext = BlogDirectory.htmlDecode(nodetext);
+                //插入锚
+                nodes[i].setAttribute("id", "blogTitle" + num);
+                let item;
+                switch (nodes[i].nodeName) {
+                    case mt:    //若为主标题
+                        item = document.createElement("dt");
+                        break;
+                    case st:    //若为子标题
+                        item = document.createElement("dd");
+                        break;
+                }
+
+                //创建锚链接
+                let itemtext = document.createTextNode(nodetext);
+                item.appendChild(itemtext);
+                item.setAttribute("name", num);
+                //添加鼠标点击触发函数
+                item.onclick = function () {
+                    let 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);
+    }
+
+};

+ 34 - 32
tmp/tmp.html

@@ -1,37 +1,39 @@
-<!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml">
+<!doctype html>
+<html>
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
-    <title></title>
-    <style>
-        body {
-            height:1000px;/*让窗体出现滚动条*/
-        }
-        .fixed {
-            position: fixed;
-            left: 100px;
-            right: 100px;
-            top: 100px;
-            bottom: 100px;
-            width: auto;
-            height: auto;
-            border: 1px solid blue;
-
-        }
-        .absolute {
-            position: absolute;
-            left: 100px;
-            right: 100px;
-            top: 100px;
-            bottom: 100px;
-            width: auto;
-            height: auto;
-            border: 1px solid red;
-        }
-    </style>
+    <meta charset="utf-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
+    <title>jquery根据文章h标签自动生成导航目录</title>
+    <link rel="stylesheet" type="text/css" href="https://web.tianyunperfect.cn/simple/toc/jquery.autoMenu.css">
+    <script src="https://web.tianyunperfect.cn/simple/toc/jquery.autoMenu.js"></script>
 </head>
+
 <body>
-<div class="fixed">fixed定位</div>
-<div class="absolute">absolute定位</div>
+<div class="autoMenu" id="autoMenu" data-autoMenu></div>
+<div id="content">
+    一路黔行,醉美贵州——黔西南、黔南游记
+    <h2>摘要1</h2>
+    <div style="height:800px;">
+        第一天抵达贵阳后,先游青岩古镇,后品酸汤鱼 宿:贵阳;第二天白天游览安顺黄果树后,晚上至黔西南州兴义入住;第三天游览完万峰林和马岭河大峡谷,返回安顺;第四天火车前往荔波游览,晚上住小七孔;第五天上午继续游玩小七孔,下午回到贵阳,晚上航班离开贵州。五天的贵州之行,完全采取坐火车坐班车的旅行方式,安排紧凑,由于第一次来到贵州,黄果树景区之大超出预期,加上火车晚点以及班车停开,虽最终均按计划走完行程,其中的曲曲折折,还是留下深刻的教训
+    </div>
+    <h3>第1天</h3>
+    <div style="height:800px;">第1天内容</div>
+    <h3>第2天</h3>
+    <h4>第212天</h4>
+    <h2>摘要2</h2>
+    <div style="height:800px;">第2天内容</div>
+    <h3>第3天</h3>
+    <div style="height:800px;">第3天内容</div>
+    <h3>第4天</h3>
+    <div style="height:800px;">第4天内容</div>
+    <h3>第5天</h3>
+    <div style="height:800px;">第5天内容</div>
+</div>
+<!-- 生成博客目录的JS代码,两级目录 -->
+<script type="text/javascript">
+    window.onload=function () {
+        BlogDirectory.createBlogDirectory("#content", "h2", "h3", 20);
+    }
+</script>
 </body>
 </html>