当前位置:首页 > uni-app

uniapp做底部导航

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

底部导航实现方法

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

使用原生导航栏

uniapp做底部导航

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

uniapp做底部导航

{
  "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设置title

uniapp设置title

设置页面标题的方法 在UniApp中设置页面标题可以通过以下几种方式实现,适用于不同场景和需求。 通过pages.json配置 在pages.json文件中,可以为每个页面单独配置导航栏标题。找到对…

uniapp使用axios无法请求

uniapp使用axios无法请求

uniapp中使用axios请求问题解决方案 在uniapp中使用axios可能会遇到跨域、请求失败或兼容性问题。以下是常见原因及解决方法: 检查axios安装与引入 确保已正确安装axios:…

uniapp隐藏右侧滚动条

uniapp隐藏右侧滚动条

隐藏右侧滚动条的方法 在UniApp中隐藏右侧滚动条可以通过CSS样式实现,适用于H5、小程序等平台。以下是几种常见的方法: 全局样式设置 在App.vue的全局样式中添加以下CSS代码,适用于所有…

uniapp 绘图

uniapp 绘图

uniapp 绘图方法 使用 Canvas 组件 uniapp 支持通过 canvas 组件实现绘图功能。在模板中声明 canvas 并设置宽度、高度和 ID,通过 JavaScript 调用绘图 A…

uniapp 滑动

uniapp 滑动

uniapp 实现滑动效果的方法 在uniapp中实现滑动效果可以通过多种方式,包括使用内置组件、CSS动画或第三方插件。以下是几种常见的方法: 使用scroll-view组件 scroll-vi…

uniapp探探

uniapp探探

uniapp实现探探滑动卡片效果 探探的滑动卡片效果主要包含卡片堆叠、左右滑动操作、动画反馈等功能。在uniapp中可以通过以下方式实现类似交互: 使用swiper组件实现基础滑动 uniapp的s…