微信小程序,自定义导航栏组件
微信小程序,自定义导航栏组件,可兼容iPhone 11及以上留海屏显示,关于参数获取设置参照微信小程序-收藏_羽筠的博客-CSDN博客
可定义设置的内容如下:
文字及返回箭头颜色
背景图片(优先级高于 背景色)
背景色
页面标题
页面标题显示图片(优先级高于 页面标题)
是否显示返回菜单
是否显示 页面主体顶部被自定义菜单挡住部分(false则主体内容会被导航栏遮盖)
组件相关代码:
.wxss
page, view, scroll-view, swiper, swiper-item, button, input, textarea, label, navigator, image{ box-sizing: border-box; } .nav-custom-bar{ width: 100%; font-size: 30rpx; position: fixed; left: 0; top: 0; z-index: 9999; transition: all linear .3s;} .nav-custom-bar-back{ width: 110rpx; padding-left: 25rpx; display: flex; justify-content: space-between; align-items: center;} .nav-custom-bar-back-arrow{ width: 22rpx; height: 22rpx; border-top: none !important; border-right: none !important; transform: rotate(45deg);} .nav-custom-bar-back-text{} .nav-custom-bar-title{ text-align: center; position: absolute; left: 50%; transform: translateX(-50%);} .nav-custom-bar-title view{ white-space: nowrap; overflow: hidden; text-overflow: ellipsis;} .nav-custom-bar-title image{ width: 100%;} .nav-custom-bar-space{} .wxml
{textColor值}}" bgImg="{{bgImg值}}" bgColor="{{bgColor值}}" title="{{pageTitle值}}" titleImg="{{pageTitleImg值}}" isShowBack="{{isShowBack值}}" isShowCustomBarSpace="{{isShowCustomBarSpace值}}"> 自定义头部菜单 - 显示返回文字 返回 注: textColor : 文字及返回箭头颜色 bgImg : 背景图片(优先级高于 背景色) bgColor : 背景色 title : 页面标题 titleImg : 页面标题显示图片(优先级高于 页面标题) isShowBack : 是否显示返回菜单 isShowCustomBarSpace : 是否显示 页面主体顶部被自定义菜单挡住部分 --> {{title}} .json
{ "component": true } .js
var App = getApp(); Component({ options: { multipleSlots: true // 在组件定义时的选项中启用多 slot 支持 }, properties: { // 文字及返回箭头颜色 textColor: { type: String, value: "#000", }, // 背景图片(优先级高于 背景色) bgImg: { type: String, value: "", }, // 背景色 bgColor: { type: String, value: "", }, // 页面标题 title: { type: String, value: "", }, // 页面标题显示图片(优先级高于 页面标题) titleImg: { type: String, value: "", }, //是否显示返回菜单 isShowBack: { type: Boolean, value: true, }, //是否显示页面主体顶部被自定义菜单挡住部分 isShowCustomBarSpace: { type: Boolean, value: true, }, }, data: { modelIsIphoneX: App.globalData.modelIsIphoneX, StatusBar: App.globalData.StatusBar, CustomBar: App.globalData.CustomBar, Custom: App.globalData.Custom }, ready: function() { }, methods: { navigateBack(){ App.doNavigateBack(); }, } }) 引用组件相关代码:
app.json中全局中进行引用声明
"usingComponents": { "navigation-custom": "/components/navigation-custom/index" }, 引用组件页面相关代码:
.wxml
111111111111 222222222222 test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test .json
{ "navigationStyle": "custom", } .js
var App = getApp(); Page({ data: { modelIsIphoneX: App.globalData.modelIsIphoneX, StatusBar: App.globalData.StatusBar, CustomBar: App.globalData.CustomBar, Custom: App.globalData.Custom }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { var query = wx.createSelectorQuery(); query.select('#view').boundingClientRect() query.exec((res) => { console.log(res[0].height); this.setData({ topImgH : res[0].height }) }) }, /** * 监听页面滚动 */ onPageScroll (e) { console.log(e.scrollTop); // 解决苹果手机在当前页面滚动到一定位置后跳转至其他页面再返回导致的scrollTop负数引起的不能设置自定义菜单背景色BUG; if(e.scrollTop < 0){ wx.pageScrollTo({ scrollTop: 0, duration: 10 }) } if(e.scrollTop > this.data.topImgH + 200){ this.setData({ bgColor:"#39b54a", bgImg:"https://img-blog.csdnimg.cn/20201014180756918.png", titleImg:"" }) }else if(e.scrollTop > this.data.topImgH - App.globalData.CustomBar){ this.setData({ bgColor:"#1cbbb4", bgImg:"", titleImg:"https://csdnimg.cn/medal/chizhiyiheng@240.png" }) }else{ this.setData({ bgColor:"", bgImg:"", titleImg:"" }) } }, })