当前位置:首页 > VUE

vue菜单tab实现

2026-01-14 07:53:38VUE

Vue 菜单 Tab 实现方法

使用动态组件切换内容

通过 Vue 的 component 动态组件结合 v-for 渲染菜单项,点击时切换显示内容:

<template>
  <div>
    <div class="tabs">
      <button 
        v-for="(tab, index) in tabs" 
        :key="index"
        @click="currentTab = tab"
        :class="{ active: currentTab === tab }"
      >
        {{ tab }}
      </button>
    </div>
    <component :is="currentTabComponent" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      tabs: ['Home', 'About', 'Contact'],
      currentTab: 'Home'
    }
  },
  computed: {
    currentTabComponent() {
      return this.currentTab.toLowerCase() + '-component'
    }
  },
  components: {
    'home-component': { template: '<div>Home Content</div>' },
    'about-component': { template: '<div>About Content</div>' },
    'contact-component': { template: '<div>Contact Content</div>' }
  }
}
</script>

<style>
.tabs button.active {
  background: #42b983;
  color: white;
}
</style>

结合路由实现

利用 Vue Router 实现带路由的 Tab 菜单:

vue菜单tab实现

<template>
  <nav>
    <router-link 
      v-for="(tab, index) in tabs" 
      :key="index"
      :to="tab.path"
      active-class="active"
    >
      {{ tab.name }}
    </router-link>
  </nav>
  <router-view />
</template>

<script>
export default {
  data() {
    return {
      tabs: [
        { name: 'Home', path: '/' },
        { name: 'About', path: '/about' },
        { name: 'Contact', path: '/contact' }
      ]
    }
  }
}
</script>

使用第三方 UI 库

Element UI 的 Tab 组件示例:

vue菜单tab实现

<template>
  <el-tabs v-model="activeTab">
    <el-tab-pane 
      v-for="(tab, index) in tabs"
      :key="index"
      :label="tab.label"
      :name="tab.name"
    >
      {{ tab.content }}
    </el-tab-pane>
  </el-tabs>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'home',
      tabs: [
        { label: '首页', name: 'home', content: '首页内容' },
        { label: '关于', name: 'about', content: '关于内容' }
      ]
    }
  }
}
</script>

动画过渡效果

为 Tab 切换添加过渡动画:

<template>
  <div>
    <button 
      v-for="(tab, index) in tabs"
      :key="index"
      @click="currentTab = tab.id"
    >
      {{ tab.title }}
    </button>

    <transition name="fade" mode="out-in">
      <div :key="currentTab">
        {{ tabs.find(t => t.id === currentTab).content }}
      </div>
    </transition>
  </div>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

响应式 Tab 菜单

实现超出宽度自动滚动的 Tab 菜单:

<template>
  <div class="tab-container">
    <div class="tab-scroll">
      <div 
        v-for="(tab, index) in tabs"
        :key="index"
        class="tab-item"
        @click="selectTab(index)"
        :class="{ active: activeIndex === index }"
      >
        {{ tab }}
      </div>
    </div>
  </div>
</template>

<style>
.tab-container {
  overflow-x: auto;
  white-space: nowrap;
}
.tab-item {
  display: inline-block;
  padding: 10px 20px;
  cursor: pointer;
}
.tab-item.active {
  border-bottom: 2px solid #42b983;
}
</style>

标签: 菜单vue
分享给朋友:

相关文章

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue computed 实现

vue computed 实现

computed 的基本用法 在 Vue 中,computed 用于声明计算属性,这些属性基于响应式依赖进行缓存。只有当依赖发生变化时,计算属性才会重新计算。 new Vue({ data: {…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue实现网站

vue实现网站

Vue 实现网站的基本步骤 Vue.js 是一个渐进式 JavaScript 框架,适合构建现代化的单页应用(SPA)或动态网站。以下是使用 Vue 实现网站的基本方法。 初始化 Vue 项目 使用…