亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何用uni-app實現(xiàn)頂部導航欄顯示按鈕和搜索框

 更新時間:2021年06月04日 14:33:10   作者:俗的太不一樣  
本文介紹了如何用uni-app實現(xiàn)頂部導航欄顯示按鈕和搜索框,感興趣的同學,可以參考下,并且試驗一下。

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

因為這個功能在小程序是根本無法實現(xiàn)的,可能受這個影響,我感覺好像實現(xiàn)不了,但是我還是回頭看了看文檔,才發(fā)現(xiàn),這個功能是可以實現(xiàn)的,只需要在pages.json中做一些配置即可

這個在官方稱作app-plus,可以自定義導航區(qū)域,具體配置如下:

"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"
                            }
                    }
                }
            }
        }    ]

效果如下:

你可能會問,我的點擊事件和輸入框事件如何監(jiān)聽?

uni-app給出了相應的api,onNavigationBarButtonTap和onNavigationBarSearchInputChanged,寫在響應的頁面中即可:

export default {
        onNavigationBarButtonTap() {
            console.log("你點擊了按鈕")
        },
        onNavigationBarSearchInputChanged () {
            console.log("你輸入了信息")
        }
}

打印結(jié)果:

但是按鈕有兩個,只有一個按鈕事件怎么辦?還有輸入框的文字如何獲???其實這兩個函數(shù)接收一個值,就是相對應的信息:

export default {
    onNavigationBarButtonTap(val) {
        console.log(val)
    },
    onNavigationBarSearchInputChanged (val) {
        console.log(val)
    }    
}

打印結(jié)果:

按鈕事件根據(jù)對應的text判斷即可,而輸入框監(jiān)聽的不是change事件,是input事件,即輸入后即可監(jiān)聽到而不是失焦

你以為這就完了?NoNoNo,眼尖的同學發(fā)現(xiàn)我做的和設計圖還是有區(qū)別的,右邊地圖有一個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  
                }
            }
        }

然后我們自己就可以寫一套導航了,最后效果如下:

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

所以我們在寫導航的時候上方的內(nèi)邊距是比下方的要大那么一點,這樣才能保證覆蓋狀態(tài)欄。

下面是我寫的源碼:

<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>

以上就是如何用uni-app實現(xiàn)頂部導航欄顯示按鈕和搜索框的詳細內(nèi)容,更多關于用uni-app實現(xiàn)頂部導航欄顯示按鈕和搜索框的資料請關注腳本之家其它相關文章!

相關文章

最新評論