Wechat small program to achieve the slider effect of the TAB page

Small program complete code:

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"> Activity </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>
      <image src='{{image}}' class='img'></image>
    </view>
  </swiper-item>

  <swiper-item>
    <view>
      <image src='{{image}}' class='img'></image>
    </view>
  </swiper-item>

  <swiper-item>
    <view>
      <image src='{{image}}' class='img'></image>
    </view>
  </swiper-item>
  
</swiper>
Copy the code

wxss:

Page {
  background-color: #f1f1f1;
}

.swiper-tab {
  background-color: #fff;
  width: 100%;
  text-align: center;
  line-height: 80rpx;
}

.swiper-tab-list {
  font-size: 30rpx;
  display: inline-block;
  width: 33.33%;
  color: # 797979;
}

.on {
  color: #ca0c16;
  border-bottom: 5rpx solid #ca0c16;
}

.swiper-box {
  display: block;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.img {
  width: 100%;
  height: 540rpx;
}
Copy the code

js:

Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    image: ".. /.. /images/404.png",
  },
  onLoad: function(options) {

  },
  onReady: function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) { that.setData({ winWidth: res.windowWidth, winHeight: res.windowHeight }); }}); }, // slide to switch TABbindChange: function(e) { var that = this; that.setData({ currentTab: e.detail.current }); }, // hit TAB to switch to swichNav:function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  onShow: function() {

  },
  onHide: function() {

  },
  onUnload: function() {},})Copy the code

json:

{
  "navigationBarTitleText": "News"
}
Copy the code

Case 2:

Effect:

Code:

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"> Approved </view> </view> <swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">
  <swiper-item>
  </swiper-item>

  <swiper-item>
  </swiper-item>
</swiper>
Copy the code

js:

*/ data: {winWidth: 0, winHeight: 0, currentTab: 0,}, // Swipe to switch TABbindChange: function(e) { var that = this; that.setData({ currentTab: e.detail.current }); }, // hit TAB to switch to swichNav:function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else{that.setData({currentTab: e.target.dataset. Current})}}, /** * life cycle function -- listen to page load */ onLoad:function(options) {}, /** * lifecycle functions -- listen to the page's first rendering complete */ onReady:function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) { that.setData({ winWidth: res.windowWidth, winHeight: res.windowHeight }); }}); }, /** * lifecycle function -- listen to page display */ onShow:function() {}, /** * lifecycle function -- listen page hide */ onHide:function() {}, /** * lifecycle functions -- listen for page unloads */ onUnload:function() {}, /** * page related event handler -- listen to user pull */ onPullDownRefresh:function() {}, /** ** onReachBottom:function() {}, /** * Users click on the upper right corner to share */ onShareAppMessage:function() {}})Copy the code

wxss:

Page {
  background-color: #f1f1f1;
}

.swiper-tab {
  background-color: #fff;
  width: 100%;
  text-align: center;
  line-height: 80rpx;
}

.swiper-tab-list {
  font-size: 30rpx;
  display: inline-block;
  width: 50%;
  color: # 797979;
}

.on {
  color: #5fdafe;
  border-bottom: 5rpx solid #30d1ff;
}

.swiper-box {
  display: block;
  height: 100%;
  width: 100%;
  overflow: hidden;
}
Copy the code

DSS: For the rest of my life, only You and me, we are family! Post-90s handsome guy, good development habits; Ability to think independently; Active and good at communication Blog: Jane books of tertiary niche www.jianshu.com/u/c785ece60…

conclusion

  • Now I will continue to explain other knowledge in depth, if you are interested, you can continue to pay attention to
  • A small gift to walk or praise