当前位置:首页 > VUE

vue 底部菜单实现

2026-02-17 09:54:49VUE

实现 Vue 底部菜单的方法

使用 vant 组件库的 Tabbar

安装 vant 库后,可以直接使用 van-tabbar 组件实现底部菜单。

vue 底部菜单实现

<template>
  <van-tabbar v-model="active" fixed>
    <van-tabbar-item icon="home-o">首页</van-tabbar-item>
    <van-tabbar-item icon="search">搜索</van-tabbar-item>
    <van-tabbar-item icon="friends-o">好友</van-tabbar-item>
    <van-tabbar-item icon="setting-o">设置</van-tabbar-item>
  </van-tabbar>
</template>

<script>
import { Tabbar, TabbarItem } from 'vant';
export default {
  components: {
    [Tabbar.name]: Tabbar,
    [TabbarItem.name]: TabbarItem,
  },
  data() {
    return {
      active: 0,
    };
  },
};
</script>

自定义底部菜单

如果不想依赖 UI 库,可以手动实现一个底部菜单。

vue 底部菜单实现

<template>
  <div class="bottom-menu">
    <div 
      v-for="(item, index) in menuItems" 
      :key="index" 
      class="menu-item"
      :class="{ 'active': activeIndex === index }"
      @click="activeIndex = index"
    >
      <span>{{ item.label }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: 0,
      menuItems: [
        { label: '首页' },
        { label: '搜索' },
        { label: '好友' },
        { label: '设置' },
      ],
    };
  },
};
</script>

<style>
.bottom-menu {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-around;
  background: #fff;
  padding: 10px 0;
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
}
.menu-item {
  padding: 5px 10px;
  cursor: pointer;
}
.menu-item.active {
  color: #1989fa;
  font-weight: bold;
}
</style>

结合路由实现页面切换

底部菜单通常需要配合 Vue Router 进行页面切换。

<template>
  <div class="bottom-menu">
    <router-link 
      v-for="(item, index) in menuItems" 
      :key="index" 
      :to="item.path"
      class="menu-item"
      active-class="active"
    >
      <span>{{ item.label }}</span>
    </router-link>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        { label: '首页', path: '/' },
        { label: '搜索', path: '/search' },
        { label: '好友', path: '/friends' },
        { label: '设置', path: '/settings' },
      ],
    };
  },
};
</script>

使用 element-uiel-menu

如果项目使用 element-ui,可以利用 el-menuhorizontal 模式实现底部菜单。

<template>
  <el-menu 
    mode="horizontal" 
    class="bottom-menu"
    :default-active="activeIndex"
    @select="handleSelect"
  >
    <el-menu-item index="1">首页</el-menu-item>
    <el-menu-item index="2">搜索</el-menu-item>
    <el-menu-item index="3">好友</el-menu-item>
    <el-menu-item index="4">设置</el-menu-item>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: '1',
    };
  },
  methods: {
    handleSelect(index) {
      console.log('选中菜单:', index);
    },
  },
};
</script>

<style>
.bottom-menu {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-around;
}
</style>

注意事项

  • 固定定位:使用 position: fixed 确保菜单始终固定在底部。
  • 路由匹配:如果结合 Vue Router,确保 active-classactive 样式正确匹配当前路由。
  • 兼容性:在移动端需考虑 safe-area-inset-bottom 以避免被设备底部遮挡。

以上方法可根据项目需求选择适合的实现方式。

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

相关文章

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue实现flbook

vue实现flbook

Vue 实现类似 Flbook 的翻页效果 要实现类似 Flbook 的翻页效果,可以使用 Vue 结合 CSS 动画和 JavaScript 事件处理。以下是实现方法: 安装依赖 需要安装 vue…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…