小程序tab

自定义组件:
自定义组件脚本:
WXML:

<view class='tab-control'>
<!-- 遍历最好使用block -->
<block wx:for="{{titles}}" wx:key="{{inde}}x"> 
<view class='tab-item {{currentIndex == index ? "active":""}}' bind:tap="handleItemClick" data-index="{{index}}"><text>{{item}}</text></view>
</block>
</view> 

WXSS:

.tab-control{
display: flex;
height: 88rpx;
line-height: 88rpx;
background: orange;
}
.tab-item{
flex: 1;
text-align: center;
}
.active{
color:red;
}
.active text{
border-bottom: 6rpx solid red;
padding: 20rpx 10rpx;
}

JS:

Component({
/**
* 组件的属性列表
*/
properties: {
titles:{
type:Array,
value:{}
}
},

/**
* 组件的初始数据
*/
data: {
currentIndex:0
},

/**
* 组件的方法列表
*/
methods: {
// 监听的所有方法都要写在此处
handleItemClick(event) {
// 1、获取传入的index
const index = event.currentTarget.dataset.index;

// 2、改变记录的currentIndex,将其对应的位置通过三目运算符加上active
this.setData({
currentIndex:index
})

// 3、发出自定义组件,通知页面内部的点击事件
this.triggerEvent('itemclick',{index,title:this.properties.titles[index]},{})
}
}
})

传入其他页面:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值