使用component实现自定义tabbar
效果图:
实现步骤:
第一步:在需要使用的page页面定义tabbar的数据,不直接定义在component组件中是为了实现解耦,方便复用
Page({
data: {
tabbar: {
"color": "#999999",
"selectedColor": "#7788dd",
"borderStyle": "#dcdcdc",
"backgroundColor": "#ffffff",
"list": [{
"key": "home",
"iconPath": "/images/icon_home.png",
"selectedIconPath": "/images/icon_home_active.png",
"text": "首页"
},
{
"key": "gift",
"iconPath": "/images/icon_gift.png",
"selectedIconPath": "/images/icon_gift_active.png",
"text": "礼品"
},
{
"key": "new",
"iconPath": "/images/icon_plus_big.png",
"iconType": "big overflow circle shadow",
"choose": "disable"
},
{
"key": "message",
"iconPath": "/images/icon_message.png",
"selectedIconPath": "/images/icon_message_active.png",
"text": "信息"
},
{
"key": "contract",
"iconPath": "/images/icon_contract.png",
"selectedIconPath": "/images/icon_contract_active.png",
"text": "联系人"
}
]
}
},
onLoad: function() {
},
tabChange: function(e) {
console.log('你点击了一下')
}
})
第二步:创建component组件custom-tabbar
第三步:编辑custom-tabbar.js的Component()构造器
Component({
/**
* 组件的属性列表
*/
properties: {
// 自定义的组件属性,这里是tabbar的list
tabbar: {
type: Object,
value: {}
}
},
/**
* 组件的初始数据
*/
data: {
//选中的tabbar
selected: 0
},
/**
* 组件的方法列表
*/
methods: {
change: function(e) {
// 更新属性和数据的方法与更新页面数据的方法类似
//获取当前选中的下标
var index = e.currentTarget.dataset.selectedindex
console.log(index)
this.setData({
selected: index
})
},
}
})
这里properties{}定义的属性详情:developers.weixin.qq.com/miniprogram…
e.currentTarget.dataset.selectedindex 属于view事件内容:
developers.weixin.qq.com/miniprogram…
这里针对于dataset对象进行说明,如何在布局中使用该属性传递参数,用于逻辑处理
第四步:实现.wxml结构
<cover-view class="c_tabbar">
<view class="tabbar_item" wx:for="{{tabbar.list}}" wx:for-item="item" wx:for-index="index" data-selectedIndex="{{index}}" wx:key="key" bind:tap="change">
<image class="tabbar_item_img {{item.iconType?item.iconType:''}}" src="{{selected==index && !item.iconType ?item.selectedIconPath:item.iconPath}}">
</image>
<text wx:if="{{item.text&&!item.iconType}}" class="tabbar_item_title {{selected==index?'selected':''}}">{{item.text}}</text>
</view>
</cover-view>
developers.weixin.qq.com/miniprogram…
这里针对于dataset对象进行说明,如何在布局中使用该属性传递参数,用于逻辑处理
关于列表循环:
developers.weixin.qq.com/ebook?actio…
第五步:实现.wxss样式
.c_tabbar {
bottom: 0;
position: fixed;
height: 95rpx;
border-top: 1px solid #dcdcdc;
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
color: #999;
background-color: #fff;
padding: 0;
}
.c_tabbar .tabbar_item .tabbar_item_title.selected {
color: red;
}
.c_tabbar .tabbar_item {
font-size: 20rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.c_tabbar .tabbar_item .tabbar_item_img {
width: 54rpx;
height: 54rpx;
}
/* big icon */
.c_tabbar .tabbar_item_img.big {
width: 95rpx;
height: 95rpx;
}
/* overflow icon */
.c_tabbar .tabbar_item_img.overflow {
position: fixed;
bottom: 25rpx;
}
/* shadow icon */
.c_tabbar .tabbar_item_img.shadow {
-moz-box-shadow: 0px 0px 20rpx rgba(0, 0, 0, 0.5);
-webkit-box-shadow: 0px 0px 20rpx rgba(0, 0, 0, 0.5);
box-shadow: 0px 0px 20rpx rgba(0, 0, 0, 0.5);
}
/* circle icon */
.c_tabbar .tabbar_item_img.circle {
border-radius: 50%;
}
page {
-webkit-user-select: none;
user-select: none;
width: 100%;
overflow-x: hidden;
margin: 0 0 95rpx 0;
padding: 0;
}
在实现第四步和第五部时,尤其是编写标签class选择器使用三元表达条件判断时,多借助控制台的Wxml工具来检查错误
使用:
page页面配置json引入组件
{
"usingComponents": {
"custom_tabbar": "/components/custom-tabbar/custom-tabbar"
}
}
.wxml页面传入属性值
<custom_tabbar tabbar="{{tabbar}}" bind:tap="tabChange"></custom_tabbar>
结语:
这做第一次作分享,有不足希望大家指出。
项目地址:
github.com/Smilekoko/W…
由于个人关于小程序的demo都在这个仓库中,所以请根据文中的步骤自行进行测试。