小程序頂部navbar(非Slider)

wxml(該導(dǎo)航欄的實(shí)現(xiàn)原理是當(dāng)你要顯示哪個(gè)view在最前端的時(shí)候隱藏其他的view):

<!-- 頂部navbar導(dǎo)航欄 -->
<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>我是第一個(gè)view</text>
</view>
<view hidden="{{currentIndex!==1}}" class='box'>
  <text>我是第一個(gè)view</text>
</view>
<view hidden="{{currentIndex!==2}}" class='box'>
  <text>我是第一個(gè)view</text>
</view>

js(根據(jù)不同的需求,data里面navbar的值和個(gè)數(shù)可以按需修改):

Page({
data: {
    navbar: ["進(jìn)行中", "成功", "失敗"],
    currentIndex: 0,
    status: false,
  },
  // navbar切換
  navbarTab: function (e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    });
  }
})

wxss(因?yàn)閚avbar的高度為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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容