zoukankan      html  css  js  c++  java
  • 小程序开发-自定义导航栏组件

    上一篇文章讲到了小程序设置全屏,隐藏导航栏的方式。

    主要是配置自定义导航栏"navigationStyle": "custom"

    这篇文章尝试自定义导航栏

    1. 首先,我们要获取状态栏的高度,以及胶囊的位置

    可以通过getSystemInfo 方法

     //获取菜单按钮(右上角胶囊按钮)的布局位置信息
      let menuButtonObject = wx.getMenuButtonBoundingClientRect();
      //获取系统信息
      wx.getSystemInfo({
        success: res => {
          //状态栏的高度
          let statusBarHeight = res.statusBarHeight,
          //胶囊按钮与顶部的距离
          navTop = menuButtonObject.top, 
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; 
          this.globalData.statusBarHeight = statusBarHeight ;//状态栏高度
          this.globalData.navHeight = navHeight;//导航栏高度
          this.globalData.navTop = navTop;//胶囊按钮与顶部的距离
          this.globalData.jnHeight = menuButtonObject.height;//胶囊的高度
          this.globalData.jnWidth = menuButtonObject.width;//胶囊的宽度
        },
        fail(err) {
          console.log(err);
        }
    

    2. 自定义导航栏 navigationBar

    • navigationBar.js 代码
    const app = getApp()
    
    Component({
    
      properties: {
        title: {
          type: String,
          value: 'weiChat'
        },
        back: {
          type: Boolean,
          value: false
        },
        home: {
          type: Boolean,
          value: false
        }
      },
      data: {
        navigationBarHeight: app.globalData.navHeight 
        statusBarHeight: app.globalData.statusBarHeight
      },
    
      methods: {
        backHome: function () {
          let pages = getCurrentPages()
          wx.navigateBack({
            delta: pages.length
          })
        },
        back: function () {
          wx.navigateBack({
            delta: 1
          })
        },
        loading: {
          type: Boolean,
          value: false
        }
      }
    })
    
    • navigationBar.wxss 代码
      白色导航栏,
    .navbar {
       100%;
      background-color: #fff;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 999;
    }
    .title-container {
      height: 40px;
      display: flex;
      align-items: center;
      position: relative;
    }
    .capsule {
      margin-left: 10px;
      height: 30px;
      background: rgba(255, 255, 255, 0.6);
      border: 1px solid #eee;
      border-radius: 16px;
      display: flex;
      align-items: center;
    }
    .capsule > view {
       45px;
      height: 60%;
      position: relative;
    }
    .capsule > view:nth-child(2) {
      border-left: 1px solid #eee;  
    }
    .capsule image {
       20px;
      height: 20px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-10px,-10px);
    }
    .title {
      position: absolute;
      top: 6px;
      left: 104px;
      right: 104px;
      height: 30px;
      line-height: 30px;
      font-size: 18px;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    .navBar-loader {
    	height: 18px;
    	 18px;
      display: inline-block;
      margin-right: 4px;
      vertical-align: middle;
    }
    
    
    • navigationBar.wxml 代码
    <view class="navbar" style="{{'height: ' + navigationBarHeight}}">
      <view style="{{'height: ' + statusBarHeight}}"></view>
      <view class='title-container'>
        <view class='capsule' wx:if="{{ back || home }}">
          <view bindtap='back' wx:if="{{back}}">
            <image src='/images/back.png'></image>         
          </view>
          <view bindtap='backHome' wx:if="{{home}}">
            <image src='/images/home_top.png'></image>
          </view>
        </view>
        <view class='title'><view wx:if="{{loading}}" class="navBar-loader"><view class="inner"></view></view>{{text}}</view>
      </view>
    </view>
    <view style="{{'height: ' + navigationBarHeight}};background: white;"></view>
    

    3. 使用

    <navigationBar title="{{title}}" loading="{{true}}" home="{{true}}" back="{{true}}"></navigationBar>
    
  • 相关阅读:
    oracle拆分逗号分隔字符串 实现split[转]
    Oracle 编译、调用或调试Procedure、Function或Package时卡死问题
    The log sequence numbers 1602631 and 1602631 in ibdata files do not match the log sequence number 2188207 in the ib_logfiles!
    win10下安装pl/sql developer 10.0.5.1710 报错ORA-12154
    Oracle EBS-SQL (INV-5):检查期间拉式物料领用记录数.sql
    Oracle EBS-SQL (INV-4):检查负库存记录数.sql
    Oracle EBS-SQL (WIP-16):检查期间手工下达的车间任务数.sql
    Oracle EBS-SQL (WIP-15):检查车间任务物料未发数量与现有量对照.sql
    Oracle EBS-SQL (WIP-14):检查车间需求与BOM差异对照.sql
    Oracle EBS-SQL (WIP-13):检查任务组件未选MRP净值.sql
  • 原文地址:https://www.cnblogs.com/limaostudio/p/13626840.html
Copyright © 2011-2022 走看看