使用wx:if的方法
(1) Wtml中的代码
<view class="big">
<view wx:for="{{list}}" wx:key="index" data-index="{{index}}" class="nav {{status==index ?'on':''}}" bindtap="change">{{item.title}}</view>
</view>
<view wx:if="{{status==0}}">1</view>
<view wx:if="{{status==1}}">2</view>
<view wx:if="{{status==2}}">3</view>
wxss中代码
/* pages/Tab/tab.wxss */
.big{
width: 100%;
height: 100rpx;
display: flex;
}
.nav{
flex: 1;
width: 100%;
height:100rpx;
text-align: center;
line-height: 100rpx;
}
.on{
color: red;
border-bottom: 4rpx solid red;
}
js中的代码
Page({
data: {
status:0,
list:[
{
id:1,
title:"青铜"
},
{
id:2,
title:"黄金"
},
{
id:3,
title:"钻石"
}
]
},
change:function(e){
let that=this
console.log(e);
var index=e.currentTarget.dataset.index
that.setData({
status:index
})
},
}
2.
1)wxml的内容
<view class="swiper-tab">
<view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">产品</view>
<view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">金融</view>
<view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">理财</view>
</view>
<swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">
<swiper-item>
<view>产品</view>
</swiper-item>
<swiper-item>
<view>金融</view>
</swiper-item>
<swiper-item>
<view>理财</view>
</swiper-item>
</swiper>
用三元表达式判断currentTab==Num是否为真,如果为真,动态添加on类名,执行标记样式,这样就实现了上面list列表的切换,至于内容重定义了data下的currentTab,让其与之对应显示,这样一个简单的tab切换功能就实现了,而且不受位置的限制,更改css样式可随意布局
2)wxss的内容
.swiper-tab{
width: 100%;
text-align: center;
line-height: 80rpx;}
.swiper-tab-list{ font-size: 30rpx;
display: inline-block;
width: 33.33%;
color: #777777;
}
.on{ color: blue;
border-bottom: 3rpx solid blue;}
.swiper-box{ display: block; height: 100%; width: 100%; overflow: hidden; }
.swiper-box view{
text-align: center;
}
3)js的内容
Page({
data: {
winWidth: 0,
winHeight: 0,
currentTab: 0,
},
onLoad: function() {
var that = this;
/**
* 获取当前设备的宽高
*/
wx.getSystemInfo( {
success: function( res ) {
that.setData( {
winWidth: res.windowWidth,
winHeight: res.windowHeight
});
}
});
},
// tab切换逻辑
swichNav: function( e ) {
var that = this;
if( this.data.currentTab === e.target.dataset.current ) {
return false;
} else {
that.setData( {
currentTab: e.target.dataset.current
})
}
},
bindChange: function( e ) {
var that = this;
that.setData( { currentTab: e.detail.current });
},
})