关于微信小程序的tarbar,相信大家都不会陌生 在实现小程序微信原装的tabbar却比较呆板,不够精致,每每不符合本身的要求css
先说一下这套方案的优势:json
实现这套方案核心仍是自定义组件
小程序
那就从这开始聊:
一个自定义组件由 json wxml wxss js 4个文件组成。
微信小程序
{
//声明引用一个组件 配置好你的组件引用路径
"usingComponents": {
"icon": "../../components/icon/index"
}
}
复制代码
//这样就可以在你的页面中添加组件
<icon type="" color="" size=""/>
复制代码
若是对于组件的定义仍有疑惑的能够参考这份文档: 官方关于自定义组件的文档bash
{
//声明这一组文件设为自定义组件
"component": true,
"usingComponents": {}
}
复制代码
<!-- 注意style里面的分号! -->
<text class="iconfont icon-{{type}}" style="color:{{color}}; font-size:{{size}}rpx" ></text>
复制代码
//这里定义了3个自定义属性他们经过{{}}与wxml中的数据链接起来
Component({
properties: {
type: {
type: String,
value: ''
},
color: {
type: String,
value: '#000000'
},
size: {
type: Number,
value: '45'
}
}
})
复制代码
到此,字体图标组件搞定。微信
{
"component": true,
//声明对字体图标组件的引用
"usingComponents": {
"icon": "../../components/icon/index"
}
}
复制代码
<view class="weibo-tabbar" >
//绑定回首页事件,此处的data-hi中的数据是为了传递到e.currentTarget.dataset.hi
//经过这个数据咱们能够用来判断是否处于首页,而后在js中配置能够阻扰原地跳转
<view class="item-left" bindtap="goHome" data-hi="{{isIndex}}">
<icon type="shouye" color="{{isIndex?'#000000':'#b1b1b1'}}" size="45"/>
<text class="1" style="color:{{isIndex?'#000000':'#b1b1b1'}}">首页</text>
</view>
<block wx:if="{{isInner}}">
<view class="item-right" style="color:#b1b1b1" bindtap="goShare">
<icon type="fenxiang" color="gray" size="45"/>
<text class="2">分享</text>
</view>
</block>
<block wx:else>
<view class="item-right" bindtap="goInfo" data-hi="{{isIndex}}">
<icon type="wode" color="{{isIndex?'#b1b1b1':'#000000'}}" size="45"/>
<text class="2" style="color:{{isIndex?'#b1b1b1':'#000000'}}">个人</text>
</view>
</block>
</view>
复制代码
const app = getApp();
Component({
properties: {
isIndex: { // 是否主页
type: Boolean,
value: false,
},
isInner: { //是否内部页面
type: Boolean,
value: false,
},
},
data: {
// 这里是一些组件内部数据
someData: {}
},
methods: {
// 这里是一个自定义方法
goHome: (e) => {
// 判断是否为主页面防止原地跳转
if(!e.currentTarget.dataset.hi){
wx.redirectTo({
url: "/pages/index/index"
})
}
},
goShare: function () {
},
goInfo: (e) => {
if(e.currentTarget.dataset.hi){
wx.redirectTo({
url: "/pages/info/info"
})
}
}
}
})
复制代码
.weibo-tabbar {
bottom: 0;
height: 97rpx;
padding: 12rpx 0rpx;
display: flex;
width: 100%;
position: fixed;
background: #ffffff;
box-sizing: border-box;
}
//产生优雅的0.5px边框
.weibo-tabbar::after {
content: "";
position: absolute;
width: 200%;
height: 200%;
top: 0;
left: 0;
border-top: 1rpx solid rgba(177, 177, 177, 0.4);
transform: scale(0.5);
transform-origin: 0 0;
}
//这里用flex布局,移动端flex布局确实很爽
.weibo-tabbar .item-left, .item-right{
//这里有一处坑,若不不设置他的层级变大的话
//你是点不到这个item按钮的,固然也不会产生触碰事件
z-index: 999;
width: 50%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-size: 20rpx;
color: #b1b1b1;
}
.shouye, .wode {
height: 45rpx;
width: 45rpx;
}
复制代码
//此处isIndex是给组件的属性传输值,别属性不添加即为默认属性值
<tabbar isIndex="true"></tabbar>
复制代码
结果:app
哇,看了半天就出这么一个小东西!
其实大道至简
掌握这套方案可以适配你须要的全部tabbar
他的颜色、大小、位置均可以本身掌控,重要的是这个解决方案。xss
以为不错的话能够点个赞鼓励一下哟布局