微信小程序入门四: 导航栏样式、tabBar导航栏


实例内容

  • 导航栏样式设置
  • tabBar导航栏

实例一:导航栏样式设置

小程序的导航栏样式在app.json中定义。

这里设置导航,背景黑色,文字白色,文字内容测试小程序

app.json内容:

{
  "pages":[
    "pages/index/index",
    "pages/login/login",
    "pages/logs/logs"
  ],
  "window":{
    "backgroundTextStyle":"red",
    "navigationBarBackgroundColor": "#000",
    "navigationBarTitleText": "测试小程序",
    "navigationBarTextStyle":"#fff"
  }
}

window中的样式说明:

属性类型默认值描述
navigationBarBackgroundColorHexColor000000 导航栏背景颜色,如”#000000”
navigationBarTextStyleStringwhite导航栏标题颜色,仅支持 black/white
navigationBarTitleTextString导航栏标题文字内容
backgroundColorHexColor#ffffff窗口的背景色
backgroundTextStyleStringdark下拉背景字体、loading 图的样式,仅支持 dark/light
enablePullDownRefreshBooleanfalse是否开启下拉刷新

效果:


实例二:tabBar导航栏

tabBar挺好的,可以放置于顶部或者底部,用于不同功能页面的切换。

tabBar同样在app.json中进行定义,看一下我在app.json中对tabBar的相关定义:

  "tabBar": {
    "selectedColor": "#1296db",
    "list": [{
      "pagePath": "pages/index/index",
      "text": "首页",
      "iconPath": "images/ico-home.png",
      "selectedIconPath": "images/ico-home-d.png"
    },{
      "pagePath": "pages/setting/setting",
      "text": "设置",
      "iconPath": "images/ico-setting.png",
      "selectedIconPath": "images/ico-setting-d.png"
    },{
      "pagePath": "pages/help/help",
      "text": "帮助",
      "iconPath": "images/ico-help.png",
      "selectedIconPath": "images/ico-help-d.png"
    }]
  }

效果:

tabBar相关属性定义说明:

属性类型必填默认值描述
colorHexColortab 上的文字默认颜色
selectedColorHexColortab 上的文字选中时的颜色
backgroundColorHexColortab 的背景色
borderStyleStringblacktabbar上边框的颜色, 仅支持 black/white
listArraytab 的列表,详见 list 属性说明,最少2个、最多5个 tab
positionStringbottom可选值 bottom、top

tabBar list定义说明:

属性类型必填说明
pagePathString页面路径,必须在 pages 中先定义
textStringtab 上按钮文字
iconPathString图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px
selectedIconPathString选中时的图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px

tabBar 是一个数组,只能配置最少2个、最多5个 tab,tab 按数组的顺序排序。

图标资源可以从这里获取:www.iconfont.cn


博客名称:王乐平博客

博客地址:http://blog.lepingde.com

CSDN博客地址:http://blog.csdn.net/lecepin


这里写图片描述

微信小程序tabbar 是一个底部导航栏,常用于展示小程序的主要功能页面。以下是实现微信小程序 tabbar 的步骤: 1. 在 app.json 文件中添加 tabBar 字段,定义 tabbar样式和配置。例如: ``` "tabBar": { "color": "#999", "selectedColor": "#07c160", "backgroundColor": "#fff", "borderStyle": "black", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/tabbar/index.png", "selectedIconPath": "images/tabbar/index-active.png" }, { "pagePath": "pages/category/category", "text": "分类", "iconPath": "images/tabbar/category.png", "selectedIconPath": "images/tabbar/category-active.png" }, { "pagePath": "pages/cart/cart", "text": "购物车", "iconPath": "images/tabbar/cart.png", "selectedIconPath": "images/tabbar/cart-active.png" }, { "pagePath": "pages/user/user", "text": "我的", "iconPath": "images/tabbar/user.png", "selectedIconPath": "images/tabbar/user-active.png" } ] } ``` 以上代码定义了一个包含个 tab 的 tabbar,每个 tab 对应一个页面,包括页面路径、页面标题、默认图标和选中图标等。 2. 在 app.js 文件中定义 tabBar 的切换事件,可以在页面之间切换时触发该事件。例如: ``` App({ onTabItemTap(item) { console.log(item.index) console.log(item.pagePath) console.log(item.text) } }) ``` 以上代码定义了一个 onTabItemTap 函数,用于在切换 tab 时输出当前的 tab 索引、页面路径和标题。 3. 在需要添加 tabbar 的页面中,可以使用小程序提供的组件来实现。例如: ``` <view class="container"> <view class="content"> <!-- 页面内容 --> </view> <tabbar></tabbar> </view> ``` 以上代码在页面底部添加了一个 tabbar,可以通过小程序提供的样式类来设置样式和布局。 4. 最后,需要将页面注册为 tabbar 页面,以便在切换 tab 时能够正确地触发 onTabItemTap 事件。例如: ``` Page({ onTabItemTap(item) { console.log(item.index) console.log(item.pagePath) console.log(item.text) }, onLoad: function () { wx.setNavigationBarTitle({ title: '页面标题' }) wx.setTabBarItem({ index: 0, text: '首页' }) } }) ``` 以上代码在页面注册时定义了 onTabItemTap 函数,并使用 wx.setTabBarItem 函数设置了当前 tab 的标题。要注意的是,只有在 app.json 中配置的页面才能被注册为 tabbar 页面。 以上是实现微信小程序 tabbar 的基本步骤,具体的样式和功能可以根据实际需求进行调整。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值