微信小程序总结

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

前言

主要记录在微信小程序开发过程所用到的功能以及遇到的问题及解决方案;


一、获取不同型号手机去除导航栏之外的高度

let menuButtonObject = wx.getMenuButtonBoundingClientRect();
  let res = wx.getSystemInfoSync()
  let statusHeight = (res.safeArea.top - res.statusBarHeight)*2 + res.statusBarHeight + menuButtonObject.height;
  let height = ((res.screenHeight - statusHeight - 24) / res.screenHeight*1) *100;

二、微信小程序自定义tabbar

(https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html))

1.app.json中的tabbar字段指定custom字段,同时其余tabBar相关配置也补充完整

代码如下(示例):

{
  "tabBar": {
    "custom": true,
    "color": "#000000",
    "selectedColor": "#000000",
    "backgroundColor": "#000000",
    "list": [{
      "pagePath": "page/component/index",
      "text": "组件"
    }, {
      "pagePath": "page/API/index",
      "text": "接口"
    }]
  },
  "usingComponents": {}
}

2.添加tabBar代码文件(要跟app.json在同一目录下)

custom-tab-bar/index.js:


let list = [
  {
    pagePath: "/pages/homePage/wallet/wallet/index",
    text: "钱包",
    iconPath: "/public/image/qianbao-weixuan.png",
    selectedIconPath: "/public/image/qianbao-xuanzhong.png"
  },
  {
    pagePath: "/pages/roomLeader/index",
    text: "管理",
    iconPath: "/public/image/guanli-icon-weixuanzhong.png",
    selectedIconPath: "/public/image/guanli-icon.png"
  },
  {
    pagePath: "/pages/homePage/user/home/index",
    text: "我的",
    iconPath: "/public/image/wode-weixuan.png",
    selectedIconPath: "/public/image/wode-xuanzhong.png"
  }
]
Component({
  data: {
    borderStyle: "white",
    selected: 0,
    color: "#999",
    selectedColor: "#14dec9",
    list: list,
    isShow:true
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      console.log(data)
      this.setData({
        selected: data.index
      })
      wx.switchTab({url})
    
      
      
    }
  }
})

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;
}

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.json:

{
  "component": true
}

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值