当前位置:首页 > 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 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue nexttick实现

vue nexttick实现

Vue 中 nextTick 的实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它的核心实现依赖于 JavaScript 的事件循环机制。…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <tem…

vue广播实现

vue广播实现

vue广播实现 在Vue中实现广播功能可以通过事件总线(Event Bus)或Vuex来完成。以下是两种常见的实现方式: 使用事件总线(Event Bus) 事件总线是一种简单的跨组件通信方式,适用…