在App.vue添加
onLaunch: function() {
uni.getSystemInfo({
success: function(e) {
// #ifndef MP
Vue.prototype.StatusBar = e.statusBarHeight;
if (e.platform == 'android') {
Vue.prototype.CustomBar = e.statusBarHeight + 50;
} else {
Vue.prototype.CustomBar = e.statusBarHeight + 45;
};
// #endif
// #ifdef MP-WEIXIN
Vue.prototype.StatusBar = e.statusBarHeight;
let custom = wx.getMenuButtonBoundingClientRect();
Vue.prototype.Custom = custom;
Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
// #endif
// #ifdef MP-ALIPAY
Vue.prototype.StatusBar = e.statusBarHeight;
Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
// #endif
}
})
},
这段代码引用的ColorUI的代码。
Vue.prototype.StatusBar = e.statusBarHeight; 获取的是手机状态栏的高度。
然后在其他页面中的data中定义这个高并且赋值 barheight: this.StatusBar
最后在页面最外层的view中动态绑定这个高,:style="{paddingTop:barheight+'px'}"
这样一来,app的页面就能根据不同手机,不同状态栏高度而改变距离顶部的高度