爱玺玺

爱玺玺的生活日记本。wx:lb87626

微信小程序tab切换原理

wxml文件:

<!--

绑定监听tindtap switchNav

data-current自定义显示

原理就是改变cuurentTab的值来达到切换效果

-->

<view class="swiper-tab">

<view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">Seside1</view>

<view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">Seside2</view>

<view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">Seside3</view>

</view>

<!--

swiper绑定监听器bindChange

current可以设置当前显示哪个view

-->

<swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">

<swiper-item>

<view>Seside1</view>

</swiper-item>

<swiper-item>

<view>Seside2</view>

</swiper-item>

<swiper-item>

<view>Seside3</view>

</swiper-item>

</swiper>





wxss文件:

.swiper-tab{

width: 100%;

border-bottom: 2rpx solid #777777;

text-align: center;

line-height: 80rpx;

}

.swiper-tab-list{

font-size: 30rpx;

display: inline-block;

width: 20%;

color: #777777;

}

.on{

color: #da7c0c;

border-bottom: 5rpx solid #da7c0c;

}

.swiper-box{

display: block;

height: 200rpx;

width: 100%;

overflow: hidden;

}

.swiper-box view{

text-align: center;

}



js文件:

// 滑动切换tab 

bindChange: function (e) {

this.setData({ currentTab: e.detail.current });

},

// 点击tab切换 

swichNav: function (e) {

console.log(e);

//点的是当前按钮不作切换

if (this.data.currentTab === e.target.dataset.current) {

return false;

} else {//否则设置数据,currentTab是全局变量,根据这个设置显示哪个

this.setData({

currentTab: e.target.dataset.current

})

}

}


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5