当前位置:首页 > 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 Router 使用 npm 或 yarn 安装 Vue Router 依赖包: npm install vue-router # 或 yarn add vue-…

vue实现选区创建

vue实现选区创建

Vue 实现选区创建的方法 在 Vue 中实现选区创建通常涉及 DOM 操作和事件处理。以下是几种常见的方法: 使用原生 DOM API 通过 window.getSelection() 和 Ran…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &l…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tab…