对于vue中v-html渲染的文章内容,实现其目录功能

一、思路

主要有以下几个步骤:

  1. 获取文章内容;
  2. 将文章内容渲染到页面;
  3. 根据文章内容生成目录;
  4. 渲染目录到页面;
  5. 实现目录锚点跳转;
  6. 样式优化。

二、实现

2.1、获取文章内容

xml 复制代码
content: '<h1>第一步</h1><h2>1.1、代码块展示</h2><pre class="ql-syntax line-numbers language-js" spellcheck="false"><code class="language-js">var str = "hello world!"</code></pre><h2>1.2、666</h2><p>66666666</p><h1>第二步</h1><p>给个赞呗!!!!!!!!!!!!</p><h1>第三步</h1><p>你学废了吗?</p>'  

2.2、将文章内容渲染到页面

arduino 复制代码
<div class="detail_con" :style="loading ? 'height:200px;' : 'height:auto;'" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
     <div v-highlight ref="blogContentRef" @click="clickImage" v-html="blogInfo.content"></div>
</div>

2.3、根据文章内容生成目录

javascript 复制代码
// 获取博客文章目录
getTitle() {
    // 生成目录
    this.$nextTick(() => {
        // 根据ref来获取文章的标题
        const articleContent = this.$refs.blogContentRef
        // 想获取的标题,将想获取的标题的标签添加到这
        const titleTag = ['H1', 'H2', 'H3']
        // 存放标题的数组
        const titles = []
        // 遍历标题节点
        articleContent.childNodes.forEach((e, index) => {
          // 找出需要做成目录的标题并在内容中给这些标题添加id属性(可用于锚点跳转)
          if (titleTag.includes(e.nodeName)) {
            // 具体封装过程
            const id = 'header-' + index
            // 设置元素id
            e.setAttribute('id', id)
            // 最终目录
            titles.push({
              id: id,
              title: e.innerHTML,
              level: Number(e.nodeName.substring(1, 2)),
              nodeName: e.nodeName,
              // read:后期主要用来判断当前阅读的是哪一个小标题的内容
              read: false
            })
          }
        })
        // 最终目录赋值给catalog
        this.catalog = titles
        // loading
        this.loading = false
    })
},

2.4、渲染目录到页面

xml 复制代码
<div class="directory">
   <h2>目录</h2>
     <ul>
         <template v-for="(item, index) in catalog">
            <!---根据read属性,动态绑定样式:为true即为正在阅读此内容--->
            <li :key="index" :style="{ paddingLeft: item.level * 30 - 44 + 'px' }" :class="{ ll: item.read == true }">
               <!---设置href,用于目录的跳转;动态绑定id,判断当前正在阅读的是哪一个小标题的内容--->
               <a :id="item.id" @click="goAnchor(index, '#' + item.id)" :class="{ cc: item.read == true }" v-html="item.title"></a>
            </li>
        </template>
    </ul>
</div>

2.5、实现目录锚点跳转

php 复制代码
// 目录锚点跳转
goAnchor(index, id) {
   // 被点击的目录对应内容中的标题样式改变
   for (let i = 0; i < this.catalog.length; i++) {
      if (index === i) {
         this.catalog[i].read = true
         document.getElementById(this.catalog[i].id).style.color = '#0eba8a'
      } else {
         this.catalog[i].read = false
         document.getElementById(this.catalog[i].id).style.color = '#666'
      }
   }

   // 参数是id选择器
   document.querySelector(id).scrollIntoView({
      behavior: 'smooth', // 动画过渡效果-behavior: "auto" 或 "smooth" 默认为 "auto"
      block: 'center', // 垂直方向对齐-block: "start", "center", "end", 或 "nearest" 默认为 "start"
      inline: 'center' // 水平方向对齐-inline: "start", "center", "end", 或 "nearest" 默认为 "nearest"
   })
},

2.6、样式优化

可根据自身情况进行调整。

三、效果展示

相关推荐
Doris_20231 小时前
说一说ESLint+Prettier生效的原理
前端·设计模式·架构
ZC跨境爬虫1 小时前
跟着 MDN 学CSS day_21:(图像溢出控制与表单元素样式定制)
前端·javascript·css·ui·交互
卷帘依旧1 小时前
微前端解决方案-qiankun
前端
moshuying1 小时前
你做的,比汇报出来的多得多
前端
shuye2161 小时前
google chrome 离线下载地址
前端·chrome
一 乐1 小时前
疫苗发布和接种预约|基于Java+vue疫苗发布和接种预约系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·疫苗发布和接种预约系统系统
yqcoder1 小时前
闭包是什么?优缺点、怎么防内存泄漏?
前端·http
lichenyang4531 小时前
鸿蒙 ArkUI 组件基础复盘:从两个 UI 卡片回到 ComponentV2、状态管理和组件分层
前端
biubiubiu_LYQ2 小时前
萌新小白基础理解篇之 this 关键字
前端·javascript
光影少年2 小时前
Redux 中间件作用(redux-thunk/redux-saga)
前端·react.js·掘金·金石计划