当前位置: 首页 > Web前端 > HTML5

小程序日常-自定义组件头部导航组件

时间:2023-04-05 22:21:14 HTML5

头部导航组件有一个小房子和一个返回按钮。可以设置背景颜色、标题文本颜色、胶囊背景颜色和边框颜色。headerNabvar.jsconstapp=getApp();Component({properties:{navbarData:{//navbarData是父页面传过来的数据type:Object,value:{},observer:function(newVal,oldVal){}}},data:{haveBack:true,//是否有返回按钮,trueorfalse,no如果从分享页面进入,没有返回按钮statusBarHeight:0,//状态栏高度navbarHeight:0,//topnavigationbarheightnavbarBtn:{//胶囊位置信息height:0,width:0,top:0,bottom:0,right:0}},//微信7.0.0支持wx.getMenuButtonBoundingClientRect()获取高度附加的胶囊按钮:function(){letstatusBarHeight=app.globalData.systeminfo.statusBarHeight//状态栏高度letheaderPosi=app.globalData.headerBtnPosi//胶囊位置信息/***wx.getMenuButtonBoundingClientRect()坐标信息取以屏幕左上角为原点*菜单按钮宽度:87*菜单按钮高度:32*菜单按钮左边框坐标:278*菜单按钮上边框坐标:26*菜单按钮右边框坐标:365*菜单按钮下边框坐标:58*/letbtnPosi={//胶囊的实际位置,坐标信息不是左上角高度的原点:headerPosi.height||32、宽度:headerPosi.width||87,top:(headerPosi.top||26)-statusBarHeight,//capsuletop-状态栏高度bottom:(headerPosi.bottom||58)-(headerPosi.height||32)-statusBarHeight,//capsulebottom-capsuleheight-状态栏高度(capsule的实际底部是距离导航栏底部的长度)right:app.globalData.systeminfo.screenWidth-(headerPosi.right||365)//screenwidth-capsuleright}让我们回来;if(getCurrentPages().length===1){//当只有一页时,从共享页面进入haveBack=false;}else{haveBack=true;}this.setData({haveBack:haveBack,//获取小程序是否通过分享进入//haveBack:true,statusBarHeight:statusBarHeight,navbarHeight:(headerPosi.bottom||58)+btnPosi.bottom,//capsulebottom+capsule真实的底部navbarBtn:btnPosi})},方法:{_goBack:function(event){wx.navigateBack({delta:1,success(){}});},_goHome:function(event){wx.reLaunch({url:'/pages/index/index'})}}})headerNabvar.json{"component":true,"usingComponents":{}}headerNabvar.wxml{{navbarData.title?navbarData.title:"默认标题"}}<viewclass\="navbar-icon"wx:if\='{{navbarData.showCapsule?navbarData.showCapsule:true}}'style\="top:{{navbarBtn.top+statusBarHeight}}px;left:{{navbarBtn.right}}px;height:{{navbarBtn.height}}px;background:{{navbarData.btnBgColor?navbarData.btnBgColor:'#fff'}};border-color:{{navbarData.borderColor?navbarData.borderColor:'rgba(0,0,0,0.3)'}}"\></view\>headerNavbar.wxss.navbar-wrap{位置:固定;宽度:100%;顶部:0;z-索引:9999999;背景色:#3281FF;box-sizing:border-box;}.??navbar-text{text-align:center;字体大小:32rpx;颜色:#333333;font-weight:600;}.navbar-icon{position:fixed;显示:弹性;边界半径:64rpx;边框:1rpx实心rgba(222,222,222,1);box-sizing:border-box;}.??navbar-icon.homeimage{height:36rpx;宽度:42rpx;填充:12rpx26rpx12rpx;显示:内联块;overflow:hidden;}.navbar-iconview{height:36rpx;左边框:1rpxsolidrgba(222,222,222,1);margin-top:12rpx;}.navbar-loading{/*background:#fff;*/text-align:center;}.haveback{宽度:18rpx;高度:34rpx;填充:12rpx26rpx12rpx;显示:内联块;溢出:隐藏;}

最新推荐
猜你喜欢