微信小程序自定义标题栏(搜索框)

(小白学习笔记)

1.首先在需要更改页面的json文件中加入"navigationStyle":"custom" "usingComponents" 同级

{
    "usingComponents":{},
    "navigationStyle":"custom"
}

如果需要整个项目所有页面都使用自定义标题栏,可以直接在app.json中的window中加入"navigationStyle":"custom",那么你整个项目都会使用自己自定义的标题栏了

2.其次在index.wxml中加入

<!--index.wxml-->
<view>
    <!-- 自定义头部 -->
      <view class='nav bg-white' style='height:{{navH}}px'>
            <view class='nav-title'>
                  <view class="INinputheader">
                <icon class="INsearchicon" type="search" size="12"></icon>
                <input class="weui-input" name="search" placeholder="请输入关键字搜索"/>
            </view>
                </view>
          </view>
        <view class="test_b" style="margin-top:{{navH}}px">这是一个边框</view>
</view>

继续在index.wxss添加样式(如果需要全局就在app.wxss) 

/* 头部搜索 */
.nav {
    width: 100%;
    overflow: hidden;
    position: relative;
    top: 0;
    left: 0;
    z-index: 10000000;
    position: fixed;
}

.nav-title {
    width: 100%;
    height: 45px;
    line-height: 45px;
    text-align: center;
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: 100000;
    font-family: PingFang-SC-Medium;
    font-size: 36rpx;
    letter-spacing: 2px;
}

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

.bg-white {
    background-color: rgb(255, 0, 0);
}

.bg-gray {
    background-color: #f7f7f7;
}

.overflow {
    overflow: auto;
}

.hidden {
    overflow: hidden;
}

.INinputheader {
    width: 60%;
    height: 30px;
    background: #efefef;
    border-radius: 30px;
    font-size: 14px;
    margin-top: 7px;
    position: relative;
    float: left;
    margin-left: 12px;
}

.INsearchicon {
    position: absolute;
    left: 20rpx;
    width: 20rpx;
    height:20rpx;
}

.weui-input {
    height: 30px;
    line-height: 30px;
    text-align: left;
    padding-left: 30px;
    letter-spacing: 0px;
}

.INtab {
    text-align: left;
    margin-left: 12px;
    color: #fff;
}
.text_b{
    display:flex;
    flex-decoration:column;
    align-items:center;
    height:1000rpx;
}

现在

wxml和wxss都写好了,因为手机各有差异,为了适配各种手机的顶部,常规的我们就需要获取标题栏的高度以此实现动态绑定高度,为了预防onLoad有时候不触发,我们直接在app.js的onLaunch的函数中加入代码:

onLaunch: function () {
        // 获取顶部栏信息
        wx.getSystemInfo({
            success: res => {
                //导航高度
                this.globalData.navHeight = res.statusBarHeight + 46;
            },
            fail(err) {
                console.log(err);
            }
        })
    }

直接获取高度,存放在与onLaunch同级

    globalData:{
        userInfo:null,
        navHeight:0
      },

接着,在需要使用的页面的js文件中先获取app,加入代码

const App = getApp();//设立顶部栏高度

获取到App实例,在onLoad中把获取到的放在data同级,即可在wxml中动态绑定使用

onLoad: function (options) {
        //自定义头部方法
        this.setData({
          navH: App.globalData.navHeight
        });
      },

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在支付宝小程序导航栏中添加搜索框,您可以按照以下步骤进行操作: 1. 在 app.json 文件中添加 "search": true。 ``` { "pages": [ "pages/index/index", "pages/mine/mine" ], "window": { "navigationBarTitleText": "我的小程序", "navigationBarBackgroundColor": "#FFFFFF", "navigationBarTextStyle": "black" }, "search": true } ``` 2. 在相应页面的 .json 文件中添加 "usingComponents" 字段,并引入 searchbar 组件。 ``` { "usingComponents": { "searchbar": "/components/searchbar/searchbar" } } ``` 3. 在相应页面的 .wxml 文件中添加 searchbar 组件,并设置相应的属性。 ``` <searchbar placeholder="请输入搜索内容"></searchbar> ``` 4. 在 components/searchbar/searchbar.wxml 文件中编写 searchbar 组件的模板代码。 ``` <view class="searchbar"> <input type="text" class="searchbar-input" placeholder="{{placeholder}}" /> <view class="searchbar-btn">搜索</view> </view> ``` 5. 在 app.wxss 文件中添加相应的样式代码。 ``` .searchbar { display: flex; align-items: center; height: 44px; background-color: #FFFFFF; border-bottom: 1rpx solid #ECECEC; } .searchbar-input { flex: 1; margin-left: 20rpx; font-size: 14px; color: #333333; border: none; outline: none; } .searchbar-btn { width: 60rpx; height: 30rpx; line-height: 30rpx; text-align: center; background-color: #FFC900; border-radius: 15rpx; margin-right: 20rpx; font-size: 14px; color: #FFFFFF; } ``` 这样,您就可以在支付宝小程序导航栏中添加搜索框了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值