当前位置:首页 > 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 菜单:

<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 组件示例:

<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 菜单:

vue菜单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 CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…

vue实现listview

vue实现listview

Vue 实现 ListView 的方法 基础列表渲染 使用 v-for 指令可以轻松实现基础列表渲染。在 Vue 中,v-for 用于遍历数组或对象,生成动态列表。 <template>…

vue 实现路径

vue 实现路径

在 Vue 中实现路径管理通常涉及路由配置、动态路由、嵌套路由等场景,以下是具体实现方法: 基础路由配置 安装 Vue Router 并创建路由实例: npm install vue-router…

vue实现im通信

vue实现im通信

Vue 实现 IM 通信的方法 选择通信协议 WebSocket 是实现实时 IM 通信的常用协议,相比 HTTP 更适合双向通信。可使用原生 WebSocket 或第三方库如 Socket.IO。…