小程序顶部navbar(非Slider)

本文介绍了一种使用WXML实现导航栏切换的方法,通过设置view的hidden属性来控制不同内容的显示与隐藏,配合WXSS实现了美观的样式效果。

wxml(该导航栏的实现原理是当你要显示哪个view在最前端的时候隐藏其他的view):

<!-- 顶部navbar导航栏 -->
<view class="navbar">
<text wx:for="{{navbar}}" data-index="{{index}}"
class="item{{currentIndex==index?'active':''}}"
bindtap="navbarTab" wx:key="unique">{{item}}</text>
</view>
<view hidden="{{currentIndex!==0}}" class='box'>
<text>我是第一个view</text>
</view>
<view hidden="{{currentIndex!==1}}" class='box'>
<text>我是第一个view</text>
</view>
<view hidden="{{currentIndex!==2}}" class='box'>
<text>我是第一个view</text>
</view>

js(根据不同的需求,data里面navbar的值和个数可以按需修改):

Page({
data: {
navbar: ["进行中", "成功", "失败"],
currentIndex: 0,
status: false,
},
// navbar切换
navbarTab: function (e) {
this.setData({
currentIndex: e.currentTarget.dataset.index
});
}
})

wxss(因为navbar的高度为70rpx,故box的margin-top需为70rpx):

.navbar {
display: flex;
width: 100%;
flex-direction: row;
line-height: 70rpx;
overflow: auto;
position: fixed;
top: 0;
}
.navbar .item {
flex: auto;
font-size: 30rpx;
text-align: center;
background: #fff;
color: #888;
}
.navbar .item.active {
color: #09bb07;
position: relative;
}
.navbar .item.active::after {
content: "";
display: block;
position: absolute;
height: 4rpx;
bottom: 0;
left: 0;
right: 0;
background: #09bb07;
}
.navbar:after {
content: " ";
position: absolute;
left: 0;
bottom: 0;
right: 0;
height: 1px;
border-bottom: 1rpx solid #e5e5e5;
color: #e5e5e5;
}
.box {
margin-top: 70rpx;
background-color: #f8f8f8;
padding: 0;
justify-content: space-between;
box-sizing: border-box;
flex-direction: column;
display: flex;
}

转载于:https://www.cnblogs.com/linwene/p/9894870.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值