微信小程序Tabbar自定义
-
1.新建tabbar.js内容如下:
function tabbarinit() {
return [{
"key": "homePage",
"pagePath": "/pages/index/index",
"iconPath": "/images/home.png",
"selectedIconPath": "/images/home_select.png",
"text": "首页"
},
{
"key": "apply",
"pagePath": "/pages/report/index",
"iconPath": "/images/apply.png",
"selectedIconPath": "/images/apply_select.png",
"text": "返高申报"
},
{
"key": "user",
"pagePath": "/pages/home/index",
"iconPath": "/images/user.png",
"selectedIconPath": "/images/user_select.png",
"text": "我的"
}
]
}
function tabbarmain(bindName = "tabdata", id, target) {
var that = target;
var bindData = {};
var otabbar = tabbarinit();
otabbar[id]['iconPath'] = otabbar[id]['selectedIconPath']
otabbar[id]['current'] = 1;
bindData[bindName] = otabbar
that.setData({
bindData
});
}
module.exports = {
tabbar: tabbarmain
}
-
2.创建tabbar.wxss.icon{width:40rpx;height:40rpx;margin-bottom:8rpx;}.tabBar{width: 100%;padding: 15rpx 0;position: fixed;bottom: 0;overflow-x: hidden;border-top: 1px solid #f5f5f5;background: #fefefe;box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.06);z-index: 999;}.tabBar-item{overflow: hidden;float: left;width:33.3333%;text-align: center;font-size: 30rpx;line-height: 30rpx;color: #888888;padding-bottom:20rpx;}.tabBartext{color: #ff5c13;}.empty_custom{height: 45px;width: 100%;}
-
3.创建tabbar.wxml<template name="tabBar"><view class="tabBar"><block wx:for="{{tabBar}}" wx:for-item="item" wx:key="tabBar"><view class="tabBar-item"><view bindtap="menuBtn" >="{{index}}" hover-class="none" >="{{item.pagePath}}"><view><image class="icon" src='{{item.iconPath}}' mode="aspectFit"></image></view><view class="{{item.current== 1 ? 'tabBartext' :''}}">{{item.text}}</view></view></view></block></view></template>
-
4.index.js中使用
onLoad() {
tabbar.tabbar("tabBar", 0, this);
}
-
5.index.wxml中引用
<import src="../tabbar/tabbar.wxml" />
<template is="tabBar" data="{{tabBar:bindData.tabBar}}" />
-
6.全局app.wxss中引用tabbar.wxss@import 'pages/tabbar/tabbar.wxss';
-
效果图