联系官方销售客服

1835022288

028-61286886

分享经验 版主:论坛审计组
使用JS,文章内容页根据H标签自动生成目录
类型:迅睿CMS 更新时间:2024-03-16 10:33:45 自动识别

获取文章所有的h标签,生成目录,增加跳转js

点击 当前页内跳转 

自动识别当前位置 进行赋值active


个人比较菜,扣了半天才搞出来,拿去参考,有不足之处大家帮忙指正


右侧目录代码 主要是id

<div class="col-lg-3 col-md-12 col-12">
      <div class="header">
  <h6 class="title title-1">目录</h6>
   </div>
    <div id="groupfile">
                               
    </div>
</div>

左侧文章代码

 <div class="entry-wrapper">
         <div class="entry-content u-text-format u-clearfix" id="content">
              {$content}    
     </div>    
     </div>

js代码 

// 首先根据id获取id下所有H标签
var content=document.getElementById("content"); 
var headList = [...content.querySelectorAll('h1,h2,h3,h4,h5,h6')];
  // 将获取的H标签构造成树
  var root = {
      children: []
  };
  var h = {
      node: headList[0],
      children: [],
      parent: root
  };
  root.children.push(h);
  headList.reduce(function (pre, cur) {
      var n = {
          node: cur,
          children: []
      }
      while (h.node.localName[1] - n.node.localName[1] !== -1) {
          h = h.parent;
          if (h === root) {
              break;
          }
      }
      n.parent = h;
      h.children.push(n);
      h = n;
      return h;
  });
  // 给H标签加id
  var index = 0;
  function createList(list) {
      var text = list.reduce(function (pre, cur) {
           cur.node.id = 'header' + index++;
          var childText // 判断该H标签有没有子层H标签
          if (cur.children.length) {
              childText = createList(cur.children)
          } else {
              childText = ''
          }
         
          pre += ` 
                  <li class="groupfile-list">
                 <a class="catalog-tag" href="#${cur.node.id}">
                   ${cur.node[xss_clean]}
                   </a>
                 ${childText}
                </li>
               
                `
                //目录的列表
          return pre;
      }, '');
      var text = ` <ul class=""> ${text}  </ul>`
      return text;
  }
//循环出目录
  var content = createList(root.children);
  document.getElementById('groupfile')[xss_clean] = content;
  
 var left_div_top = 100; //左侧边栏距离顶部高度
    var right_nav_top = 150;     //右侧标题距离浏览器窗口顶部距离(距离顶部多少就切换左侧标题高亮)
    var groupfile_list = $("#groupfile .groupfile-list>a"); // 获取左侧标题列表dome
    var groupfile_list1 = $("#groupfile .groupfile-list"); // 获取左侧标题列表dome,用来给li加active
        //滚动条事件
        $(window).scroll(function () {
            var windouWidth = $(this).width();//获取浏览器自身宽度
            var windouHeight = $(this).height();//获取浏览器自身高度
            //获取滚动条的滑动距离
            var scroH = $(this).scrollTop();
            // 滚动js
            if ($(".main").offset()) {
                // js判断标题高亮
                $(headList).each(function (index, element) {
                    if ($("#header" + (index)).offset().top + windouHeight - right_nav_top < scroH + windouHeight) {
                        $(groupfile_list1[index-1]).removeClass("active"),
                        $(groupfile_list1[index]).addClass("active")
                    } else {
                        $(groupfile_list1[index]).removeClass("active")
                    }
                });
                
             
            }
        });
        // 目录跳转js
        groupfile_list.click(function (e) {
            e.preventDefault();
            var annname = $(this).attr("href");
            $('html, body').animate({ scrollTop: $(annname).offset().top - left_div_top }, 100)
        });  

image

回帖
  • 阿陆来
    #1楼    阿陆来
    2023-02-22 09:57:43
    Chrome 0
    感谢你的技术分享!!!很不错!
  • 又菜又爱玩
    #2楼    又菜又爱玩
    2023-02-22 10:20:48
    Edge 0
    补充:左侧粘性自己搞,我是用的是开源jq插件 GitHub - garand/sticky: jQuery Plugin for Sticky Objects
  • 善建者
    #3楼    善建者
    2023-02-23 22:59:42
    Chrome 0
    我也遇到了这个问题,感谢分享!
  • yudeleishu
    #4楼    yudeleishu
    2023-02-24 10:07:37
    Chrome 0
    感谢你的技术分享!
  • 麦没有兜
    #5楼    麦没有兜
    2023-06-29 15:53:21
    Chrome 0
    感谢你的技术分享!
  • Hi
    #6楼    Hi
    2023-07-04 14:09:15
    Chrome 0
    感谢你的技术分享!
  • demo1452
    #7楼    demo1452
    2023-07-06 21:50:29
    Chrome 0
    看看如何使用
  • 张先生
    #8楼    张先生
    2023-07-20 09:01:09
    Chrome 0
    大佬 厉害了。
  • 天客隆模板网
    #9楼    天客隆模板网
    2023-07-26 20:17:51
    Chrome 0
    看看如何使用
  • 屺无心
    #10楼    屺无心
    2023-08-24 10:29:51
    Chrome 0
    感谢你的技术分享,学习学习!!!
  • 嘟嘟小将-爆仓专业户
    #11楼    嘟嘟小将-爆仓专业户
    2023-08-25 20:35:14
    Chrome 0
    感谢你的技术分享!
  • chaoyong
    #12楼    chaoyong
    2023-11-30 13:13:50
    Chrome 0
    感谢大佬分享!
  • heimeng
    #13楼    heimeng
    2023-12-11 21:20:57
    Chrome 0
    看看咋写的
  • 白嫖怪
    #14楼    白嫖怪
    2023-12-19 15:44:23
    Chrome 0
    让我康康怎么个事儿
  • 洛沙
    #15楼    洛沙
    2024-01-04 22:05:25
    Chrome 0
    😀 😎 看看,学习下
  • cuinew520
    #16楼    cuinew520
    2024-03-05 23:41:42
    Chrome 0
    感谢您的无私奉献!
  • cuinew520
    #17楼    cuinew520
    2024-03-05 23:42:42
    Chrome 0
    又菜又爱玩 感谢您的无私奉献
  • 学无止境
    #18楼    学无止境
    2024-03-16 10:33:45
    Chrome 0
    看看怎么个思路,谢谢。