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

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

三、效果展示

相关推荐
祈澈菇凉3 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js
小纯洁w4 小时前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
想睡好4 小时前
css文本属性
前端·css
qianmoQ4 小时前
第三章:组件开发实战 - 第五节 - Tailwind CSS 响应式导航栏实现
前端·css
zhoupenghui1684 小时前
golang时间相关函数总结
服务器·前端·golang·time
White graces5 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea
庸俗今天不摸鱼5 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
bubusa~>_<5 小时前
解决npm install 出现error,比如:ERR_SSL_CIPHER_OPERATION_FAILED
前端·npm·node.js
yanglamei19625 小时前
基于Python+Django+Vue的旅游景区推荐系统系统设计与实现源代码+数据库+使用说明
vue.js·python·django
流烟默6 小时前
vue和微信小程序处理markdown格式数据
前端·vue.js·微信小程序