在小程序中实现Tab切换

使用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 });

    },
})
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值