当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序自定义顶部组件customHeader的示例代码

微信小程序自定义顶部组件customHeader的示例代码

2020年06月23日  | 移动技术网IT编程  | 我要评论
1、开启配置自定义顶部{ "window": { "navigationstyle":"custom" }}在app.json的文件window配置"navigationstyle": "custom

1、开启配置自定义顶部

在这里插入图片描述

{
 "window": {
 "navigationstyle":"custom"
 }
}

在app.json的文件window配置"navigationstyle": "custom"属性即可

2、自定义顶部计算原理

2.1 获取系统状态栏的高度和屏幕宽度

使用wx.getsysteminfo这个函数获取系统状态栏的高度和屏幕宽度。

在这里插入图片描述

2.2 获取右上角胶囊位置信息

使用wx.getmenubuttonboundingclientrect()方法获取右上角胶囊位置信息。
关键问题在于自定义胶囊的上、下和左边距。

在这里插入图片描述

2.3 自定义顶部距离计算代码

app.js代码如下

app({
 // 小程序初始化
 onlaunch: function() {
 // 获取自定义顶部高度相关参数
 let capsuleobj = wx.getmenubuttonboundingclientrect();
 // console.log("==胶囊信息==");
 // console.log(capsuleobj);

 wx.getsysteminfo({
 success: (res) => {
 // console.log("==获取系统信息==");
 // console.log(res)
 var statusbarheight = res.statusbarheight; //顶部状态栏高度

 this.globaldata.capsuleobj = capsuleobj;
 this.globaldata.titleheight = statusbarheight + capsuleobj.height + (capsuleobj.top - statusbarheight) * 2;
 },
 failure() {
 }
 })
 },

 // 全局缓存
 globaldata: {
 loginstatus: false,
 },
})

3、封装自定义顶部

3.1效果图展示

在这里插入图片描述
在这里插入图片描述

3.2组件代码

index.wxml

<!--components/customheader/index.wxml-->
<view class="customheader_box" style="height:{{titleheight}}px; background-color:{{bgcolor}};">
 <!-- 菜单 -->
 <view wx:if="{{menuflag}}" class="menu_box" style="height:{{capsuleobj.height}}px; top:{{capsuleobj.top}}px;">
 <view class="customicon" bindtap="meunclick">
 <image src="/images/custommenu.png"></image>
 </view>
 </view>

 <!-- 返回+首页 -->
 <view wx:if="{{backhomeflag}}" class="backhome_box" style="width:{{capsuleobj.width}}px; height:{{capsuleobj.height}}px; top:{{capsuleobj.top}}px;">
 <view class="customicon backicon" bindtap="backclick">
 <image src="/images/customback.png"></image>
 </view>
 <view class="customicon homeicon" bindtap="homeclick">
 <image src="/images/customhome.png"></image>
 </view>
 </view>

 <!-- 标题 -->
 <view class="customheader_title" style="top:{{capsuleobj.top}}px; height:{{capsuleobj.height}}px; line-height:{{capsuleobj.height}}px;">
 {{customtitle}}
 </view>
</view>

<!-- 自定义顶部距离修正 -->
<view class="customwrap" style="height:{{titleheight}}px;"></view>

index.wxss

/* components/customheader/index.wxss */

.customheader_box {
 width: 100%;
 overflow: hidden;
 position: fixed;
 top: 0;
 left: 0;
 z-index: 99999;
}

.customicon {
 flex: 1;
}

.customicon image {
 width: 30rpx;
 height: 30rpx;
}

/* 菜单 */
.menu_box{
 text-align: center;
 box-sizing: border-box;
 overflow: hidden;
 position: absolute;
 left: 10px;
 z-index: 11;
 display: flex;
 justify-content: space-between;
 align-items: center;
}

.menu_box .customicon image{
 width: 36rpx;
 height: 36rpx;
}

/* 返回+首页 */

.backhome_box {
 text-align: center;
 border: 1px solid #e5e5e5;
 border-radius: 20px;
 box-sizing: border-box;
 overflow: hidden;
 position: absolute;
 left: 10px;
 z-index: 11;
 display: flex;
 justify-content: space-between;
 align-items: center;
}

.backicon {
 border-right: 1rpx solid #e5e5e5;
}

/* 标题 */

.customheader_title {
 width: 100%;
 padding-left: 115px;
 padding-right: 115px;
 text-align: center;
 font-size: 32rpx;
 font-weight: bold;
 color: #333;
 text-overflow: ellipsis;
 box-sizing: border-box;
 overflow: hidden;
 white-space: nowrap;
 position: absolute;
 left: 0;
 z-index: 10;
}

/* 自定义顶部距离修正 */
.customwrap{
 width: 100%;
 position: relative;
 left: 0;
 z-index: 99998;
}

index.js

// components/customheader/index.js
const app = getapp();

component({
 /**
 * 组件的属性列表
 */
 properties: {
 customtitle: string,
 bgcolor: {
 type: string,
 value: '#fff'
 },
 menuflag: {
 type: boolean,
 value: false
 },
 backhomeflag: {
 type: boolean,
 value: false
 },
 },

 /**
 * 组件的初始数据
 */
 data: {

 },

 attached: function() {
 this.setdata({
 titleheight: app.globaldata.titleheight,
 capsuleobj: app.globaldata.capsuleobj
 })
 },

 options: {
 multipleslots: true, //开启多slot
 },

 /**
 * 组件的方法列表
 */
 methods: {
 // 菜单
 meunclick: function () {
 wx.navigateto({
 url: '/pages/menu/menu',
 })
 },

 // 返回
 backclick: function() {
 wx.navigateback({
 delta: 1
 })
 },
 
 // 回首页
 homeclick: function() {
 wx.navigateto({
 url: '/pages/index/index'
 })
 },
 }
})

index.json

{
 "component": true
}

4、组件使用方法

index.wxml

<!--pages/index/index.wxml-->

<!-- 自定义顶部 -->
<customheader menuflag customtitle="首页"></customheader>

<view class="url">
	<navigator hover-class="none" url="../child/child">跳到子页</navigator>
</view>

ps:我这边封装了2个样式,meneflag是菜单的。backhomeflag是“返回+首页”样式的。

json配置

{
 "usingcomponents": {
 "customheader": "/components/customheader/index"
 }
}

5、小结

此组件兼容性,可以兼容安卓、ios、刘海屏,如果你们测试出来有新bug,可以在github提出issues,帮助您解决。

链接在此:
微信小程序自定义顶部组件

到此这篇关于微信小程序-自定义顶部组件customheader的文章就介绍到这了,更多相关微信小程序自定义顶部组件内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网