小程序自定义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 */

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

相关推荐
2501_9151063212 小时前
iOS 抓不到包怎么办?从 HTTPS 代理排查到 TCP 数据流捕获的全链路解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
游戏开发爱好者812 小时前
APP上架苹果应用商店经验教训与注意事项
android·ios·小程序·https·uni-app·iphone·webview
hyswl66614 小时前
数字货物搬家小程序
python·小程序
郭俊强15 小时前
小程序RSA、AES加密
小程序
2501_9160074716 小时前
苹果应用商店上架的系统逻辑,从产品开发到使用 开心上架 上架IPA 交付审核流程
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088917 小时前
Python抓包HTTPS详解:Wireshark、Fiddler、Charles等工具使用教程
python·ios·小程序·https·uni-app·wireshark·iphone
刻刻帝的海角17 小时前
uniapp引入qqmap-wx-jssdk实现微信小程序端获取用户当前位置
微信小程序·小程序·uni-app
2501_9160088918 小时前
uni-app 上架到 App Store 的项目流程,构建、打包与使用开心上架(Appuploader)上传
android·ios·小程序·https·uni-app·iphone·webview
桐溪漂流18 小时前
微信小程序路由及 `EventChannel` 通信
微信小程序·小程序
00后程序员张18 小时前
怎么在 iOS 上架 App,从构建端到审核端的全流程协作解析
android·macos·ios·小程序·uni-app·cocoa·iphone