示例1:

index.wxml
<view>
<view class="tab-container">
<view
class="default {{type=='expend'? 'expend-active':''}}"
bindtap="handleType"
data-type="expend"
>支出</view
>
<view
class="default {{type=='earning'? 'earning-active': ''}}"
bindtap="handleType"
data-type="earning"
>收入</view
>
<view
class="default {{type=='transaccount'?'transaccount-active': ''}}"
bindtap="handleType"
data-type="transaccount"
>转账</view
>
</view>
</view>
index.js
Page({
/**
* 页面的初始数据
*/
data: {
type: 'expend',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {},
handleType(event) {
const type = event.currentTarget.dataset.type;
this.setData({
type: type,
});
},
});
index.wxss
/* pages/tablistchange/tablistchange.wxss */
.tab-container {
display: flex;
justify-content: center;
align-items: center;
padding: 10rpx 0;
}
.tab-container .default {
margin-right: 25rpx;
padding: 5rpx 15rpx;
}
.expend-active {
color: #0ca168;
border-bottom: 2px solid #0ca168;
}
.earning-active {
color: #ff6b6a;
border-bottom: 2px solid #ff6b6a;
}
.transaccount-active {
color: #abcdef;
border-bottom: 2px solid #abcdef;
}
示例2:

index.wxml
<view class="encourage-content">
<view class="encorage-title">{{accountlist.encourtitle}}</view>
<view class="encourage-list">
<block wx:for="{{listData}}" wx:key="*this">
<view
data-item="{{item}}"
class="list-item {{item.account == accountlist.account ? 'list-active': ''}}"
bindtap="handleList"
>¥<text>{{item.account}}</text></view
>
</block>
</view>
</view>
index.js
// pages/tablistchange/tablistchange.js
Page({
/**
* 页面的初始数据
*/
data: {
accountlist: {
// 通过初始化一个目标对象
account: 2,
encourtitle: '两只黄鹂鸣翠柳',
},
listData: [
// 循环遍历的list数据
{
account: 2,
encourtitle: '两只黄鹂鸣翠柳',
},
{
account: 3,
encourtitle: '三人行,必有我师',
},
{
account: 5,
encourtitle: '吾生有涯,吾知无涯',
},
{
account: 13,
encourtitle: '一行白鹭上青天',
},
{
account: 14,
encourtitle: '白驹过隙,岁月如梭,愿君只争朝夕',
},
{
account: 52,
encourtitle: '何以解忧,唯有暴富',
},
],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {},
// 列表切换
handleList(event) {
// console.log(event);
this.setData({
// 给accountList对象重新赋值
accountlist: event.currentTarget.dataset.item,
});
},
});
index.wxss
.encourage-content {
text-align: center;
padding: 30rpx 80rpx 0 80rpx;
}
.encourage-content .encorage-title {
padding-bottom: 30rpx;
color: #ff6b6a;
}
.encourage-content .encourage-list {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.encourage-content .encourage-list .list-item {
width: 31%;
height: 80rpx;
border: 1px solid #dddd;
margin-bottom: 15rpx;
line-height: 80rpx;
}
.encourage-content .encourage-list .list-active {
color: #ff6b6a;
border: 1px solid #ff6b6a;
}
来源:https://zhuanlan.zhihu.com/p/264763638