【详细】豆瓣小程序–从0-1实现自己的导航栏控件

时间:2020-6-28 作者:admin

    

导航栏是app,小程序中非常常见的一个需求,作为常见的需求,如何高效地完成该需求的实现,就异常关键。那么我们如何从0到1实现自己的导航栏控件呢,请看本文。

 导航栏实际效果

1. 没有点击时候,导航栏显示放大镜图标

2. 点击导航栏,显示搜索两个字

3. 可以输入关键词,进行搜索导航

由于在一个小程序里面,导航栏功能出现很多,因此我计划将其做成一个控件,以便多次复用,减少代码重复冗余。这就涉及到了components组件的知识点

那么这个功能的整体架构和应用可以分为两部分,控件的布局,应用调用页面的布局

searchbar空间布局

      首先是wxml,这里我定义了search-navigator这个控件对象,isnaviagator这个判断是否导航的参数,url则是跳转到对应的导航页面。

<view class="searchbar">
  <navigator wx:if="{{isnavigator}}" url='/pages/search/search' class='search-navigator'></navigator>
</view>

     其次是js文件,定义isnavigatior参数

// components/searchbar/searchbar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isnavigator: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  }
})

 接着是,wxss布局样式,涉及导航栏的外观。

background-repeat 属性设置是否及如何重复背景图像。默认地,背景图像在水平和垂直方向上重复。

search-input-group是值得,导航条里面输入文字的那部分布局,见红圈部分。

background-image:url() 设置的是放大镜图片文件位置,这里也可以才用上传的webp去实现

.searchbar{
  background-color: #41be57;
  padding: 20rpx;
}

.search-navigator{
  width: 100%;
  height: 60rpx;
  background-color: #fff;
  border-radius: 10rpx;
  background-image: url("pages/images/search.png") ;/* 放大镜图片文件 */
  
  /****  采用webp,将图片上传到网上,再去url指定获取图片  ****/
  /*  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAPA0lEQVR4Xu2dj5UUNxLGpQgMERhHwBKBrUnAEIFxBOYiOByBIQKvIzBOYAQR3F4EhyM4iED3Pq/mPDvsTFep1b3qqq/f4wFvJc3qK/
2mVKU/HQMfKkAFzioQqQ0VoALnF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4KzPAkQ0NCQLoMDzYyw5MM/VoKAsKx3VWBRk8CSJ6NuBeNgHQdHmxshicZ8i1gBIRjehEFGj3JcAuJBGSR4cFGqyfB5dp4mY/mGSpoJyAa07GsWoGcs/alq4hH8AJR9c0s6l9OUIGACERikXkKABLlZeLDvESUgMyzPWsLFcBVq/WK1q+EVR6P4EUIiNBaLDZfAVyCUW/
cl7xT8sVDn8lHjwnIfLuzBYUC9fILyR3JQ+zVIiAK47JoPwUEcQkC9Qe/xIKA9LM5W1IqcMGbDJPqJSBKo7J4fwWO7hzDFUq4D+zBPcehlwSkv73ZoiEFCIghY7Ir/RUgIP01ZYuGFCAghozJrvRXgID015QtGlKAgBgyJrvSXwEC0l9TtmhIAQJiyJjsSn8FCEh/TdmiIQUIiCFjsiv9FSAg/TVli4YUICCGjMmu9FeAgPTXlC0aUoCAGDImu9JfAQLSX1O2aEgBAmLImOxKfwUISH9N2aIhBQiIIWOyK/0VICD9NWWLhhQgIIaMya70V4CA9NeULRpSgIAYMia70l8BAtJfU7ZoSAECYsiY7Ep/BQhIf03ZoiEFCIghY7Ir/RUgIP01ZYuGFCAghozJrvRXgID015QtGlKAgBgyJrvSXwEC0l9TtmhIAQJiyJjsSn8FCEh/TdmiIQX+B7BN6BQFysCGAAAAAElFTkSuQmCC"); */


  background-position: center center;
  background-repeat: no-repeat;
  background-size: 8%;
}

.search-input-group{
  width: 100%;
  height: 60rpx;
  background-color: #fff;
  border-radius: 10rpx;
  padding: 10rpx 20rpx;
  box-sizing: border-box;
}

.search-input{
  min-height: 40rpx;
  height: 40rpx;
  font-size: 12px;
}

这样一个导航栏的控件searchbar就实现了,那么该控件在其他页面如何调用的呢

 

调用导航栏控件 

wxml文件,在这里直接引用searchbar控件,传入isnavigtor参数为true

<searchbar isnavigator="{{true}}"></searchbar>

 对于引用页面,需要在json中添加引用的控件依赖

{
  "usingComponents": {
    "searchbar":"/components/searchbar/searchbar"   
  }
}

 那么在这个页面,我们就可以轻松的使用searchbar这个导航栏控件了,非常方便。喜欢给我点个关注吧

 

 

 

 

 

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:87074139@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。