小程序代码片段
**
效果图
**
代码片段(点击可直接查看功能实现的全部代码)
部分代码
wxml
<view class="navBar {{navbarData.position?'navBarPosition':''}}" style="height: {{navbarHeight}}px;">
<view class="navImg" style='margin-top: {{statusBarHeight}}px;top: 18rpx' >
<image src="/assets/images/back.png" bindtap="_goBack" wx:if="{{imgStatus}}" class="img"></image>
<image src="/assets/images/back2.png" bindtap="_goBack" style="width: 40rpx; height: 40rpx;" wx:else=""></image>
</view>
<view style='line-height:{{navbarBtn.height + navbarBtn.top}}px;margin-top: {{statusBarHeight}}px; top: 8rpx' class="{{imgStatus ? 'navText' : 'textColor'}}" >
<text class="textTitle">{{navbarData.title }}</text>
</view>
</view>
app.js
App({
onLaunch() {
wx.getSystemInfo({ // 获取设备信息
success: (res) => {
this.globalData.systeminfo = res
//导航栏
let statusBarHeight = res.statusBarHeight // 状态栏高度
let headerPosi = wx.getMenuButtonBoundingClientRect() // 胶囊位置信息
let btnPosi = { // 胶囊实际位置,坐标信息不是左上角原点
height: headerPosi.height,
width: headerPosi.width,
// 胶囊top - 状态栏高度
top: headerPosi.top - statusBarHeight,
// 胶囊bottom - 胶囊height - 状态栏height (现胶囊bottom 为距离导航栏底部的长度)
bottom: headerPosi.bottom - headerPosi.height - statusBarHeight,
// 屏幕宽度 - 胶囊right
right: res.screenWidth - headerPosi.right
}
// 原胶囊bottom + 现胶囊bottom
this.globalData.navbarHeight = headerPosi.bottom + btnPosi.bottom
this.globalData.statusBarHeight = statusBarHeight
this.globalData.navbarBtn = btnPosi
wx.setStorageSync('navbarHeight', this.globalData.navbarHeight)
},
})
// 获得胶囊按钮位置信息
this.globalData.headerBtnPosi = wx.getMenuButtonBoundingClientRect()
},
globalData: {
userInfo: null,
headerBtnPosi: {}, // 胶囊按钮位置信息
navbarHeight: 0,
statusBarHeight: 0,
navbarBtn: 0
}
})
app.json
{
"pages": [
"index/index"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin",
"navigationBarTextStyle": "black",
"navigationStyle":"custom"
},
"style": "v2",
"sitemapLocation": "sitemap.json",
"lazyCodeLoading": "requiredComponents"
}