美文网首页
小程序自定义导航栏并全局使用

小程序自定义导航栏并全局使用

作者: zhanglizzy | 来源:发表于2021-03-25 14:56 被阅读0次

    首先查看官方文档知识点
    1、什么情况可自定义?怎么自定义?


    1616654271860.jpg 1616654322744.jpg

    具体代码如下:
    在app.json文件中

    "window": {
        "navigationStyle": "custom"
      },
    

    全局引用再在app.json文件中加入如下两行

    "usingComponents": {
        "navigationBar": "./components/navigationBar/navigationBar"
      },
    

    仅单个页面引用就只在单个页面的app.json种引用上面usingComponents

    再看官方文档知识点
    2、怎么使用components?


    1616654397827.jpg

    创建一个components文件夹存储所有的自定义组件,再创建一个navigationBar的文件夹组件。

    注:每个机型的状态栏高度不是固定的,在app.js启动时可获取状态栏高度并存入global全局data中可供自定义组件动态设置导航栏高度

    //获取系统信息
        wx.getSystemInfo({
          success(res) {
            that.globalData.statusBarHeight = res.statusBarHeight;
          },
        });
    

    navigationBar.js

    const app = getApp()
    
    Component({
      properties: {
        title: {
          type: String,
          value: ''
        },
        back: {
          type: Boolean,
          value: true
        },
        backHome: {
          type: Boolean,
          value: false
        }
      },
      data: {
        statusBarHeight: app.globalData.statusBarHeight + 'px',
        navigationBarHeight: (app.globalData.statusBarHeight + 44) + 'px'
      },
    
      methods: {
        backHome: function () {
          let pages = getCurrentPages()
          wx.navigateBack({
            delta: pages.length
          })
        },
        back: function () {
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
    

    navigationBar.wxml

    <view class="navbar" style="{{'height: ' + navigationBarHeight}}">
      <view style="{{'height: ' + statusBarHeight}}"></view>
      <view class='title-container'>
        <view class='capsule' wx:if="{{ back || backHome }}">
          <view bindtap='back' wx:if="{{back}}">
            <image src='/images/arrow_white.png'></image>         
          </view>
          <view bindtap='backHome' wx:if="{{backHome}}">
            <image src='/images/arrow_blue.png'></image>
          </view>
        </view>
        <view class='title'>{{title}}</view>
      </view>
    </view>
    

    navigationBar.wxss

    .navbar {
      width: 100%;
      background-color: pink;
      /* 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 #fff;
      border-radius: 16px;
      display: flex;
      align-items: center;
    }
    .capsule > view {
      width: 45px;
      height: 60%;
      position: relative;
    }
    .capsule > view:nth-child(2) {
      border-left: 1px solid #fff;  
    }
    .capsule image {
      width: 50%;
      height: 100%;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
    }
    .title {
      color: white;
      position: absolute;
      top: 6px;
      left: 104px;
      right: 104px;
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    

    3、自定义导航栏已设置,自定义组件已创建,那么剩下去父组件中引用自定义的子组件了。

    <navigationBar title="ddddd" backHome="true"></navigationBar>
    

    这个title和backHome都是子组件中properties里定义的需要传入的参数。

    相关文章

      网友评论

          本文标题:小程序自定义导航栏并全局使用

          本文链接:https://www.haomeiwen.com/subject/gualhltx.html