前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JS生成HTML的目录

JS生成HTML的目录

作者头像
码客说
发布2024-03-29 13:01:51
940
发布2024-03-29 13:01:51
举报
文章被收录于专栏:码客码客

前言

生成目录结构

代码语言:javascript
复制
function getCatalog(className){
    let showDom = document.querySelector("."+className);
    // 获取所有标题元素
    const headings = showDom.querySelectorAll('h1, h2, h3, h4, h5, h6');
    let num = 0;
    let titleArr = Array.from(headings).map(heading=>{
        num+=1;
        let cName = "z_catalog_"+num;
        heading.classList.add(cName);
        const level = parseInt(heading.tagName.charAt(1));
        const title = heading.textContent;
        return {
            level:level,
            title:title,
            className:cName
        }
        });
    return titleArr
}

let catalogArr = getCatalog("v-show-content");
console.info(catalogArr);

滚动到指定位置

代码语言:javascript
复制
function scrollToDiv(className) {
  let mDom = document.querySelector("."+className);
  mDom.scrollIntoView({ behavior: "smooth" });
}

scrollToDiv("z_catalog_5")

Vue项目中对接

模板

代码语言:javascript
复制
<div class="catalog" v-show="showCatalog">
  <div class="catalog_title">
    <div v-show="showCatalog">目录</div>
    <div class="catalog_btn" @click="showCatalog = !showCatalog">
      <Icon type="md-arrow-round-forward" />
    </div>
  </div>
  <div class="catalog_content" v-show="showCatalog">
    <div
      v-for="(catalog, index) in catalogArr"
      :key="index"
      class="catalog_item"
      :style="{ 'margin-left': (catalog.level - 1) * 16 + 'px' }"
      @click="scrollToDiv(catalog.className)"
    >
      {{ catalog.title }}
    </div>
  </div>
</div>

<div class="show_catalog" v-show="!showCatalog" @click="showCatalog = true">
  <Icon type="md-arrow-round-back" />
</div>

JS

代码语言:javascript
复制
{
  data() {
    return {
      showCatalog: true,
      catalogArr: [],
    };
  },
  async mounted() {
    this.init();
  },
  methods: {
    async init() {
      await this.$nextTick();
      this.catalogArr = this.getCatalog("v-show-content");
    },
    getCatalog(className) {
      let showDom = document.querySelector("." + className);
      // 获取所有标题元素
      const headings = showDom.querySelectorAll(
        "h1, h2, h3, h4, h5, h6"
      );
      let num = 0;
      return Array.from(headings).map((heading) => {
        num += 1;
        let cName = "z_catalog_" + num;
        heading.classList.add(cName);
        const level = parseInt(heading.tagName.charAt(1));
        const title = heading.textContent;
        return {
          level: level,
          title: title,
          className: cName,
        };
      });
    },
    scrollToDiv(className) {
      let mDom = document.querySelector("." + className);
      mDom.scrollIntoView({ behavior: "smooth" });
    },
  },
};

CSS

代码语言:javascript
复制
.catalog {
  position: absolute;
  width: 200px;
  max-height: 60%;
  box-shadow: 4px 4px 10px 2px #ddd;
  top: 170px;
  right: 20px;
  z-index: 20;
  background: white;
  display: flex;
  flex-direction: column;
  border-radius: 8px;
  overflow: hidden;

  .catalog_title {
    font-weight: bold;
    padding: 6px 10px 6px 14px;
    flex: none;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .catalog_btn {
      font-weight: normal;
      cursor: pointer;
      border-radius: 20px;
      padding-left: 10px;
      font-size: 20px;
    }

    .catalog_btn:hover {
      color: dodgerblue;
    }
  }

  .catalog_content {
    flex: auto;
    overflow-y: auto;
    padding: 20px;
    border-top: 1px solid #ddd;

    .catalog_item {
      cursor: pointer;
      padding-bottom: 6px;
    }

    .catalog_item:hover {
      color: dodgerblue;
    }
  }
}

.show_catalog {
  position: absolute;
  height: 40px;
  width: 40px;
  background: white;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 10px 0 0 10px;
  box-shadow: 4px 4px 10px 2px #ddd;
  font-size: 20px;
}

.show_catalog:hover {
  background: var(--primary);
  color: white;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
    • 生成目录结构
      • 滚动到指定位置
      • Vue项目中对接
        • 模板
          • JS
            • CSS
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档