欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序自定義頂部組件customHeader的示例代碼

 更新時(shí)間:2020年06月03日 11:38:11   作者:tony康  
這篇文章主要介紹了微信小程序自定義頂部組件customHeader的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、開啟配置自定義頂部

在這里插入圖片描述

{
 "window": {
 "navigationStyle":"custom"
 }
}

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

2、自定義頂部計(jì)算原理

2.1 獲取系統(tǒng)狀態(tài)欄的高度和屏幕寬度

使用wx.getSystemInfo這個(gè)函數(shù)獲取系統(tǒng)狀態(tài)欄的高度和屏幕寬度。

在這里插入圖片描述

2.2 獲取右上角膠囊位置信息

使用wx.getMenuButtonBoundingClientRect()方法獲取右上角膠囊位置信息。
關(guān)鍵問題在于自定義膠囊的上、下和左邊距。

在這里插入圖片描述

2.3 自定義頂部距離計(jì)算代碼

app.js代碼如下

App({
 // 小程序初始化
 onLaunch: function() {
 // 獲取自定義頂部高度相關(guān)參數(shù)
 let capsuleObj = wx.getMenuButtonBoundingClientRect();
 // console.log("==膠囊信息==");
 // console.log(capsuleObj);

 wx.getSystemInfo({
 success: (res) => {
 // console.log("==獲取系統(tǒng)信息==");
 // console.log(res)
 var statusBarHeight = res.statusBarHeight; //頂部狀態(tài)欄高度

 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>

 <!-- 標(biāo)題 -->
 <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;
}

/* 標(biāo)題 */

.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
 },
 },

 /**
 * 組件的初始數(shù)據(jù)
 */
 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個(gè)樣式,meneFlag是菜單的。backHomeFlag是“返回+首頁”樣式的。

json配置

{
 "usingComponents": {
 "customHeader": "/components/customHeader/index"
 }
}

5、小結(jié)

此組件兼容性,可以兼容安卓、IOS、劉海屏,如果你們測試出來有新bug,可以在gitHub提出issues,幫助您解決。

鏈接在此:
微信小程序自定義頂部組件

到此這篇關(guān)于微信小程序-自定義頂部組件customHeader的文章就介紹到這了,更多相關(guān)微信小程序自定義頂部組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論