微信小程序实现一个功能,让头部固定,下面动态滚动的效果,如下图所示
具体的实现思路是,用wx.getSystemInfo获取可使用的屏幕高度减去头部的固定高度,作为滚动层scroll-view的使用高度;
核心代码如下
var that = this
wx.getSystemInfo({
success: function (res) {
this.setData({
tbodyHeight:res.windowHeight
})
}
})
<scroll-view scroll-y="true" style='height:{{tbodyHeight}}px;'></scroll-view>
然而在实际使用过程中发现,多个页面中每个页面调用wx.getSystemInfo获取可使用的屏幕高度的值在不同真机的出现了值不一致的问题,这也就导致了滚动视图层显示效果不佳;
解决思路是通过设置全局变量的方式;具体实现方式如下;
app.js
onLaunch: function () {
wx.getSystemInfo({
success: function (res) {
that.globalData.windowHeight = res.windowHeight
}
})
}
,
globalData: {
windowHeight:null
}
需要获取windowHeight的页面js
如index.js
const app = getApp()
onReady:function(){
var tbodyHeight = app.globalData.windowHeight - 90; //90为头部固定高度
that.setData({
tbodyHeight: tbodyHeight.toFixed(0)
})
}
好了,以上为我对微信小程序windowHeight每个页面值不一致的解决方案。
附我的第一个微信小程序,请大家多多指教^_^