当前位置:首页 > uni-app

uniapp做底部导航

2026-03-05 10:50:34uni-app

底部导航实现方法

在UniApp中实现底部导航通常使用uni-app提供的原生导航栏组件或自定义组件。以下是两种常见的方法:

使用原生导航栏

pages.json中配置tabBar属性,这是最简单的方式:

{
  "pages": [
    {
      "path": "pages/home/home",
      "style": {
        "navigationBarTitleText": "首页"
      }
    },
    {
      "path": "pages/category/category",
      "style": {
        "navigationBarTitleText": "分类"
      }
    },
    {
      "path": "pages/cart/cart",
      "style": {
        "navigationBarTitleText": "购物车"
      }
    },
    {
      "path": "pages/user/user",
      "style": {
        "navigationBarTitleText": "我的"
      }
    }
  ],
  "tabBar": {
    "color": "#999",
    "selectedColor": "#007AFF",
    "backgroundColor": "#fff",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/home/home",
        "text": "首页",
        "iconPath": "static/tabbar/home.png",
        "selectedIconPath": "static/tabbar/home-active.png"
      },
      {
        "pagePath": "pages/category/category",
        "text": "分类",
        "iconPath": "static/tabbar/category.png",
        "selectedIconPath": "static/tabbar/category-active.png"
      },
      {
        "pagePath": "pages/cart/cart",
        "text": "购物车",
        "iconPath": "static/tabbar/cart.png",
        "selectedIconPath": "static/tabbar/cart-active.png"
      },
      {
        "pagePath": "pages/user/user",
        "text": "我的",
        "iconPath": "static/tabbar/user.png",
        "selectedIconPath": "static/tabbar/user-active.png"
      }
    ]
  }
}

自定义底部导航

如果需要更灵活的样式或功能,可以自定义底部导航组件:

  1. 创建组件文件components/tabbar/tabbar.vue
<template>
  <view class="tabbar">
    <view 
      v-for="(item, index) in list" 
      :key="index" 
      class="tabbar-item"
      :class="{ active: currentIndex === index }"
      @click="switchTab(index, item.pagePath)"
    >
      <image :src="currentIndex === index ? item.selectedIconPath : item.iconPath" />
      <text>{{ item.text }}</text>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    currentIndex: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      list: [
        {
          pagePath: "/pages/home/home",
          text: "首页",
          iconPath: "/static/tabbar/home.png",
          selectedIconPath: "/static/tabbar/home-active.png"
        },
        {
          pagePath: "/pages/category/category",
          text: "分类",
          iconPath: "/static/tabbar/category.png",
          selectedIconPath: "/static/tabbar/category-active.png"
        },
        {
          pagePath: "/pages/cart/cart",
          text: "购物车",
          iconPath: "/static/tabbar/cart.png",
          selectedIconPath: "/static/tabbar/cart-active.png"
        },
        {
          pagePath: "/pages/user/user",
          text: "我的",
          iconPath: "/static/tabbar/user.png",
          selectedIconPath: "/static/tabbar/user-active.png"
        }
      ]
    }
  },
  methods: {
    switchTab(index, path) {
      if (this.currentIndex === index) return
      uni.switchTab({
        url: path
      })
    }
  }
}
</script>

<style>
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  height: 50px;
  background: #fff;
  box-shadow: 0 -1px 5px rgba(0,0,0,0.1);
}
.tabbar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.tabbar-item image {
  width: 24px;
  height: 24px;
}
.tabbar-item text {
  font-size: 12px;
  margin-top: 4px;
}
.active {
  color: #007AFF;
}
</style>
  1. 在页面中使用自定义组件:
<template>
  <view>
    <!-- 页面内容 -->
    <tabbar :currentIndex="currentIndex" />
  </view>
</template>

<script>
import tabbar from '@/components/tabbar/tabbar.vue'
export default {
  components: {
    tabbar
  },
  data() {
    return {
      currentIndex: 0
    }
  }
}
</script>

注意事项

  • 原生tabBar只支持最多5个tab
  • 自定义导航需要处理页面切换逻辑
  • 在H5端自定义导航需要考虑浏览器底部工具栏的遮挡问题
  • 图标建议使用24x24或28x28像素大小
  • 需要为每个tab页面创建对应的页面文件

样式调整技巧

  • 可以通过修改tabBarborderStyle属性来改变顶部边框样式
  • 使用selectedColor控制选中状态的颜色
  • 自定义导航可以添加动画效果提升用户体验
  • manifest.json中可以配置全局的导航栏样式

这两种方法都能实现底部导航功能,原生方式更简单但灵活性较低,自定义方式可以完全控制样式和行为但需要更多代码。

uniapp做底部导航

标签: uniapp
分享给朋友:

相关文章

uniapp开发

uniapp开发

uniapp开发简介 uniapp是一款基于Vue.js的跨平台开发框架,支持一次开发,多端部署。开发者可以通过编写一套代码,发布到iOS、Android、Web以及各种小程序平台(如微信、支付宝、百…

uniapp消息推送

uniapp消息推送

uniapp消息推送实现方法 uniapp支持多种消息推送方式,包括uniPush、个推、极光推送等。以下为常见实现方案: uniPush(官方推荐) uniapp官方提供的推送服务,基于DClo…

uniapp使用npm

uniapp使用npm

uniapp中使用npm的方法 uniapp支持通过npm安装和管理第三方依赖包,以下是具体操作步骤: 安装Node.js环境 确保本地已安装Node.js(建议使用LTS版本),安装后会自动包含…

uniapp多环境配置

uniapp多环境配置

多环境配置的必要性 在UniApp开发中,多环境配置能有效区分开发、测试、生产等不同环境的API地址、密钥等参数,避免手动修改代码导致的错误。 创建环境配置文件 在项目根目录下创建env.js或co…

uniapp实现懒加载

uniapp实现懒加载

uniapp实现懒加载的方法 在uniapp中实现懒加载可以通过多种方式,以下是几种常见的方法: 使用uni.lazyLoad组件 uniapp提供了内置的懒加载组件,适用于图片等资源的懒加载。在…

uniapp接入

uniapp接入

接入UniApp的基本流程 UniApp是一个基于Vue.js的跨平台开发框架,支持一次开发,多端发布。以下是接入UniApp的基本流程: 环境准备 确保已安装Node.js(建议版本12+)和HB…