当前位置:首页 > 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 组件:

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)
}

平滑滚动到锚点

添加平滑滚动效果:

vue实现文章目录

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;
}

响应式设计

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

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

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

相关文章

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

vue 实现目录树

vue 实现目录树

Vue 实现目录树的方法 在 Vue 中实现目录树功能可以通过多种方式完成,以下是一些常见的方法: 使用递归组件实现目录树 递归组件是 Vue 中实现树形结构的常见方法,通过组件自身调用自身来实现多…

vue实现树形目录

vue实现树形目录

Vue 实现树形目录的方法 递归组件实现 递归组件是Vue中实现树形结构的常见方法,通过组件调用自身实现无限层级嵌套。 <template> <ul> <l…

vue实现动态树形目录

vue实现动态树形目录

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

实现目录js

实现目录js

实现目录功能的方法 使用JavaScript动态生成目录可以通过以下方式实现: 获取标题元素 遍历文档中的标题元素(如h1、h2、h3等),收集它们的文本内容和层级信息。 创建目录结构 根据标题的…

uniapp我的文章

uniapp我的文章

uniapp 文章管理实现方法 数据绑定与列表渲染 在 data 中定义文章列表数组,通过 v-for 渲染到页面。示例代码: data() { return { articles:…