微信小程序开发笔记2——自定义导航栏组件

本文主要是熟悉微信小程序自定义组件的开发,以一个常见的导航栏(Tabbar)需求为例。

官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/

首先我们先看一下最终实现的效果:
tabbar演示

这是一个非常简单的例子,就用这个例子敲开自定义组件的大门。如果你对vue的组件很熟的话,那么你应该会轻松上手。

思路

改组件应该具备最基本的属性和方法:

  • 每个tab页签显示的文字
  • 页签被选中时的颜色
  • 选中某个页签时,父组件应该知道这个被选中的页签信息,从而做一些逻辑操作

这里我还给页签切换时添加了slider的滑动动画,为了更好的用户体验

编码实现

组件 wxml
<view class='tabbar-container'>
  <view class='tabbar'>
  <!-- 页签 -->
    <block wx:for="{{tabItems}}" wx:key="ti+{{index}}">
      <view id="{{index}}"
            class='tabbar-item'
            style='{{ activedIndex == index ? ("color: " + activedColor) : "" }}'      
            bindtap="clickTab">
        <view class="tabbar-title">{{item}}</view>
      </view>
    </block>
    <!-- 
    	slider 滑块,会随着选中的页签而滑动至对应的位置,
    	使用了CSS3的translateX属性 
    	-->
    <view class='selected-slider'
          style="transform: translateX({{sliderOffset}}px); -webkit-transform: translateX({{sliderOffset}}px);background-color: {{activedColor}}">
    </view>
  </view>
</view>
组件js
Component({
  properties: {
    tabItems: Array,
    // 选中标签页的颜色(文字颜色+小滑块颜色)
    activedColor: {
      type: String,
      value: '#d5001c'
    }
  },
  data: {
    activedIndex: 0,
    // slider的左偏移量,用这个来控制其移动的距离
    sliderOffset: 0
  },
  methods: {
    // 切换tab时调用的方法
    clickTab(e) {
      this.setData({
        activedIndex: e.currentTarget.id,
        sliderOffset: e.currentTarget.offsetLeft
      })
      // 触发父组件的tab-change方法,并将当前选中的tab作为参数传递给父组件
      this.triggerEvent('tab-change', { activedTab: e.currentTarget.id })
    }
  }
})
组件的 wxss
.tabbar {
  display: flex;
  align-items: center;
  position: relative;
  font-size: 28rpx;
  box-shadow: 0 6rpx 6rpx 0 #f0f0f0;
}

.tabbar-item {
  flex: 1;
  text-align: center;
  padding: 30rpx 0;
  color: #b5b4ba;
}

.tabbar-title{
  display: inline-block;
}
.selected-slider {
  position: absolute;
  content: " ";
  left: 0;
  bottom: 0;
  width: 33.33%;
  height: 6rpx;
  /* 选中页签时滑动的移动动画 */  
  -webkit-transition: -webkit-transform 0.3s;
  transition: transform 0.3s;
}
组件json
{
  "component": true,
  "usingComponents": {}
}

使用该组件

注册组件

不要忘了在使用该组件的页面或父组件的json文件中注册该组件:

"usingComponents": {
    "tabbar": "/components/tabbar/tabbar"
  }
页面/父组件 的wxml
<view class='tab'>
  <tabbar tab-items="{{tabOptions}}" actived-color="green" 
  		 bind:tab-change="onTabChange">
  </tabbar>
</view>
<view class='tab-content tab-{{currentTabIndex}}'>
  {{'当前Tab: ' + currentTabIndex}}
</view>
页面/父组件 的js
Page({
  data: {
    tabOptions: ['全部', '我的好友', '特别关注'],
    currentTabIndex: 0
  },
  onTabChange(e){
    // 接受来自组件传递的参数
    const detail = e.detail
    this.setData({
      currentTabIndex: detail.activedTab
    })
  }
})
页面/父组件 的wxss
.tab-content{
  margin-top: 10rpx;
  height: 200px;
  width: 100%;
  text-align: center;
  padding: 100px 0;
}
.tab-0{
  background-color: lightblue;
}
.tab-1{
  background-color: lightgreen;
}
.tab-2{
  background-color: lightpink;
}

github

demo源码:https://github.com/JerryYuanJ/mini-app-pratice
如果对你有帮助,欢迎star。或者你发现bug也欢迎issue。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值