微信小程序怎么自定义导航栏

1、第一先获取状态栏和胶囊的高度、

  /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
            this.getHei()
        this.getIntegralShop()
        this.getUserInfo()
        this.setData({
            //    状态栏的高度
            statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'] + 'px',
            //    胶囊的高度 也就是自定导航栏的高度
            navheight: wx.getMenuButtonBoundingClientRect()['height'] + 'px',
            //    状态栏加胶囊的高度 加上下的padding的高度 12
            pagetop: wx.getMenuButtonBoundingClientRect()['height'] + 

            wx.getSystemInfoSync()['statusBarHeight'] + 12 + 'px',
            baseUrl:getbaseUrl()+'/image/'
        })
        // console.log(this.data.statusBarHeight)
        // console.log(this.data.navheight)
        // console.log(this.data.pagetop)
    },

    //并且监听页面的滚动

      onPageScroll(e){
        // console.log(e.detail.scrollTop)
        this.setData({
            scrollTop:e.detail.scrollTop
            })
        },

2、html的结构

    
 <view class="navibg {{scrollTop>10?'navstyl':'nonavstyl'}}">
          //状态栏的高度,主要是用来占坑
        <view style="height:{{statusBarHeight}};"></view>
        <view class="backBot">
            <view class="iconfont1" catchtap="goBack">
                <text class="bot1 iconfont icon-zuo {{scrollTop>10?'bot3':'bot2'}}"></text>
            </view>
            <view  class="{{scrollTop>10?'naviName':'nonaviName'}}">
                积分商城
            </view>
            <view style="width: 140rpx;"></view>
        </view>
    </view>

3、相关样式

/* pages/integralShop/integralShop.wxss */

.integral .navibg{
    width: 100%;
    position: fixed;
    top: 0;
    right: 0;
    z-index: 999;
}
.integral .navibg .backBot{
    display: flex;
    /* line-height: 32px; */
    padding: 12rpx 0;
}
.integral .navibg .backBot .iconfont1{
    width: 140rpx; 
    display: flex;
    align-items: center;
    justify-content: center;
}
.integral .navibg .backBot .iconfont1 .bot1{
    display: flex;
    align-items: center;
    font-size: 40rpx;
    padding: 20rpx;
    line-height: 64rpx;
    color: #fff;
    border-radius: 50%;
    width: 30rpx;
    height: 30rpx;
}
.integral .navibg .backBot .iconfont1 .bot2{
    color: #fff;
    background-color: rgba(0,0,0,.5);
}
.integral .navibg .backBot .iconfont1 .bot3{
    color: #000;
}
.integral .navibg .backBot .naviName{
    flex: 1;
    opacity: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height:64rpx ;
    transition: all 1.5s;
}
.integral .navibg .backBot .nonaviName{
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    opacity: 0;
    text-align: center;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值