自定义微信小程序NavigationBar
App.js
App({
onLaunch() {
let menuButtonObject = wx.getMenuButtonBoundingClientRect();
wx.getSystemInfo({
success: res => {
let statusBarHeight = res.statusBarHeight,
navTop = menuButtonObject.top,
navObjWid = res.windowWidth - menuButtonObject.right + menuButtonObject.width,
navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
this.globalData.navHeight = navHeight;
this.globalData.navTop = navTop;
this.globalData.navObj = menuButtonObject.height;
this.globalData.navObjWid = navObjWid;
this.globalData.windowWidth = res.windowWidth;
this.globalData.windowHeight = res.windowHeight;
},
fail(err) {
console.log(err)
}
})
},
globalData: {
navHeight: 0,
navTop: 0,
navObj: 0,
navObjWid: 0,
windowWidth: 0,
windowHeight:0,
}
})
page.js文件
var app = getApp();
Page({
data: {
navHeight: app.globalData.navHeight,
navTop: app.globalData.navTop,
navObj: app.globalData.navObj,
navObjWid: app.globalData.navObjWid ,
},
onLoad(options) {
wx.setNavigationBarColor({
frontColor:"#ffffff",
backgroundColor:"#ffffff"
})
},
}
page.json文件
{
"usingComponents": {},
"navigationStyle": "custom",
}
page.wxml文件
<view class="header">
<image src="https://w.wallhaven.cc/full/9d/wallhaven-9doq78.png" mode="aspectFill" class="header-bg" />
<view style="height: {{navHeight*2}}rpx; ">
<view style="height: {{navObj*2}}rpx; padding-top: {{navTop*2}}rpx; position: relative; width: 100%; text-align: center; line-height: {{navObj*2}}rpx; color: #ffffff; font-size: 28rpx;" >
防伪溯源
</view>
</view>
</view>
page.wxss文件
page {
width: 100%;
}
.header {
width: 100%;
position: relative;
height: 2000rpx;
}
.header .header-bg{
height: 100%;
width: 100%;
position: absolute;
left: 0;
top: 0;
}