小程序底部tabbar设置扫一扫功能

效果图及需要的页面:

     

custom-tab-bar/index.js

Component({
  data: {
    selected: 0,
    "color": "#bfbfbf",
    "selectedColor": "#56D88A",
    "borderStyle": "#fff",
    list: [
    {
      "pagePath": "/pages/index/index",
      "text": "首页",
      "iconPath": "../pages/image/home.png",
      "selectedIconPath": "../pages/image/homes(2).png"
    },
    {
      "pagePath": "/pages/ScanPage/ScanPage",
      "text": "扫一扫",
      "iconPath": "../pages/image/home.png",
      "selectedIconPath": "../pages/image/homes(2).png"
    },
    {
      "pagePath": "/pages/mine/mine",
      "text": "我的",
      "iconPath": "../pages/image/mine.png",
      "selectedIconPath": "../pages/image/mines(2).png"
    }
  ]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const index = e.currentTarget.dataset.index
      if(index==1){   //如果点击扫一扫,就调取扫一扫功能
        wx.scanCode({
          success: (res) => {
           //扫码成功后的操作
          }
        })
      }else{
        const url = data.path
        wx.switchTab({url})   //点击首页时,跳转到'首页'页面;点击我的时,跳转到'我的'页面
      }
      this.setData({
        selected: data.index
      })
    }
  }
})

custom-tab-bar/index.json

{
  "component": true
}

custom-tab-bar/index.wxml

<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
  </cover-view>
</cover-view>

custom-tab-bar/index.wxss

/* custom-tab-bar/index.wxss */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item cover-image {
  width: 27px;
  height: 27px;
}

.tab-bar-item cover-view {
  font-size: 10px;
}

扫一扫页面啥也不用写

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值