小程序自定义tabbar,中间凸起

微信小程序自带tabbar,但无法实现中间按钮凸起样式和功能,因此按照设计重新自定义一个tabbar

1、创建tabbar文件,与pages同级创建一个文件夹,custom-tab-bar,里面按照设计图将底部tabbar样式编写

csharp 复制代码
<view class="tab-bar">
  <view class="tab-bar-border"></view>
  <block wx:for="{{list}}" wx:key="index">
    <view wx:if="{{item.isSpecial}}" class="tab-bar-item" data-name="{{item.text}}" data-path="{{item.pagePath}}" data-click="{{ item.isSpecial || false }}" data-index="{{index}}" bindtap="switchTab">
      <view class="special-image">
        <image class="special-image-pic" mode="widthFix" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
      </view>
      <view style="color: {{selected === index ? selectedColor : color}}" class="special-text tab-text">{{item.text}}</view>
      
    </view>
    <view wx:else class="tab-bar-item" data-name="{{item.text}}" data-path="{{item.pagePath}}" data-click="{{ item.isSpecial }}" data-index="{{index}}" bindtap="switchTab">
      <image class="item-image" mode="widthFix" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
      <view class="tab-text" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
      <view class="num" wx:if="{{item.text == '消息' && message_num != 0}}">{{message_num}}</view>
    </view>
  </block>
</view>

2、在app.js中添加点击事件

csharp 复制代码
  /* 自定义底部按钮切换 */
  getCurrentTabbar(selected, that) {
    if (typeof that.getTabBar === 'function' &&
      that.getTabBar()) {
      if (wx.getStorageSync('openid')) {
        msg_unread().then(res => {
          that.getTabBar().setData({
            selected: selected,
            message_num: res.data.data
          })
        })
      } else {
        that.getTabBar().setData({
          selected: selected
        })
      }
    }
  },

3、在app.json中修改默认tabbar数据结构

csharp 复制代码
  "tabBar": {
    "custom": true,
    "list": [
        {
            "pagePath": "pages/index/index",
            "text": "首页"
        },
        {
            "pagePath": "pages/contact/index",
            "text": "通讯录"
        },
        {
            "pagePath": "pages/release/index",
            "text": "发布"
        },
        {
            "pagePath": "pages/news/index",
            "text": "消息"
        },
        {
            "pagePath": "pages/personal/index",
            "text": "我的"
        }
    ]
},

4、在对应的页面中执行点击事件

csharp 复制代码
  app.getCurrentTabbar(index,this);
  /* index:tabbar对应的index */

如需源码,请点击下载源码,或点击顶部下载按钮

相关推荐
weixin_177297220691 小时前
旧物回收小程序,一键解决旧物处理难题
小程序
*拯6 小时前
小程序弹出层/抽屉封装 (抖音小程序)
小程序
半兽先生6 小时前
uniapp小程序获取手机设备安全距离
小程序·uni-app
weixin_177297220696 小时前
盲盒一番赏小程序系统发展:创新玩法激发市场活力
小程序
ywyy67986 小时前
推客小程序系统开发:全栈式技术解决方案与行业赋能实践
大数据·人工智能·微信小程序·小程序·系统·推客系统·推客小程序
向明天乄6 小时前
uniapp,小程序中实现文本“展开/收起“功能的最佳实践
小程序·uni-app
ywyy67987 小时前
「数智化聚合分销生态系统」定制开发:重构全渠道增长引擎
大数据·搜索引擎·微信小程序·小程序·系统·聚合分销系统·聚合分销
PyAIGCMaster9 小时前
一个完整的项目示例:taro开发微信小程序
微信小程序·小程序·taro
奇妙方程式16 小时前
微信小程序 地图 使用 射线法 判断目标点是否在多边形内部(可用于判断当前位置是否在某个区域内部)
微信小程序·小程序·地图
龙泉寺天下行走16 小时前
Python 翻译词典小程序
python·oracle·小程序