小程序获取页面某元素高度及兼容不同手机高度自适应

             

 如图,页面分三部分,顶部轮播、中间内容区、底部tabBar,底部固定,中间区高度用wx.createSelectorQuery()获取,轮播的高度自适应,使在不同的手机上完全铺满无滚动。

底部tabBar的高度固定(自定义)是63,但是有部分手机底下有空白区域,如上图对比,多出的这一部分也算作底部tabBar的高度了,打印wx.getSystemInfoSync(),可看到screenHeight 和 safeArea.bottom 的差值就是额外的高度。

  

获取页面某元素的高度代码

 const query = wx.createSelectorQuery()
    query.select('.heihgtBody').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function(res){
      console.log(res)
    })

 完整代码如下

onLoad(){
      var that = this
      console.log(wx.getSystemInfoSync()) 
      let SystemInfoSync =  wx.getSystemInfoSync()
      let windowHeight = SystemInfoSync.windowHeight //屏幕高度
      //部分手机额外底下的空白高度
      let addedHeight = SystemInfoSync.screenHeight - SystemInfoSync.safeArea.bottom 
      const query = wx.createSelectorQuery()
      query.select('.heihgtBody').boundingClientRect()
      query.selectViewport().scrollOffset()
      query.exec(function(res){
        console.log("高度",res)
        let height =  windowHeight - res[0].height - 60 - addedHeight
        that.setData({
          swiperHeight: height + 'px'
        })
      })
    },

  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值