微信小程序 选项卡 tab 可左右滑动,可点击选择
小程序模板
<view class="box">
<view class="topTabSwiper">
<view wx:for="{{tabs}}" wx:key="tab_id" class="tab {{currentData == index ?'tabBorer':''}} " data-current="{{index}}" wx:item="item" bindtap="checkCurrent">{{item.tab_name}}</view>
</view>
<swiper current="{{currentData}}" class='swiper' duration="300" bindchange="bindchange">
<swiper-item wx:for="{{list}}" wx:key="customer_mobile" wx:item="item">
{{item.name}}
</swiper-item>
</swiper>
</view>
小程序css
.box{ width: 750rpx; display: block;}
.tab {
width: 50%;
display: block;
height: 60rpx; line-height: 60rpx;
text-align: center;
}
.topTabSwiper {width: 750rpx; display: flex; justify-content: space-between;
}
.topTabSwiper:after {
content: "";
clear: both;
display: block;
}
.tabBorer {
border-bottom: 1px solid #f00;
color: #f00;
}
.swiper {
width: 100%;
height: calc(100vh - 70rpx) ;
}
.swiper_con {
text-align: center;
width: 100%;
height: 100%;
padding: 80rpx 0;
}
小程序javascript
Page({
data: {
currentData : 0,
tabs:[
{tab_id:'1',tab_name:'导航1'},
{tab_id:'2',tab_name:'导航2'},
{tab_id:'3',tab_name:'导航3'},
],
list:[
{name:'我导航1对应的tab页'},
{name:'我导航2对应的tab页'},
{name:'我导航3对应的tab页'},
]
},
onLoad(options) {
},
bindchange:function(e){
const that = this;
that.setData({
currentData: e.detail.current
})
},
checkCurrent:function(e){
const that = this;
if (that.data.currentData === e.target.dataset.current){
return false;
}else{
that.setData({
currentData: e.target.dataset.current
})
}
},
onReady() {
},
onShow() {
},
onHide() {
},
onUnload() {
},
onPullDownRefresh() {
},
onReachBottom() {
},
onShareAppMessage() {
}
})
![在这里插入图片描述](https://img-blog.csdnimg.cn/6fe137d42502432294cf5c73f322cb1a.jpeg#pic_center)