300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 微信小程序自定义搜索导航栏

微信小程序自定义搜索导航栏

时间:2019-02-14 20:14:13

相关推荐

微信小程序自定义搜索导航栏

自定义微信小程序导航栏

因为项目需要,要实现如京东小程序类似的搜索导航栏-下图所示搜索框在导航栏中。参考了官方API最终实现了该有的效果,顺便吧此次实现的思路和代码记录下来。

一、拆解分析

按照正常的导航栏拆解来进行计算自定义导航栏的高度。根据下图中可以得到导航栏的高度等于:手机状态栏的高度 + 胶囊按钮高度(途中标注菜单栏)+ 以及胶囊按钮的上下边距。

根据微信API - getMenuButtonBoundingClientRect可以得到胶囊按钮的坐标和高宽此时可以得到的信息:

再根据getSystemInfo获得状态栏高度—statusBarHeight

然后计算出边距为:

margin = top(胶囊按钮上边界坐标) - statusBarHeight

最终得到导航栏的高度为:

customBarHeight = height + statusBarHeight + (margin * 2)

知道这些信息剩下的就是实现了,下面是全部实现代码。

二、代码实现

json文件设置

"navigationStyle": "custom"

JS代码部分

//index.js//获取应用实例const app = getApp()Page({data: {navHeight: '',menuButtonInfo: {},searchMarginTop: 0, // 搜索框上边距searchWidth: 0, // 搜索框宽度searchHeight: 0 // 搜索框高度},onLoad: function () {this.setData({menuButtonInfo: wx.getMenuButtonBoundingClientRect()})console.log(this.data.menuButtonInfo)const {top, width, height, right } = this.data.menuButtonInfowx.getSystemInfo({success: (res) => {const {statusBarHeight } = resconst margin = top - statusBarHeightthis.setData({navHeight: (height + statusBarHeight + (margin * 2)),searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距searchHeight: height, // 与胶囊按钮同高searchWidth: right - width // 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度})},})}})

wxml代码

<!--index.wxml--><view class="custom-bar" style="height:{{navHeight}}px"><view class="custom-bar__wrapper" style="margin-top:{{searchMarginTop}}px; height: {{searchHeight}}px;width: {{searchWidth}}px" ><view class="search-group"><image src="../../static/images/search.png" mode="aspectFit" /><input class="search-group__input" auto-focus placeholder="输入点什么吧"/></view></view></view>

wxss代码

view {box-sizing: border-box;overflow: hidden;}.custom-bar {/* background-color: #aaa; */position: fixed;left: 0;top: 0;width: 100%;}.custom-bar__wrapper {padding: 0 10rpx;display: flex;justify-content: center;align-items: center;}.search-group {width: 100%;height: 100%;display: flex;justify-content: flex-start;align-items: center;border: 1px solid #666;border-radius: 60rpx;padding: 0 10rpx;}.search-group > input {font-size: 25rpx;}.search-group > image {height: 32rpx;width: 32rpx;margin-right: 20rpx}

最终实现效果(返回按钮因为懒得找图标就没加上去 - -) ------ 代码地址

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。