对于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、样式优化

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

三、效果展示

相关推荐
光影少年13 小时前
RN原生交互 & 桥接
前端·javascript·react native·react.js·前端框架
张人玉13 小时前
基于 Vue 3 + ECharts + Express + SQLite 构—— LDA 模型的新闻舆论主题识别与情感分析系统
vue.js·echarts·express
东风破_13 小时前
React 状态更新为什么不是立即赋值?从 useState、状态快照到更新队列
前端
无人生还13 小时前
从 Vue3 到 React · 快速上手系列第 4 篇:条件渲染与列表渲染
前端·vue.js·react.js
颜进强13 小时前
从零搭建私人 RAG 实战:用 Markdown 沉淀技术决策与业务决策
前端·后端·ai编程
咩咩啃树皮13 小时前
第46篇:Vue3 Router工程化进阶——懒加载+嵌套路由+全局守卫+权限控制
javascript·vue.js·ecmascript
触底反弹13 小时前
💡 React 父子组件通信:一个进度条教会我的 5 件事
前端·react.js·面试
咩咩啃树皮13 小时前
第44篇:Vue3侦听器完全精讲——watch与watchEffect区别、深度监听、异步业务落地
前端·javascript·vue.js
Python私教13 小时前
前端转 AI 全栈:别只做聊天框,SSE 与审批流才是分水岭
前端·人工智能
晓得迷路了14 小时前
栗子前端技术周刊第 139 期 - Nuxt 4.5、Vue 3.6 RC、Angular 发布节奏...
前端·javascript·vue.js