一、
先写页面,在wxml使用swiper轮播图可实现选项卡功能(这部分我也是参考了网上的小伙伴的分享)。
<view class='topTabSwiper'>
<block wx:for="{{navlist}}" wx:key="index">
<view class='tab {{currentData == index ? "tabBorer" : ""}}' data-current = "{{index}}" bindtap='checkCurrent'>{{item.name}}</view>
</block>
</view>
<swiper current="{{currentData}}" class='swiper' style="height:600px;" duration="300" bindchange="bindchange">
<block wx:for="{{content}}" wx:key="index">
<swiper-item><view class='swiper_con'>{{item.name}}</view></swiper-item>
</block>>
</swiper>
二、样式
.tab {
float: left;
width: 33.3333%;
text-align: center;
padding: 10rpx 0;
}
.topTabSwiper {
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
zoom: 1;
}
.topTabSwiper:after {
content: "";
clear: both;
display: block;
}
.tabBorer {
border-bottom: 1px solid #f00;
color: #f00;
}
.swiper {
width: 100%;
}
.swiper_con {
text-align: center;
width: 100%;
height: 100%;
padding: 80rpx 0;
}
三、js
Page({
/**
* 页面的初始数据
*/
data: {
currentData: 0,
navlist:[
{
name:'推荐'
},
{
name:'热点 '
},
{
name:'关注'
}
],
content:[
{
name: '推荐1'
},
{
name: '热点 2'
},
{
name: '关注3'
}
]
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
//获取当前滑块的index
bindchange: function (e) {
const that = this;
that.setData({
currentData: e.detail.current
})
},
//点击切换,滑块index赋值
checkCurrent: function (e) {
const that = this;
if (that.data.currentData === e.target.dataset.current) {
return false;
} else {
that.setData({
currentData: e.target.dataset.current
})
}
}
})
结论:这样就可以实现选项卡的功能,而且可以动态获取后台的数据进行合理渲染页面,一般navlist和content这两个都是从后台请求获取到的。
版权声明:本文为qq_43062569原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。