微信小程序自定義導航欄配置(頂部欄搜索框)


配置信息app.json:

  "window": {
    "backgroundTextStyle": "light",//非自定義字體位置
    "navigationBarBackgroundColor": "#fff",//非自定義背景顏色
    "navigationBarTitleText": "項目名",//標題
    "navigationBarTextStyle": "white",//右上交三個點和關閉按鈕顏色只能配置黑白
    "navigationStyle": "custom"//自定義需要配置的內容
  },

因為要適配各種手機app.js配置

 // 獲取頂部欄信息
    wx.getSystemInfo({
      success: res => {
        //導航高度
        this.globalData.navHeight = res.statusBarHeight + 46;
      }, fail(err) {
        console.log(err);
      }
    })

在globalData也配置信息

 globalData: {
    userInfo: null,
    navHeight: 0
  }

app.js完整配置dome

App({
  onLaunch: function () {
    // 獲取頂部欄信息
    wx.getSystemInfo({
      success: res => {
        //導航高度
        this.globalData.navHeight = res.statusBarHeight + 46;
      }, fail(err) {
        console.log(err);
      }
    })
  },
  globalData: {
    userInfo: null,
    navHeight: 0
  }
})

app.wxss樣式

/* 自定義頂部欄高度 */
.nav{
  width: 100%;
  overflow: hidden;
  position: relative;
  top: 0;
  left: 0;
  z-index: 10;
}
.nav-title{
  width: 100%;
  height: 45px;
  line-height: 45px;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 10;
  font-family:PingFang-SC-Medium;
  font-size:36rpx;
  letter-spacing:2px;
}
.nav .back{
  width: 22px;
  height: 22px;
  position: absolute;
  bottom: 0;
  left: 0;
  padding: 10px 15px;
}
.bg-white{
  background-color:#FFCE58;
}
.bg-gray{
  background-color: #f7f7f7;
}
.overflow{
  overflow: auto;
}
.hidden{
  overflow: hidden;
}
.INinputheader{
  width:60%;
  height:30px;
  background:rgba(255,255,255,1);
  border-radius:30px;
  font-size: 14px;
  margin-top:7px;
  position: relative; 
  float: left;
  margin-left:12px;
}
.INsearchicon{
  position: absolute;
  left:12px;
}
.weui-input{
  height: 30px;
  line-height: 30px;
  text-align: left; 
  padding-left: 30px;
  letter-spacing:0px;
}
.INtab{
  text-align: left;
  margin-left: 12px;
  color: #fff;
}

現在配置完開始使用

index.wxml頁面引用

<view>
  <!-- 自定義頭部 -->
  <view class='nav bg-white' style='height:{{navH}}px'>
    <view class='nav-title'>
      <view class="INinputheader">
        <icon class="INsearchicon" type="search" size="12"/>
        <input class="weui-input" placeholder="搜索學習內容"/>
      </view>
    </view>
  </view>
  <scroll-view class='bg-gray overflow' style='height:calc(100vh - {{navH}}px)' scroll-y >
    <view class='hidden'>
      <!-- 正文 -->
      <view>當前位置信息:</view>
    </view>
  </scroll-view>
</view>

index.js頁面引用

const App = getApp();//設立頂部欄高度
Page({
    data:{},
    onLoad: function (options) {
    //自定義頭部方法
    this.setData({
      navH: App.globalData.navHeight
    });
  }
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM