注册登录

小程序自定义导航栏组件开发,小程序自定义导航栏模板的实现

2018-09-20
导读:先说一下我的需求吧,因为我们的小程序是嵌套了webview。 点击webview里面的项目,点的层级太深,用户后退很麻烦。然后pm又找了一个自定义的导航的小程序,发现别人可以,然后我就...

先说一下我的需求吧,因为我们的小程序是嵌套了webview。 点击webview里面的项目,点的层级太深,用户后退很麻烦。然后pm又找了一个自定义的导航的小程序,发现别人可以,然后我就开始研究。

小程序自定义导航栏开发注意点与参考文档

 

  • 微信官方设计指导中关于胶囊按钮的描述 由此推测胶囊宽度87pt=116px,设置之后,的确能产生较好的兼容性效果
  • [2018-07-06]根据测试截图发现微信胶囊宽度应该在96px左右,还有待研究
  • 社区相关Q&A:自定义标题栏高度计算
  • 使用时注意方法与属性版本兼容性
  • 完善之路:
    1. 开一个项目采集设备的screenHeight,windowHeight,pixelRatio等信息到一个数据库中
    2. 微信提供这样一个数据库便于计算,或者微信优化自定义标题栏(譬如通知栏可以改变颜色但不要算在自定义范围内,给出胶囊宽高到通知栏距离到右侧屏幕边框距离等相关参数)

非自定义导航栏高度怎么计算?

  1. wx.getSystemInfo 和 wx.getSystemInfoSync 获取机器信息
  2. screenHeight - windowHeight 计算标题栏高度
  3. 标题栏高度
    • 'iPhone': 64,
    • 'iPhone X': 88,
    • 'android': 68

      —— 不完全统计(ip6 , ip5 , ip6p , ipx , 小米mix2 , 小米5等综合了开发工具提供的数据和真机数据)所得

 

有了这些从网上找到的资料,还有名叫猫圈的小程序的例子。开始代码实现了。

配置

"window": {        
        "navigationBarBackgroundColor": "#fff",        
        "navigationBarTextStyle": "black",       
        "backgroundColor": "#fff",        
        "navigationStyle": "custom"    
    },复制代码

这里navigationStyle 配置修改之后就只剩一个胶囊按钮,也就意味着所有页面的导航都的自定义实现,你可以选择模版或者组件来开发,这里我是选择用的组件开发。

然后定义导航的各个数值,我是在app。js 里面定义的

title_height: "64",        
        statusbarHeight: "24",        
        titleIcon_height: "32",        
        titleIcon_width: "87",        
        title_top: "24",        
        title_text: "xxx", // iphone X + 24        
        prefix: 24
复制代码

组件

wxml的代码

 
    {{title_text}}    
              
复制代码

wxss的代码

.title {  width: 100%;  background-color: #175dc6;  box-sizing: border-box;  position: fixed;  transform: translateZ(0);  z-index: 999990;}
.title_text {  text-align: center;  font-size: 37rpx;  color: #fff;  line-height: 44px;}
.title_icon {  background-color: #175dc6;  position: fixed;  top: 54rpx;  left: 16rpx;  border-radius: 64rpx;  box-sizing: border-box;  border: 0.5px solid #ebe48e;  display: flex;}
.title_icon image {  height: 20px;  width: 20px;  padding-top: 5px;  display: inline-block;  overflow: hidden;}
.title_icon view {  height: 18px;  border-left: 1px solid #bfb973;  margin-top: 6px;}
.floatL {  float: left;}
.title_icon image:nth-of-type(1), .title_icon image:nth-of-type(2) {  padding-right: 10px;  padding-bottom: 10px;  padding-left: 10px;}复制代码

js的代码

const app = getApp();
Component({    
    properties: {        
        isShow: { // 是否显示后退按钮            
            type: String,            
            value: "1"        
        },        
        isIndex: { // 是否主页            
            type: Boolean,            
            value: false,        
        },        
        title_height: { //             
            type: String,            
            value: app.config.title_height,        
        },        
        titleIcon_height: {            
            type: String,            
            value: app.config.titleIcon_height,        
        },        
        titleIcon_width: {            
            type: String,            
            value: app.config.titleIcon_width,        
        },        
        statusbarHeight: {            
            type: String,            
            value: app.config.statusbarHeight,        
        },        
        title_top: {            
            type: String,            
            value: app.config.title_top,        
        },        
        title_text: {            
            type: String,            
            value: app.config.title_text,        
        },    
    },    
    methods: {        
        _goBack: function() {            
            wx.navigateBack({                
                delta: 1            
            });       
        },        
        _goHome: function() {            
            wx.switchTab({               
                url: "/pages/index/index"            
            });        
            }    
        }
})复制代码

这样组件就写好了 只需要在你每个页面里面用这个组件传不同的值就可以了。

应用

在首页的wxml应用。 json 文件里面的配置 "navigationBarTitleText": "啦啦啦", "navigationBarBackgroundColor": "#175dc6", "usingComponents": { "header": "/components/layout/header/header" }复制代码
重磅推荐:小程序开店目录

第一部分:小商店是什么

第二部分:如何开通一个小商店

第三部分:如何登录小商店

第四部分:开店任务常见问题

第五部分:小商店可以卖什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收货/物流

第九部分:小程序怎么结算

第十部分:小程序客服

第十一部分:电商创业

第十二部分:小程序游戏开发

电话咨询 微信咨询 预约演示 0元开店