微信小程序自定义navbar

自定义导航栏

项目中需求: 导航栏在插画上, 这时需要采用自定义导航栏实现.

在json配置中把navigationStyle设置为custom, 这时只会保留右上角胶囊按钮.

公共的导航栏编写一个自定义组件调用.

欢迎大神们指教…

效果图

在这里插入图片描述

创建组件

类似于页面,一个自定义组件由 json wxml wxss js 4个文件组成.

首先需要在 json 文件中进行自定义组件声明, 将component 字段设为 true

custom-component

Component

  • 新建文件夹components
  • components新建组件navbar
  • navbar组件对应文件上写code

json

首先在json文件中, 将 component 字段设为 true 可这一组文件设为自定义组件

{
    "component": true
}

wxml

<!--components/navbar/navbar.wxml-->
<view>
    <view class='nav bg' style='height:{{navH}}px'>
        <view class='nav-title'>
            {{pageName}}
            <image src='/libs/assets/images/goBack.png' mode='aspectFit' class='back' bindtap='navBack'></image>
        </view>
    </view>
</view>

wxss

/* components/navbar/navbar.wxss */

.nav {
    width: 100%;
    overflow: hidden;
    position: fixed;
    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: 34rpx;
    color: #fff;
    letter-spacing: 1px;
}

.nav .back {
    width: 20px;
    height: 20px;
    position: absolute;
    bottom: 0;
    left: 0;
    padding: 10px 15px;
}

.bg {
    background-color: #345098;
}

js

// components/navbar/navbar.js

const app = getApp();

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        pageName: String,
    },

    /**
     * 组件的初始数据
     */
    data: {
        navH: 0,
        navbarData: {
            showCapsule: 1
        }
    },
    lifetimes: {
        attached: function () {
            this.setData({
                navH: app.globalData.navHeight
            })
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        //回退
        navBack: function () {
            wx.navigateBack({
                delta: 1
            })
        }
    }
})

组件使用步骤:

  1. 需在app全局中进行相关操作, 2. 在需要中page中使用该组件

app.json

app.json下的window配置 navigationStylecustom

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

app.js

注意: 在app.js全局定义导航高度navHeight, 否则有可能遇到navHeightundefined

/**
 * 获取手机系统信息
 */
wx.getSystemInfo({
    let that = this;
    success: res => {
        //导航高度  状态栏高度 + 导航栏高度44(这时写46)
        that.globalData.navHeight = res.statusBarHeight + 46;
    }, fail(err) {
        console.log(err);
    }
})

/**
* 全局变量
*/
globalData: {
    navHeight: 0,
}

page.js

在需要的页面.js中获取 导航栏高度

const app = getApp();

Page({
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight
    })
  },
})

page.wxml

在需要的页面.wxml中 使用navbar组件

注意: 默认会给导航栏覆盖, 需设置属性等margin-topheight:calc(100vh - {{navH}}各所需

<view class='indexWrap'>
   <!-- navbar组件 -->
  <navbar page-name="当前title"></navbar>
    
   <!--内容-->
  <view class='content' style='margin-top: {{navH}}px'>
  </view>
</view>

page.json

引入navbar组件, 注意路径

"usingComponents": {
    "nav-bar": "/commpents/navbar/navbar",
}

图二插画背景使用

图二有插画, 这时不能引入公共的组件直接使用了. 需进行修改如下

wxml

使用 wx.getSystemInfoSync()['statusBarHeight']则能获取到顶部状态栏的高度,单位为px.
iPhonex、xr、max等状态栏高度为90px, 其他小屏iPhone设备为66px

<view class='userWrap'>
    <!-- 背景插画 -->
    <view class='userBg'>

        <!-- 导航栏  style='height:{{navH}}px' -->
        <view class='customNav' style='padding-top:{{navH}}px'>
            <view class='nav-title'>
                我的
                <image src='/libs/assets/images/goBack.png' mode='aspectFit' class='back' bindtap='navBack'></image>
            </view>
        </view>
        
        <!-- 内容同样距离顶部状态栏的高度-->
        <view class="headBox" style='padding-top:{{navH}}px'>
        	
        </view>
    </view>
</view>

wxss

.userBg {
    margin: 0 auto;
    @include wh(100%, 300rpx);
    @include bis("背景图片");
}

/** 自定义导航栏 */
.customNav {
    width: 100%;
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    .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;
        color: #fff;
        letter-spacing: 1px;
    }
    .back {
        width: 20px;
        height: 20px;
        position: absolute;
        bottom: 0;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        padding: 0px 30rpx;
        display: flex;
        align-self: center;
        align-items: center;
    }
}

感谢大神们

参考一

参考二

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Uniapp微信小程序可以通过以下步骤自定义导航栏: 1. 在App.vue中定义全局导航栏组件,例如: ```vue <template> <view class="nav-bar"> <view class="nav-bar-left" @click="$router.back()"> <text class="iconfont icon-fanhui"></text> </view> <view class="nav-bar-title"> <slot name="title"></slot> </view> <view class="nav-bar-right"></view> </view> </template> <script> export default { name: 'NavBar' } </script> <style scoped> .nav-bar { height: 44px; display: flex; justify-content: space-between; align-items: center; background-color: #fff; box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.1); position: fixed; top: 0; left: 0; right: 0; z-index: 999; } .nav-bar-left, .nav-bar-right { width: 44px; height: 44px; display: flex; justify-content: center; align-items: center; } .nav-bar-title { flex: 1; text-align: center; font-size: 18px; font-weight: bold; color: #333; } </style> ``` 2. 在需要使用导航栏的页面中引入导航栏组件并传入标题,例如: ```vue <template> <view> <nav-bar> <template v-slot:title> 自定义导航栏 </template> </nav-bar> <view class="content"> <!-- 页面内容 --> </view> </view> </template> <script> import NavBar from '@/components/NavBar.vue' export default { components: { NavBar } } </script> <style> .content { margin-top: 44px; } </style> ``` 3. 根据需要在导航栏组件中添加左右按钮,并在按钮的click事件中处理相应的逻辑,例如: ```vue <template> <view class="nav-bar"> <view class="nav-bar-left" @click="$router.back()"> <text class="iconfont icon-fanhui"></text> </view> <view class="nav-bar-title"> <slot name="title"></slot> </view> <view class="nav-bar-right" @click="handleRightButtonClick()"> <text class="iconfont icon-add"></text> </view> </view> </template> <script> export default { name: 'NavBar', methods: { handleRightButtonClick() { // 处理右侧按钮点击事件 } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值