微信小程序实现tab组件

1、首先在wxml文件中:

<view class="main">
  <view>
    <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>
  <swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight}}px" bindchange="bindChange">
    <swiper-item>
      你好
    </swiper-item>
    <swiper-item>
        <view>欢迎评价</view>
    </swiper-item>
</swiper>
</view>

2、js文件中:

/**
   * 页面的初始数据
   */
  data: {
  
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
  },
  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 });

},

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    console.log("options")
    var that = this;
    wx.getSystemInfo({

      success: function(res) {
          that.setData({
              winWidth: res.windowWidth,
              winHeight: res.windowHeight+50
          });
      }

  });
    var tokens = wx.getStorage({
      key: 'token',
    });
    that.setData({
      id: options.id,
      token: tokens
    })
    
  },

 3、wxss样式:

/* tab样式开始 */

.swiper-tab{
  height: 10vh;
  display: flex;
  line-height: 7vh;
  margin-bottom: 10px;
}
.swiper-tab-list{  
  font-size: 16px;
  display: inline-block;
  width: 50%;
  color: #777777;
  text-align: center;
  margin-bottom: 10px;
}
.on{ 
  color: blue;
  border-bottom: 3rpx solid blue;
  padding-bottom:1vh;
}

.swiper-box{ 
  display: block; 
  height: 80vh; 
  width: 100%; 
  overflow: scroll; 
}
.swiper-box view{
  text-align: center;
}
/* tab样式结束 */

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值