微信小顺序之选项卡

选项卡随处可见,微信小顺序中也不破例,下面来写一个简朴的小顺序选项卡

思绪

  • 之前写过基于swiper的选项卡,在小顺序中有swiper组件,毫无疑问这里要用到swiper组件
  • 小顺序中的swiper组件有个题目就是不能依据内容自适应高度,所以要经由过程wx.getSystemInfoSync猎取装备高度设置swiper高度
  • 小顺序中的swiper组件中swiper-item内容超越可视区后没法转动显现,所以这里要用到另一个组件scroll-view

小顺序中的swiper组件功用照样比较有限的,有待优化。

计划

1.首先在js中设置数据

 data: {
    tabs: ['菜单一', '菜单二'],// 导航菜单栏
    curIdx:0,// 当前导航索引
    scrollHeight:0, //转动高度 = 装备可视区高度 -  导航栏高度
    list:[],// 内容区列表
  },

在onLoad函数中添补数据

  /**
   * 性命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let list=[];
    for (let i=1;i<=30;i++){
      list.push(i)
    }
    this.setData({
      list: list
    });
  },

2.在WXML中轮回衬着出导航

<!-- 导航栏最先 -->
<view class="swiper-tab">
  <view wx:for="{{tabs}}" wx:key class="swiper-tab-item {{curIdx==index?'swiper-active':''}}" data-current="{{index}}" catchtap="clickTab">
    <text>{{item}}</text>
  </view>
</view>

3.设置当前运动导航款式

/*初始化款式*/
view, text, picker, input, button, image{
  display: flex;
  box-sizing: border-box;
}
/* 导航款式*/
.swiper-tab {
  position: relative;
  width: 100%;
  height: 100rpx;
  justify-content: center;
  align-items: center;
}

.swiper-tab-item {
  background-color: #f3f3f3;
  width: 50%;
  height: 80rpx;
  justify-content: center;
  align-items: center;
}
.swiper-active{
  background-color: rgb(129, 190, 247);
  color: #fff;
}

4.内容显现区

内容显现区运用swiper组件,swiper-item个数要与tabs数组长度 一致

<!-- 内容最先 -->
<swiper class="swiper_content" current="{{curIdx}}"   bindchange="swiperTab" style='height:{{scrollHeight}}px'>
  <swiper-item>
    <scroll-view class="scroll-y" scroll-y style='height:{{scrollHeight}}px' bindscrolltolower="onReachBottom">
    <view wx:for="{{list}}" wx:key>
      <text> 内容一{{item}}</text>
    </view>
        </scroll-view>
  </swiper-item>
  <swiper-item>
    内容二
  </swiper-item>
</swiper>

小顺序中的swiper组件有个题目就是不能依据内容自适应高度,所以要经由过程[wx.getSystemInfoSync][4]猎取装备高度设置swiper高度
小顺序中的swiper组件中swiper-item内容超越可视区后没法转动显现,所以这里要用到另一个组件[scroll-view][5]。
我们在onShow函数中经由过程getSystemInfoSync猎取装备的宽高来设置swiper组件高度以及scroll-view高度

  onShow: function () {
    // 100为导航栏swiper-tab 的高度
   this.setData({
     scrollHeight: wx.getSystemInfoSync().windowHeight - (wx.getSystemInfoSync().windowWidth / 750 * 100),
   })
  },

5.点击导航栏切换内容

  //点击切换
  clickTab: function (e) {
    this.setData({
      curIdx: e.currentTarget.dataset.current
    })
  }, 

6.滑动内容切换导航栏

  //滑动切换
  swiperTab: function (e) {
    this.setData({
      curIdx: e.detail.current
    });
  },

7.可转动地区转动最底革新数据


  /**
 * 页面上拉触底事宜的处置惩罚函数
 */
  onReachBottom: function () {
    // 更新列表
    let list = this.data.list;
    console.log(list)
    let lens = list.length
    for (let i = lens; i < lens+30; i++) {
      list.push(i)
    }
    this.setData({
      list: list
    });
  
  },

一个美丽的选项卡就完成了

完全案例

    原文作者:月影
    原文地址: https://segmentfault.com/a/1190000016300102
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞