微信小程序导航栏自定义

微信小程序缺省的导航栏既不够美观,又没有特色,相信很多人都希望能够拥有一个既美观又有特色的小程序导航栏。刚好公司有个项目需要开发一款小程序对公司开发的产品进行设置。不多说,直接介绍如何自定义美观又有特色的导航栏。

1、在app.json文件中,增加如下两处代码:

    1)、       "navigationStyle": "custom"

    2)、 "usingComponents": {

    "navigation-bar": "/navigationbar/navigationbar"

  }

如下图所示:

2、在app.js文件中增加如下两处代码:

        1)

    const systemInfo = wx.getWindowInfo();

    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();

    this.globalData.navBarHeight = systemInfo.statusBarHeight + 60;

    this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;

    this.globalData.menuTop = menuButtonInfo.top;

    this.globalData.menuHeight = menuButtonInfo.height;

        2)

   navBarHeight: 0,

    menuRight : 0,

    menuTop: 0,

    menuHeight : 0,

3、在index.wxml文件中增加如下代码:

<navigation-bar default-data="{{defaultData}}"></navigation-bar>

4、最后,开发属于自己的导航栏Components,文件如下所示:

navigationbar.js:

// navigationbar/navigationbar.wxml.js
const app = getApp()
Component({
    properties: {
        // defaultData(父页面传递的数据-就是引用组件的页面)
        defaultData: {
            type: Object,
            value: {
                title: "雾化器设置"
            },
            observer: function(newVal, oldVal) {}
        }
    },
    data: {
        navBarHeight: app.globalData.navBarHeight,
        menuRight: app.globalData.menuRight,
        menuTop: app.globalData.menuTop,
        menuHeight: app.globalData.menuHeight,
    },
    attached: function() {},
    methods: {}
})

navigationbar.json

{
  "component": true,
  "usingComponents": {}
}

navigationbar.wxml

<!-- 自定义顶部栏 -->
<view class="nav-bar" style="height:{{navBarHeight}}px;">
<image style="position:absolute; left:40rpx;top:90rpx;width:472rpx;height:90rpx" src="../icon/logo.png"></image>

</view>
<view style="height:{{navBarHeight}}px;"></view>

navigationbar.wxss

/* navigationbar/navigationbar.wxml.wxss */

.nav-bar{ position: fixed; width: 100%; top: 0; color: rgb(41, 40, 40); background: #fff;}
.nav-bar .search{ width: 60%; color: #333; font-size: 14px; background: #fff; position: absolute; border-radius: 50px; background: #ddd; padding-left: 14px;}

至此,您就可以拥有属于自己的导航栏了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远的元子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值