小程序自定義頭部

微信小程序window配置


1.配置app.json

"window": {

? ? "backgroundColor": "#F6F6F6",

? ? "backgroundTextStyle": "light",

? ? "navigationStyle": "custom",//必填

? ? "navigationBarTextStyle": "black"

? },

2.小程序頭部組件

js

// headTemplates/head/head.js

const app = getApp()

Component({

? properties: {

? ? navbarData: { //navbarData? 由父頁面?zhèn)鬟f的數(shù)據(jù)蔽豺,變量名字自命名

? ? ? type: Object,

? ? ? value: {},

? ? ? observer: function(newVal, oldVal) {

? ? ? ? //console.log(newVal, oldVal)

? ? ? }

? ? }

? },

? data: {

? ? height: '',

? ? //默認值? 默認顯示左上角

? ? navbarData: {

? ? ? showCapsule: 1

? ? },

? ? open: false

? },

? attached: function() {

? ? // 獲取是否是通過分享進入的小程序

? ? this.setData({

? ? ? share: app.globalData.share

? ? })


? ? // 定義導航欄的高度? 方便對齊

? ? this.setData({

? ? ? totalTopHeight: app.globalData.totalTopHeight,

? ? ? titleBarHeight: app.globalData.titleBarHeight,

? ? ? statusBarHeight: app.globalData.statusBarHeight,

? ? ? inputHeight: 64,

? ? ? // inputHeight: app.globalData.inputHeight || 64,

? ? })

? ? var pages = getCurrentPages();

? ? var currentPage = pages[pages.length - 1];

? ? var url = currentPage.route; //獲取路由

? ? this.setData({

? ? ? ['navbarData.showBackImage']: pages.length > 1 ? true : false

? ? })

? ? var marginRight = 0;

? ? if (this.data.navbarData.showHome && this.data.navbarData.showBackImage) {

? ? ? marginRight = 186;

? ? } else if (!this.data.navbarData.showHome && this.data.navbarData.showBackImage) {

? ? ? marginRight = 84;

? ? } else if (this.data.navbarData.showHome && !this.data.navbarData.showBackImage) {

? ? ? marginRight = 102;

? ? } else if (this.data.navbarData.eye) {

? ? ? marginRight = 102;

? ? } else {

? ? ? marginRight = 0;

? ? }

? ? this.setData({

? ? ? marginRight: marginRight

? ? })

? },

? methods: {

? ? // 返回上一頁面

? ? _navback() {

? ? ? wx.navigateBack()

? ? },

? ? _eyeBtn() {

? ? ? if (this.data.open) {

? ? ? ? this.setData({

? ? ? ? ? open: false

? ? ? ? })

? ? ? } else {

? ? ? ? this.setData({

? ? ? ? ? open: true

? ? ? ? })

? ? ? }

? ? ? this.triggerEvent("openClick", {open:this.data.open})

? ? },

? ? //返回到首頁

? ? _backhome() {

? ? ? wx.switchTab({

? ? ? ? url: '/pages/index/index',

? ? ? })

? ? },

? }

})

css

/* headTemplates/head/head.wxss */

/* 頂部要固定定位? 標題要居中? 自定義按鈕和標題要和右邊微信原生的膠囊上下對齊 */

.nav-wrap {

? position: fixed;

? width: 100%;

? top: 0;

? /* background: red; */

? color: #000;

? z-index: 9999999;

}

/* .btnOpen{

? width: 100rpx;

? height: 100%;

? text-align: center;

}

.btnOpen image{

? width: 52rpx;

? height: 36rpx;

} */

.nav-title-box{

? ? /* display: flex;

? ? align-items: center;

? ? justify-content: flex-start; */

? ? position: relative;

}

.nav-title{

? ? /* margin: 0 auto;

? ? flex:1; */

? ? width: 100%;

? ? text-align:center;

? ? color: #333;

? ? font-size: 32rpx;

? ? font-weight:bold;

? ? z-index: 1000000

}

.backBox{

position: absolute;

? ? top: 0;

? ? left: 0;

? ? z-index: 1000001

}

.back-pre,.back-pres,.home-pre{

? ? /* display: flex;

? ? align-items: center;

? ? justify-content: center; */

? ? float: left;

? ? /* margin: 22rpx; */

}

.back-pre-image{

? ? width: 13rpx;

? ? height: 24rpx;

? ? margin-top: 4rpx;

? ? padding: 10rpx 22rpx;

}

.back-pres-image{

? ? width: 52rpx;

? ? height: 36rpx;

? ? /* margin-top: 4rpx; */

? ? margin: 0 22rpx;

}

.home-pre-image{

? ? width: 42rpx;

? ? height: 42rpx;

? ? padding: 10rpx 30rpx;

}

.home-pre .line{

? ? width: 1px;

? ? height: 36rpx;

? ? background:rgba(204,204,204,1);

}

.nav-search{

? ? width: 100%;


}

.nav_seach_left{

? ? /* width:438rpx; */

? ? /* height:64rpx; */

? ? background:rgba(245,245,245,1);

? ? /* background:#000; */

? ? border-radius:32rpx;

? ? position:relative;

? ? /* margin-top:10rpx; */

? ? float: left;

? ? margin-left:16rpx;

? ? /* margin-top: -4rpx; */

? ? display: flex;

}

.nav_seach_left1{

? ? width:60%;

}

.nav_seach_left2{

? ? width:66%;

}

.nav_seach_logo{

? ? width: 32rpx;

? ? height:32rpx;

? ? margin-left: 24rpx;

? ? /* position:absolute;

? ? left: 29rpx;

? ? top:18rpx; */

}

.nav_seach_box_right{

? ? width: 72%;

? ? /* height:64rpx;

? ? line-height:64rpx; */

? ? /* margin:auto; */

? ? padding-left:12rpx;

? ? font-size:28rpx;

? ? font-weight:400;

? ? color:rgba(140,140,140,1);

? ? overflow: hidden;

? ? text-overflow:ellipsis;

? ? white-space: nowrap;

}

.placeholderstyle{

? ? font-size:28rpx;

? ? font-weight:400;

? ? color:rgba(140,140,140,1);

}

.null_logo{

? ? /* width: 200rpx; */

? ? height:60rpx;

? ? text-align:right;

? ? position:absolute;

? ? right: 30rpx;

? ? /* top:2rpx; */

}

/* .null_logo image{

? ? width: 24rpx;

? ? height:24rpx;

? ? vertical-align: top;

? ? margin-top:20rpx;

} */

wxml

<!--headTemplates/head/head.wxml-->

<view class='nav-wrap' style='height: {{totalTopHeight}}px;border-bottom:1px solid rgba(237,237,237,1);background:#fff;'>

? <view style='height: {{statusBarHeight}}px;'></view>

? <!-- // 導航欄 中間的標題 -->

? <view class='nav-title-box' style='height:{{titleBarHeight}}px;line-height:{{titleBarHeight}}px;'>

? ? <view class='backBox'>

? ? ? <view bindtap='_navback' class='back-pre' wx:if='{{navbarData.showBackImage?(!share?navbarData.showBackImage:navbarData.showBackImage):navbarData.showBackImage}}'>

? ? ? ? <image class='back-pre-image' src='/images/back-pre.png' mode='aspectFill' style="margin-top:{{(totalTopHeight-24)/2}}rpx"></image>

? ? ? </view>

? ? ? <view bindtap='_eyeBtn' class='back-pres' wx:if='{{navbarData.eye}}'>

? ? ? ? <image class='back-pres-image' wx:if='{{open==false}}' src='/assets/imgs/closeye.png' mode='aspectFill'></image>

? ? ? ? <image class='back-pres-image' wx:if='{{open}}' src='../../../assets/imgs/openeye.png' mode='aspectFill'></image>

? ? ? </view>

? ? ? <view class='home-pre' wx:if="{{navbarData.showHome}}" bindtap='_backhome'>

? ? ? ? <view class='line'></view>

? ? ? ? <image class='home-pre-image' src='/assets/imgs/home@2x.png' mode='aspectFill'></image>

? ? ? </view>

? ? </view>

? ? <view class='nav-title' wx:if='{{navbarData.titleShow}}' style='line-height:{{titleBarHeight}}px;margin-right:{{marginRight}}rpx;'>{{navbarData.title}}</view>

? </view>

</view>

json


{

? ? "component": true,

? ? "usingComponents": {}

}

3.app.js

全局信息

totalTopHeight: 0, //自定義頭部總高度

? ? statusBarHeight: 0, //自定義頭部狀態(tài)欄高度

? ? titleBarHeight: 0, //自定義頭部標題欄高度

onShow


//獲設(shè)備型號

? ? wx.getSystemInfo({

? ? ? success: function (res) {

? ? ? ? self.globalData.phonexh = res.model.indexOf("iPhone X");

? ? ? ? self.globalData.statusBarHeight = res.statusBarHeight;

? ? ? ? var menuButton = wx.getMenuButtonBoundingClientRect();

? ? ? ? var isiOS = res.system.indexOf('iOS') > -1;

? ? ? ? if (!isiOS) {

? ? ? ? ? self.globalData.totalTopHeight = 48 + res.statusBarHeight;

? ? ? ? } else {

? ? ? ? ? self.globalData.totalTopHeight = 44 + res.statusBarHeight;

? ? ? ? }

? ? ? ? self.globalData.titleBarHeight = self.globalData.totalTopHeight - res.statusBarHeight;

? ? ? }

? ? })

4.頁面中使用


wxml


<!--頭部-->

<nav-bar bind:openClick='openVal' navbar-data='{{nvabarData}}'></nav-bar>

js


// 頭部組件所需的參數(shù)

? ? nvabarData: {

? ? ? showHome: false, //是否顯示Home

? ? ? showBackImage: true, //是否顯示左上角返回圖片

? ? ? titleShow: true, //是否顯示中間標題

? ? ? title: '登錄', //導航欄 中間的標題

? ? ? eye: false,

? ? },

? ? // 此頁面 頁面內(nèi)容距最頂部的距離

? ? height: app.globalData.totalTopHeight,

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末就斤,一起剝皮案震驚了整個濱河市培漏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌条获,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蒋歌,死亡現(xiàn)場離奇詭異帅掘,居然都是意外死亡,警方通過查閱死者的電腦和手機堂油,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門修档,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人府框,你說我怎么就攤上這事吱窝。” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長背桐。 經(jīng)常有香客問我须板,道長,這世上最難降的妖魔是什么鲫惶? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮实抡,結(jié)果婚禮上欠母,老公的妹妹穿的比我還像新娘。我一直安慰自己吆寨,他們只是感情好赏淌,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著啄清,像睡著了一般六水。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上辣卒,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天掷贾,我揣著相機與錄音,去河邊找鬼荣茫。 笑死想帅,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的啡莉。 我是一名探鬼主播港准,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼咧欣!你這毒婦竟也來了浅缸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤魄咕,失蹤者是張志新(化名)和其女友劉穎衩椒,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哮兰,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡烟具,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了奠蹬。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片朝聋。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖囤躁,靈堂內(nèi)的尸體忽然破棺而出冀痕,到底是詐尸還是另有隱情荔睹,我是刑警寧澤,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布言蛇,位于F島的核電站僻他,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏腊尚。R本人自食惡果不足惜吨拗,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望婿斥。 院中可真熱鬧劝篷,春花似錦、人聲如沸民宿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽活鹰。三九已至哈恰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間志群,已是汗流浹背着绷。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留锌云,地道東北人荠医。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像宾抓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子豫喧,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355