微信小程序 实现底部导航栏

效果:
这里写图片描述
html:

<template>        
  <footer class="navigaion" >
    <ul class="ul_nav">
      <li class="li_nav" v-for="(item,index) in tabBar.list" :key="item.text" 
        :style='select==index? "color:"+tabBar.selectedColor : "color:"+tabBar.color'  @click="clickIndex(index)">
        <navigator :url="item.pagePath" open-type="redirect">
          <img :src="select==index?item.selectedIconPath:item.iconPath" alt="">
          <div class="nav_title">{{item.text}}</div>
          </navigator>
      </li>
    </ul>
  </footer>
</template>

js:

<script>
export default {
  data: () => {
    return {
      select: global.app.currentMenu;
      tabBar: {
          color: "#a9b7b7", //未选择时 底部导航文字的颜色
          selectedColor: "blue", //选择时 底部导航文字的颜色
          borderStyle: "white", //底部导航边框的样色
          backgroundColor:'#fff',
          list: [
            {
              selectedIconPath: "/static/images/home1.png", //选中时 图标路径
              iconPath: "/static/images/home.png", //未选择时 图标路径
              pagePath: "pages/index/main", //页面访问地址
              text: "发现"
            },
            {
              selectedIconPath: "/static/images/read1.png",
              iconPath: "/static/images/read.png",
              pagePath: "pages/detail/main",
              text: "阅读"
            },
            {
              selectedIconPath: "/static/images/mine1.png",
              iconPath: "/static/images/mine.png",
              pagePath: "pages/setting/main",
              text: "我的"
            }
          ]
}
    };
  },
  methods: {
    clickIndex(i) {
      // this.select = i;
      this.select = global.app.currentMenu;
    }
  },
  //用户退出页面时发生
  onUnload() {
    select: 0;
  },
  onShow() {
    select: 0;
  }
};
</script>

css:

<style scope>
.navigaion {
  position: fixed;
  z-index: 801;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100rpx;
  background: #fff;
  /* border-top: 1px solid #d9d9d9; */
}
.ul_nav {
  height: 100rpx;
  width: 100%;
  display: flex;
}
.li_nav {
  flex: 1;
  height: 100%;
  float: left;
  width: 33%;
  text-align: center;
  list-style-type: none;
  margin: 0px;
  padding: 0px;
}
.li_nav a {
  flex: 1;
  height: 100%;
}
.li_nav img {
  width: 60rpx;
  height: 60rpx;
}
.nav_title {
  font-size: 24rpx;
}
</style>
要在微信小程序实现底部导航栏,需要按照以下步骤进行操作: 1. 在 app.json 文件中设置底部导航栏的样式和内容。具体来说,在 app.json 文件中的 tabBar 属性中设置导航栏的样式和包含的页面路径,例如: ``` "tabBar": { "color": "#999", "selectedColor": "#007aff", "backgroundColor": "#f7f7f7", "borderStyle": "black", "list": [{ "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/tabbar/home.png", "selectedIconPath": "images/tabbar/home_selected.png" },{ "pagePath": "pages/order/order", "text": "订单", "iconPath": "images/tabbar/order.png", "selectedIconPath": "images/tabbar/order_selected.png" },{ "pagePath": "pages/mine/mine", "text": "我的", "iconPath": "images/tabbar/mine.png", "selectedIconPath": "images/tabbar/mine_selected.png" }] } ``` 2. 在 app.js 文件中添加监听函数,用于处理底部导航栏的点击事件。具体来说,在 app.js 文件中的 onTabItemTap 函数中添加相应的逻辑代码,例如: ``` App({ onLaunch: function () { // ... }, onTabItemTap: function(item) { console.log('tap tab item', item.index, item.pagePath) } }) ``` 3. 在需要使用底部导航栏的页面中添加相应的代码。具体来说,在需要使用底部导航栏的页面中,需要将页面的配置文件中的 navigationBarHidden 属性设置为 true,同时在页面的 WXML 文件中添加一个 tabbar 组件,例如: ``` <tabbar selected="{{selected}}" bindchange="tabbarChange"> <tabbar-item key="home" icon="{{homeIcon}}" selected-icon="{{homeSelectedIcon}}"> <view wx:slot="text">首页</view> </tabbar-item> <tabbar-item key="order" icon="{{orderIcon}}" selected-icon="{{orderSelectedIcon}}"> <view wx:slot="text">订单</view> </tabbar-item> <tabbar-item key="mine" icon="{{mineIcon}}" selected-icon="{{mineSelectedIcon}}"> <view wx:slot="text">我的</view> </tabbar-item> </tabbar> ``` 其中,selected 表示当前选中的 tabbar-item 的 key 值,bindchange 表示 tabbar-item 点击事件的处理函数。 以上就是在微信小程序实现底部导航栏的基本步骤,需要注意的是,开发者需要根据实际需求进行样式和逻辑的调整,以达到最佳的用户体验效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值