当前位置:首页 > VUE

vue实现文章目录

2026-01-12 03:41:08VUE

Vue 实现文章目录的方法

使用插件自动生成目录

安装 vue-toc 插件:

npm install vue-toc --save

在组件中引入并使用:

<template>
  <div>
    <vue-toc :content="articleContent" />
    <div v-html="articleContent"></div>
  </div>
</template>

<script>
import VueToc from 'vue-toc'
export default {
  components: { VueToc },
  data() {
    return {
      articleContent: '<h1>标题1</h1><h2>子标题1</h2><h3>三级标题</h3>'
    }
  }
}
</script>

手动实现目录组件

创建 TableOfContents.vue 组件:

<template>
  <div class="toc">
    <ul>
      <li v-for="(item, index) in headings" :key="index">
        <a :href="`#${item.id}`">{{ item.text }}</a>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: ['headings'],
  mounted() {
    this.generateHeadings()
  },
  methods: {
    generateHeadings() {
      const article = document.querySelector('.article-content')
      const headings = article.querySelectorAll('h1, h2, h3, h4, h5, h6')
      this.headings = Array.from(headings).map(heading => ({
        id: heading.id || heading.textContent.toLowerCase().replace(/\s+/g, '-'),
        text: heading.textContent,
        level: parseInt(heading.tagName.substring(1))
      }))
    }
  }
}
</script>

动态高亮当前阅读位置

添加滚动监听:

methods: {
  handleScroll() {
    const headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6')
    let currentHeading = null

    headings.forEach(heading => {
      const rect = heading.getBoundingClientRect()
      if (rect.top >= 0 && rect.top < 200) {
        currentHeading = heading
      }
    })

    if (currentHeading) {
      // 更新活动目录项样式
      this.activeId = currentHeading.id
    }
  }
},
mounted() {
  window.addEventListener('scroll', this.handleScroll)
},
beforeDestroy() {
  window.removeEventListener('scroll', this.handleScroll)
}

平滑滚动到锚点

添加平滑滚动效果:

methods: {
  scrollToHeading(id) {
    const element = document.getElementById(id)
    if (element) {
      window.scrollTo({
        top: element.offsetTop - 20,
        behavior: 'smooth'
      })
    }
  }
}

样式优化

添加基本样式:

.toc {
  position: fixed;
  top: 20px;
  right: 20px;
  max-width: 300px;
  padding: 10px;
  background: #f8f8f8;
  border-radius: 4px;
}

.toc ul {
  list-style: none;
  padding-left: 0;
}

.toc li {
  margin: 5px 0;
  padding-left: calc((var(--level) - 1) * 15px);
}

.toc a {
  color: #333;
  text-decoration: none;
}

.toc a:hover, .toc .active {
  color: #42b983;
  font-weight: bold;
}

响应式设计

添加媒体查询适应移动设备:

vue实现文章目录

@media (max-width: 768px) {
  .toc {
    position: static;
    max-width: 100%;
    margin-bottom: 20px;
  }
}

标签: 目录文章
分享给朋友:

相关文章

vue实现目录功能

vue实现目录功能

Vue实现目录功能的方法 基于路由的目录结构 在Vue项目中,可以通过路由配置自动生成目录结构。使用vue-router的routes数组可以定义页面层级关系,结合递归组件渲染目录树。 // rou…

vue实现动态树形目录

vue实现动态树形目录

动态树形目录的实现方法 在Vue中实现动态树形目录通常涉及递归组件和动态数据加载。以下是具体实现步骤: 数据结构设计 树形数据通常采用嵌套结构,每个节点包含子节点数组: const treeDat…

react实现博客文章概要

react实现博客文章概要

实现博客文章概要的几种方法 使用字符串截取 通过截取文章内容的前N个字符作为概要,适用于纯文本内容。注意处理中文字符和截断位置。 function getSummary(content, lengt…

vue 实现目录

vue 实现目录

Vue 实现目录功能 在 Vue 中实现目录功能通常涉及动态生成目录结构,并根据页面内容自动更新目录项。以下是几种常见的实现方法: 使用 vue-router 生成路由目录 通过 vue-route…

vue实现目录

vue实现目录

Vue 实现目录功能 在 Vue 中实现目录功能通常需要结合动态路由、组件递归或第三方库。以下是几种常见方法: 基于路由的目录结构 适用于需要与路由绑定的目录(如文档站点): // router.…

vue实现文章目录

vue实现文章目录

实现文章目录的基本思路 在Vue中实现文章目录,通常需要解析文章的标题(如h1、h2、h3等标签),动态生成目录结构,并支持点击跳转。以下是具体实现方法: 解析文章标题 通过DOM操作或正则表达式提…