選項卡算是小程序中必不可少,大家基本上都會用到的一個功能組件,今天就來分享一個自己做的效果,縱向的一個選項卡
wxml
<view class='productNav'> <!-- 左側 --> <view class='left'> <view class="{{active==0?'selected':'normal'}}" id="0" bindtap='switchNav'>牙齒清潔</view> <view class="{{active==1?'selected':'normal'}}" id="1" bindtap='switchNav'>牙齒矯正</view> <view class="{{active==2?'selected':'normal'}}" id="2" bindtap='switchNav'>牙齒種植</view> <view class="{{active==3?'selected':'normal'}}" id="3" bindtap='switchNav'>牙齒治療</view> <view class="{{active==4?'selected':'normal'}}" id="4" bindtap='switchNav'>拔牙補牙</view> <view class="{{active==5?'selected':'normal'}}" id="5" bindtap='switchNav'>牙齒美容</view> </view> <!-- 右側 --> <view class='right'> <view class='type'> <!-- current:當前所在滑塊的 index --> <!-- vertical:滑動方向是否為縱向 --> <swiper class="swiper-item" current='{{currentTab}}' vertical='{{true}}'> <!-- catchtouchmove 阻止彈窗后滾動穿透 --> <swiper-item id="0" catchtouchmove="false"> 牙齒清潔 </swiper-item> <swiper-item id="1" catchtouchmove="false"> 牙齒矯正 </swiper-item> <swiper-item id="2" catchtouchmove="false"> 牙齒種植 </swiper-item> <swiper-item id="3" catchtouchmove="false"> 牙齒治療 </swiper-item> <swiper-item id="4" catchtouchmove="false"> 拔牙補牙 </swiper-item> <swiper-item id="5" catchtouchmove="false"> 牙齒美容 </swiper-item> </swiper> </view> </view> </view>
wmss
/* 選項卡 */ .productNav{ display: flex; flex-direction: row; font-family: "Microsoft YaHei" } .left{ width: 25.3%; font-size: 30rpx; background-color: #f4f4f4; } .left view{ text-align: center; height: 95rpx; line-height: 95rpx; } .selected{ background-color: #fff; font-weight: bold; color: #E54847; } .normal{ background-color: #f4f4f4; border-bottom: 1px solid #f2f2f2; } .right{ width:74%; margin: 0; } swiper{ height: 1050rpx; } .swiper-item view{ width: 540rpx; height: 200rpx; padding: 25rpx 0 25rpx 20rpx; border-bottom: 1rpx solid #ccc; }
js
Page({ data: { active: 0, currentTab: 0 }, // 選項卡 主要是通過判斷e.target.id的值 設置對應的id顯示 switchNav: function (e) { var page = this; var id = e.target.id; if (this.data.currentTab == id) { return false; } else { page.setData({ currentTab: id }); } page.setData({ active: id }); } })