小程序頂部navbar(非Slider)


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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM