本章节为微信小程序weui开放的最后一个组件,searchbar搜索栏的实现,详情如下:
一、引入小程序weui css样式库,请参考第一章
二、在pages目录中新建目录searchbar以及文件searchbar.wxml,并添加如下代码
<view class="page"> <view class="page__hd"> <view class="page__title">SearchBar</view> <view class="page__desc">搜索栏(www.vxzsk.com V型知识库原创)</view> </view> <view class="page__bd"> <view class="weui-search-bar"> <view class="weui-search-bar__form"> <view class="weui-search-bar__box"> <icon class="weui-icon-search_in-box" type="search" size="14"></icon> <input type="text" class="weui-search-bar__input" placeholder="搜索" value="{{inputVal}}" focus="{{inputShowed}}" bindinput="inputTyping" /> <view class="weui-icon-clear" wx:if="{{inputVal.length > 0}}" bindtap="clearInput"> <icon type="clear" size="14"></icon> </view> </view> <label class="weui-search-bar__label" hidden="{{inputShowed}}" bindtap="showInput"> <icon class="weui-icon-search" type="search" size="14"></icon> <view class="weui-search-bar__text">搜索</view> </label> </view> <view class="weui-search-bar__cancel-btn" hidden="{{!inputShowed}}" bindtap="hideInput">取消</view> </view> <view class="weui-cells searchbar-result" wx:if="{{inputVal.length > 0}}"> <navigator url="" class="weui-cell" hover-class="weui-cell_active"> <view class="weui-cell__bd"> <view>实时搜索文本</view> </view> </navigator> <navigator url="" class="weui-cell" hover-class="weui-cell_active"> <view class="weui-cell__bd"> <view>实时搜索文本</view> </view> </navigator> <navigator url="" class="weui-cell" hover-class="weui-cell_active"> <view class="weui-cell__bd"> <view>实时搜索文本</view> </view> </navigator> <navigator url="" class="weui-cell" hover-class="weui-cell_active"> <view class="weui-cell__bd"> <view>实时搜索文本</view> </view> </navigator> </view> </view> </view>
三、在根目录app.json配置文件中配置searchbar文件路径,保存后会自动生成对应的js、css等文件
"pages/searchbar/searchbar",
四、searchbar.wxss代码实现
.searchbar-result{ margin-top: 0; font-size: 14px; } .searchbar-result:before{ display: none; } .weui-cell{ padding: 12px 15px 12px 35px; }
五、searchbar.js代码实现
Page({ data: { inputShowed: false, inputVal: "" }, showInput: function () { this.setData({ inputShowed: true }); }, hideInput: function () { this.setData({ inputVal: "", inputShowed: false }); }, clearInput: function () { this.setData({ inputVal: "" }); }, inputTyping: function (e) { this.setData({ inputVal: e.detail.value }); } });
六、效果
如上图红色圈圈部分,点击搜索框,会出现实时搜索文本,类似于百度搜索一样,体验很好。
此文章本站原创,地址 https://www.vxzsk.com/1924.html
转载请注明出处!谢谢!
感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程
上一篇:WebDAV文件分发
下一篇:3.6、rsync同步实现文件目录复制
^