当前位置:首页 > 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 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue简单实现

vue简单实现

Vue 简单实现示例 以下是一个使用 Vue.js 创建简单应用的示例,涵盖基本功能如数据绑定、事件处理和组件化。 基础 HTML 结构 <!DOCTYPE html> <ht…

vue实现项目

vue实现项目

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 创建 Vue 项目 使用 Vue CLI 快…

vue实现rgb调色

vue实现rgb调色

Vue实现RGB调色功能 实现思路 通过Vue的数据绑定和事件处理,动态调整RGB三个通道的值,实时更新显示颜色。 核心代码示例 <template> <div class=…

不用vue实现spa

不用vue实现spa

使用原生 JavaScript 实现 SPA 通过监听 URL 变化动态加载内容,结合 history.pushState 或 hashchange 事件实现路由切换。 // 路由配置 const…

vue实现共享动画

vue实现共享动画

Vue 共享动画实现方法 使用 Vue Transition 组件 Vue 内置的 Transition 组件可以实现元素进入/离开的过渡效果。通过命名过渡和 mode 属性可以控制多个元素的过渡顺序…