微信小程序底部导航栏中间突出

站在前人基础上研究 =>大神原文,我是在此基础上自己改造的。

业务需求

中间的突出,点击就跳转去别的页面添加
在这里插入图片描述

知识点

  • 微信自定义底部导航栏页面之间跳转是要用wx.switchTab,但是业务需求我不想跳转到tab页我只想wx.navigateTo去别的(没有tabbar的)页面,你可能会说wx.hideTabBar隐藏tabBar啊,但是这样返回页面会有问题。所以可以借助微信路由组件<navigator/>
  • 但是如果想要中间的那个可以用wx.navigateTo,在app.json里的tabbar对象就不要把中间那页加上去,不然会被当做tab页无法使用wx.navigateTo

app里代码

这里只放另外的四页,中间那页别放
在这里插入图片描述

直接自定义tabbar

在这里插入图片描述

<template>
  <view class="customTabbar" wx:if="{{tabBarShow}}" wx:class="{{{safeDistance:!bottomNum}}}">
    <view class="cell" bindtap="onTabBarChange('user')">
      <view class="imgBox">
        <van-icon
          class="customIcon {{tabBarActive === 'user' ? 'activeCor' : ''}}"
          size="26"
          name="wap-home"
        />
      </view>
      <text class="title {{ tabBarActive === 'user' ? 'activeCor' : '' }}">首页</text>
    </view>
    <view class="cell" bindtap="onTabBarChange('tab2')">
      <view class="imgBox">
        <van-icon
          class="customIcon {{tabBarActive === 'tab2' ? 'activeCor' : ''}}"
          size="26"
          name="chart-trending-o"
        />
      </view>
      <text class="title {{ tabBarActive === 'tab2' ? 'activeCor' : '' }}">tab页2</text>
    </view>

    <!-- switchTab -->
    <!-- <view class="cell" bindtap="onTabBarChange('mid')">
      <view class="imgBox">
        <image src="../assets/images/icon_release.png" mode="aspectFill" class="img_icon" />
      </view>
      <text class="title {{ tabBarActive === 'mid' ? 'activeCor' : '' }}">mid</text>
    </view>-->

    <!-- navigateTo -->
    <navigator class="cell" hover-class="none" open-type="navigate" url="{{path}}">
      <view class="imgBox">
        <image src="../assets/images/icon_release.png" mode="aspectFill" class="img_icon" />
      </view>
      <text class="title {{ tabBarActive === 'mid' ? 'activeCor' : '' }}">添加</text>
    </navigator>

    <view class="cell" bindtap="onTabBarChange('tab3')">
      <view class="imgBox">
        <van-icon
          class="customIcon {{tabBarActive === 'tab3' ? 'activeCor' : ''}}"
          size="26"
          name="chart-trending-o"
        />
      </view>
      <text class="title {{ tabBarActive === 'tab3' ? 'activeCor' : '' }}">tab页3</text>
    </view>
    <view class="cell" bindtap="onTabBarChange('my')">
      <view class="imgBox">
        <van-icon
          class="title {{ tabBarActive === 'my' ? 'activeCor' : '' }}"
          size="26"
          name="user-circle-o"
        />
      </view>
      <text class="title {{ tabBarActive === 'my' ? 'activeCor' : '' }}">我的</text>
    </view>
  </view>
</template>
<script>
import { createComponent } from '@mpxjs/core'
import store from '../store'
createComponent({
  data: {
    activeFlag: 0,
    path: '/pages/mid'
  },
  computed: {
    tabBarActive() { // 用于标记那个激活
      return store.state.tabBarActive
    },
    bottomNum() { // 判断是否是全面屏(demo可以不写)
      return store.state.bottomNum
    },
    tabBarShow() { // 自定义导航栏无法使用wx.hidetabbar,可以用这个控制
      return store.state.tabBarShow
    }
  },
  methods: {
    onTabBarChange(string) {
      store.commit('CHANGE_TABER', string)
      wx.switchTab({ url: '/pages/' + string })
    }
  }
})
</script>
<style lang="stylus">
.customTabbar
  width 100%
  display flex
  justify-content space-between
  border-top 1px solid #efefef
  background #fff
  .cell
    width 20%
    height 100rpx
    display flex
    flex-direction column
    justify-content center
    .imgBox
      width 100%
      height 50rpx
      display flex
      justify-content center
      .img_icon
        position absolute
        /* left: 77rpx; */
        top -36rpx
        width 96rpx
        height 96rpx
        border-radius 50%
        border-top 2rpx solid #f2f2f3
        background-color #fff
        text-align center
        box-sizing border-box
        padding 6rpx
      .customIcon
        color #909090
    .title
      font-size 24rpx
      text-align center
      color #909090
.activeCor
  color #E92F20 !important
.safeDistance
  margin-bottom 60rpx
</style>
<script type='application/json' lang='json'>
  { 
    "component": true,
    "usingComponents": {
        "van-icon": "@vant-weapp/weapp/dist/icon/index"
    }
  }
</script>
<think>嗯,用户想在小程序导航栏中添加一个二维码按钮。首先,我得回忆一下微信小程序的开发文档。底部导航栏通常是用tabBar配置的,默认只能放页面路径。但用户的需求是在中放一个特殊按钮,比如二维码图标,这可能需要自定义tabBar的方案。 根据引用[3]提到的wx.hideHomeButton,虽然这个API是隐藏首页按钮的,但说明小程序提供了操作界面元素的API。不过底部标签栏属于原生组件,直接修改中按钮可能行不通。所以需要放弃原生tabBar,改用自定义组件模拟整个导航栏结构。 然后想到,自定义tabBar需要在小程序的app.json里启用custom字段,然后创建一个自定义组件。这个组件会包含几个普通按钮和一个中的二维码按钮。布局方面,中的按钮可能需要绝对定位,并且突出显示,比如加大尺寸或者放在其他按钮上方。 接着是功能实现。点击二维码按钮需要触发扫码功能,这里引用[1]提到了引入支付和二维码SDK,但用户需要的可能是启动相机扫描。应该用wx.scanCode这个API,用户授权后就能调起扫码界面了。另外,如果二维码按钮是用来展示收款码的,可能需要调用生成二维码的库,比如weapp-qrcode,在点击后弹出一个模态框显示二维码图片。 还要考虑权限问题。引用[1]中提到小程序需要申请支付权限,但扫码功能本身应该不需要额外权限,除非涉及支付回调。不过用户的需求只是启动扫码,可能只需要在app.json里声明scopes.userLocation权限,因为扫码有时会关联地理位置。 另外,引用[2]提到了注册小程序的步骤,虽然和当前问题不直接相关,但提醒我可能需要检查开发者的账号权限是否足够,是否有使用自定义组件的能力。不过用户的问题集中在实现方法,这部分可以先不提及。 最后,自定义组件的细节需要注意。比如在app.json里声明usingComponents,创建custom-tab-bar目录,编写组件wxml、wxss和js文件。其中wxml里用flex布局平均分配按钮位置,中按钮用absolute定位并居中。js里处理点击事件,分别跳转页面或触发扫码。 可能的问题点包括自定义tabBar的兼容性,尤其是不同手机屏幕的适配,以及扫码后的回调处理。不过用户当前的问题集中在界面实现,功能细节可以后续补充。另外,如果用户需要的是展示静态二维码而非扫码,实现方式会不同,但根据问题描述,用户应该是想要一个触发扫码的按钮。 </think>在微信小程序下方导航栏中实现二维码按钮,需采用 **自定义`tabBar`方案**,因为原生`tabBar`不支持在中插入功能性按钮。以下是详细步骤和代码实现: --- ### 一、核心实现步骤 1. **配置启用自定义`tabBar`** 在 `app.json` 中添加: ```json { "tabBar": { "custom": true, // 启用自定义tabBar "list": [ /* 原有页面配置 */ ] } } ``` 2. **创建自定义组件** - 新建目录 `custom-tab-bar` - 创建组件文件:`index.wxml`, `index.wxss`, `index.js`, `index.json` 3. **实现组件布局** (`index.wxml`) ```html <view class="tab-bar"> <!-- 左侧按钮 --> <view class="tab-item" bindtap="navigateToHome">首页</view> <!-- 中二维码按钮(突出样式) --> <view class="tab-item qr-center" bindtap="handleQRClick"> <image src="/images/qr-icon.png" class="qr-icon"></image> </view> <!-- 右侧按钮 --> <view class="tab-item" bindtap="navigateToUser">我的</view> </view> ``` 4. **添加样式** (`index.wxss`) ```css .tab-bar { display: flex; position: fixed; bottom: 0; width: 100%; height: 100rpx; background: white; border-top: 1rpx solid #eee; } .tab-item { flex: 1; text-align: center; line-height: 100rpx; } .qr-center { position: relative; } .qr-icon { position: absolute; top: -30rpx; /* 向上突出显示 */ left: 50%; transform: translateX(-50%); width: 80rpx; height: 80rpx; border-radius: 50%; background: #07C160; } ``` 5. **绑定点击事件** (`index.js`) ```javascript Component({ methods: { // 处理二维码按钮点击 handleQRClick() { wx.scanCode({ // 调用微信扫码API success(res) { console.log("扫码结果:", res.result) // 处理扫码逻辑(如跳转支付页) }, fail(err) { console.error("扫码失败:", err) } }) }, navigateToHome() { wx.switchTab({ url: '/pages/home/index' }) }, navigateToUser() { wx.switchTab({ url: '/pages/user/index' }) } } }) ``` --- ### 二、关键细节说明 1. **扫码权限配置** 在 `app.json` 中声明所需权限: ```json "requiredPrivateInfos": ["getLocation", "scanQRCode"] ``` 2. **按钮突出效果** - 通过 `position: absolute` + `top: -30rpx` 使二维码按钮悬浮于导航栏上方 - 圆形背景增强视觉焦点(可选添加阴影动画) 3. **扫码功能扩展** - **收款码场景**:若需生成收款二维码,使用插件如 `weapp-qrcode`: ```javascript // 点击后生成收款码弹窗 const qr = new QRCode("canvasId", { text: "weixin://wxpay/bizpayurl?pr=XXXXX", width: 200, height: 200 }) ``` --- ### 三、注意事项 1. **自定义`tabBar`兼容性** - 基础库需 ≥ 2.5.0,旧版本需降级方案 - Android 底部安全距离可通过 `env(safe-area-inset-bottom)` 适配 2. **扫码回调逻辑** - 支付场景需配合 `wx.requestPayment` - 外部链接需用 `web-view` 组件打开 3. **审核规范** 涉及支付功能需完成微信支付申请[^1],且按钮不得诱导用户扫码转账。 --- ### 四、相关问题 1. 如何为自定义`tabBar`添加动画效果? 2. 微信小程序扫码支付的回调结果如何处理? 3. 如何兼容全面屏手机的底部安全区域? > 实现效果参考: > ![自定义tabBar效果](https://example.com/custom-tabbar-demo.png) > _图示:中悬浮二维码按钮的导航栏样式_ [^1]: 微信支付商户平台接入文档 [^2]: 微信小程序自定义 tabBar 官方文档 [^3]: 微信JSAPI `wx.scanCode` 能力说明
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值