微信小程序scroll-view组件自适应不同高度的手机

已知微信相比于H5开发有rpx这个单位可以自适应一些不同宽度的手机,但是有的时候需要自适应一些手机高度贼大的手机比如iphoneX系列

所以scroll-view的wxss样式里肯定不能写死,我能想到的方法就是js里头加载画面前再设置高度

wxml里

<scroll-view scroll-y style='height:{{scroll_height}}rpx'>
    // 一些标签
</scroll-view>

js

Page({
  data: {
    scroll_height: 0,
    }
})

onLoad: function (options) {
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scroll_height: windowHeight * 750 / windowWidth - (本页面除了scroll以外其他组件的高度rpx) - 30
    })
  },

根据屏幕的宽高计算出屏幕高度的rpx值,减去其他组件的高度得出scroll高度

猜你喜欢

转载自blog.csdn.net/qq_20686495/article/details/82887541