微信小程序實現標簽頁滑塊效果


標題圖

微信小程序實現標簽頁滑塊效果

案例一

小程序完整代碼:

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>
      <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>

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;
}

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
        });
      }
    });
  },
  // 滑動切換tab
  bindChange: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  // 點擊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
      })
    }
  },
  onShow: function() {

  },
  onHide: function() {

  },
  onUnload: function() {

  },
})

json:

{
  "navigationBarTitleText": "消息"
}

案例二:

效果圖:

案例二

代碼:

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

  <swiper-item>
  </swiper-item>
</swiper>

js:

Page({

  /**
   * 頁面的初始數據
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
  },
  // 滑動切換tab
  bindChange: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  // 點擊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
      })
    }
  },
  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函數--監聽頁面初次渲染完成
   */
  onReady: function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函數--監聽頁面顯示
   */
  onShow: function() {

  },

  /**
   * 生命周期函數--監聽頁面隱藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函數--監聽頁面卸載
   */
  onUnload: function() {

  },

  /**
   * 頁面相關事件處理函數--監聽用戶下拉動作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 頁面上拉觸底事件的處理函數
   */
  onReachBottom: function() {

  },

  /**
   * 用戶點擊右上角分享
   */
  onShareAppMessage: function() {

  }
})

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;
}

達叔小生:往后余生,唯獨有你
You and me, we are family !
90后帥氣小伙,良好的開發習慣;獨立思考的能力;主動並且善於溝通
簡書博客: 達叔小生
https://www.jianshu.com/u/c785ece603d1

結語

  • 下面我將繼續對 其他知識 深入講解 ,有興趣可以繼續關注
  • 小禮物走一走 or 點贊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM