最近公司准備做app,最終決定使用uni-app框架開發,但是當把設計圖給我的時候我心里有點沒底,因為他的設計圖頂部長成這個樣子:

因為這個功能在小程序是根本無法實現的,可能受這個影響,我感覺好像實現不了,但是我還是回頭看了看文檔,才發現,這個功能是可以實現的,只需要在pages.json中做一些配置即可
這個在官方稱作app-plus,可以自定義導航區域,具體配置如下:
"pages": [ { "path": "pages/index/index", "style": { "navigationBarBackgroundColor": "#00c170", "app-plus": { "bounce": "none", "titleNView": { "buttons": [ { "text": "地圖", "fontSize":"16", "float": "right", "color":"#fff" }, { "text": "唐山", "fontSize":"16", "float": "left", "color":"#fff" } ], "searchInput":{ "align": "center", "placeholder": "請輸入查找房源信息", "borderRadius":"50upx", "backgroundColor": "#fff" } } } } }
]
效果如下:

你可能會問,我的點擊事件和輸入框事件如何監聽?
uni-app給出了相應的api,onNavigationBarButtonTap和onNavigationBarSearchInputChanged,寫在響應的頁面中即可:
export default { onNavigationBarButtonTap() { console.log("你點擊了按鈕") }, onNavigationBarSearchInputChanged () { console.log("你輸入了信息") } }
打印結果:

但是按鈕有兩個,只有一個按鈕事件怎么辦?還有輸入框的文字如何獲取?其實這兩個函數接收一個值,就是相對應的信息:
export default { onNavigationBarButtonTap(val) { console.log(val) }, onNavigationBarSearchInputChanged (val) { console.log(val) } }
打印結果:

按鈕事件根據對應的text判斷即可,而輸入框監聽的不是change事件,是input事件,即輸入后即可監聽到而不是失焦
詳細配置可查閱官方文檔: 傳送門
如果還不夠詳細,來這里
你以為這就完了?NoNoNo,眼尖的同學發現我做的和設計圖還是有區別的,右邊地圖有一個icon我沒有寫,如果按照上邊的方法是不能加的,但是我們可以去掉導航欄自定義
page.json里每個頁面的導航欄是默認開啟的,有一個navigationStyle屬性,默認值是default,我們把它改成custom就能把他去掉了:
{ "path": "pages/index/index", "style": { "navigationStyle":"custom" }
但是 移動端導航依然在,這就需要我們使用 titleNView這個屬性了,它是用來專門設置導航欄的,具體如下:
{ "path" : "pages/secondPage/secondPage", "style" : { "navigationStyle": "custom", "app-plus": { "titleNView": false } } }
然后我們自己就可以寫一套導航了,最后效果如下:

這里有一個坑,除了要給這個導航設置固定定位外,實際上手機最上方的狀態欄,也就是這個位置是透明的,因為我們把默認的導航去掉了:

所以我們在寫導航的時候上方的內邊距是比下方的要大那么一點,這樣才能保證覆蓋狀態欄。
下面是我寫的源碼:
<template>
<view class="head">
<view class="header-wrap">
<view class="index-header">
<text class="address" v-if="leftWords">{{leftWords}}</text>
<view class="input-wrap" v-if="input">
<input type="text"
placeholder="請輸入搜索"
v-model="value"
@change="inputChange"/>
<text class="iconfont iconfangdajing"></text>
</view>
<view class="map-wrap"
v-if="rightWords||rightIcon"
@click="rightClick">
<text class="iconfont" :class="rightIcon"></text>
<text>{{rightWords}}</text>
</view>
</view>
</view>
<view class="blank"></view>
</view>
</template>
<script>
export default {
name: "IndexHeader",
props: [
'leftWords',
'input',
'rightIcon',
'rightWords'
],
data () {
return {
value: ''
}
},
methods: {
inputChange: function () {
this.$emit('change',this.value)
},
rightClick: function () {
this.$emit("rightClick")
}
}
}
</script>
<style lang="scss">
$color-base: #00c16f;
$words-color-base: #333333;
$words-color-light: #999999;
.header-wrap {
width: 100%;
position: fixed;
top: 0;
z-index: 999;
.index-header {
height: 88upx;
line-height: 88upx;
padding: 0 30upx;
padding-top: 40upx;
background-color: $color-base;
font-size: 28upx;
color: #fff;
display: flex;
align-items: center;
justify-content: space-between;
.address {
font-size: 26upx;
}
.input-wrap {
width: 500upx;
height: 70upx;
padding: 10upx 30upx 10upx 100upx;
box-sizing: border-box;
background-color: #fff;
border-radius: 50upx;
color: $words-color-base;
position: relative;
text {
position: absolute;
left: 40upx;
top: -8upx;
color: $words-color-light;
font-size: 30upx;
}
}
.map-wrap {
.iconfont {
font-size: 32upx;
margin-right: 5upx;
}
text {
font-size: 26upx;
}
}
}
}
.blank {
height: 126upx;
}
</style>
更多前端資料關注【東八區的大閘蟹】免費領取

