您当前的位置: 首页 >  微信小程序

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

微信小程序:自定义头部(navigationStyle=custom)及手机适配

彭世瑜 发布时间:2022-02-21 14:04:56 ,浏览量:2

自定义顶部栏之后,通过微信小程序给出的接口获取几个位置坐标(单位都是: px)

用到的接口及其文档

  • 取菜单按钮(右上角胶囊按钮)的布局位置信息 Object wx.getMenuButtonBoundingClientRect()
  • 获取系统信息 Object wx.getSystemInfoSync()

获取到的值:

  • 灰色:胶囊底部坐标 menuInfo.bottom
  • 天空蓝:胶囊顶部坐标 menuInfo.top
  • 绿色:状态栏高度 systemInfo.statusBarHeight

注意:这三个值都是距离顶部的距离

直观显示如下 在这里插入图片描述 使用如下代码

page.json

{
  "navigationStyle": "custom",
  "usingComponents": {}
}

page.js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    systemInfo: {},
    menuInfo: {},
  },

 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let menuInfo = wx.getMenuButtonBoundingClientRect();
    let systemInfo = wx.getSystemInfoSync();
    
    console.log(systemInfo, menuInfo);
    this.setData({
      systemInfo,
      menuInfo,
    });
  }
});

page.wxss

.skyblue {
  background-color: skyblue;
}
.green {
  background-color: green;
}
.grey {
  background-color: grey;
}

page.wxml


            
关注
打赏
1665367115
查看更多评论
0.1684s