当前位置:首页 > VUE

vue实现目录

2026-01-06 23:58:59VUE

Vue 实现目录功能

在 Vue 中实现目录功能通常需要动态生成目录结构,并支持点击跳转到对应内容区域。以下是几种常见实现方式:

基于滚动监听和动态生成

安装依赖(如需):

npm install vue-scrollto

组件实现:

<template>
  <div>
    <div class="toc" v-if="headings.length">
      <ul>
        <li v-for="(heading, index) in headings" :key="index">
          <a @click="scrollTo(heading.id)">{{ heading.text }}</a>
        </li>
      </ul>
    </div>
    <div class="content" v-html="content" ref="content"></div>
  </div>
</template>

<script>
import VueScrollTo from 'vue-scrollto'

export default {
  data() {
    return {
      headings: [],
      content: '<h1 id="section1">Section 1</h1><p>Content...</p><h2 id="section2">Section 2</h2><p>More content...</p>'
    }
  },
  mounted() {
    this.generateTOC()
  },
  methods: {
    generateTOC() {
      const contentEl = this.$refs.content
      const headingEls = contentEl.querySelectorAll('h1, h2, h3, h4, h5, h6')

      this.headings = Array.from(headingEls).map(el => ({
        id: el.id,
        text: el.innerText,
        level: parseInt(el.tagName.substring(1))
      }))
    },
    scrollTo(id) {
      VueScrollTo.scrollTo(`#${id}`, 500)
    }
  }
}
</script>

<style>
.toc {
  position: fixed;
  right: 20px;
  top: 20px;
  background: #f5f5f5;
  padding: 10px;
  border-radius: 4px;
}
.toc ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.toc li {
  margin: 5px 0;
}
.toc a {
  cursor: pointer;
  color: #333;
}
.toc a:hover {
  text-decoration: underline;
}
</style>

使用第三方库

对于更复杂的需求,可以使用专门库如 vue-toc

vue实现目录

安装:

npm install vue-toc

使用示例:

vue实现目录

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

<script>
import VueToc from 'vue-toc'

export default {
  components: { VueToc },
  data() {
    return {
      content: '<h1>Title</h1><h2>Subtitle</h2><p>Content...</p>'
    }
  }
}
</script>

动态路由目录

对于多页面应用的目录,可以结合 Vue Router:

<template>
  <div>
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.title }}</router-link>
      </li>
    </ul>
    <router-view />
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        { path: '/section1', title: 'Section 1' },
        { path: '/section2', title: 'Section 2' }
      ]
    }
  }
}
</script>

响应式目录高亮

添加滚动监听实现当前目录项高亮:

mounted() {
  window.addEventListener('scroll', this.onScroll)
},
methods: {
  onScroll() {
    const headings = document.querySelectorAll('h1, h2, h3')
    let current = ''

    headings.forEach(heading => {
      const rect = heading.getBoundingClientRect()
      if (rect.top <= 100) {
        current = heading.id
      }
    })

    this.activeHeading = current
  }
}

模板中添加高亮类:

<li v-for="heading in headings" :class="{ active: activeHeading === heading.id }">
  <a @click="scrollTo(heading.id)">{{ heading.text }}</a>
</li>

以上方法可根据具体需求选择或组合使用。对于内容动态加载的情况,需要在内容更新后重新调用 generateTOC 方法。

标签: 目录vue
分享给朋友:

相关文章

vue如何实现冒泡

vue如何实现冒泡

Vue 实现冒泡排序 在 Vue 中实现冒泡排序,可以通过数据绑定和响应式特性动态展示排序过程。以下是一个完整的实现示例,包含代码和详细说明。 数据准备 在 Vue 的 data 中定义一个数组作为…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncCom…