微信小程序使用swiper和scroll-view做Tab标签页

说白了,就是利用轮播图的特性做Tab标签页

Page({
  data: {
    //顶部安全距离(状态栏高度)
    statusBarHeight: wx.getSystemInfoSync().statusBarHeight,
    navScrollLeft: 0,
    currentTab: 0,
    navData: [
      {
        text: 'A'
      },
      {
        text: 'B'
      },
      {
        text: 'C'
      },
      {
        text: 'D'
      },
    ],
  },
  onLoad: function () {},
  switchNav(event) {
    var cur = event.currentTarget.dataset.current;
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
  },
})
<!-- miniprogram\pages\index\index.wxml -->
  <view class="navtab" style="margin-top:100px;">
    <view class="tablist">
        <block wx:for="{{navData}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx">
        <view class="nav-item {{currentTab == idx ?'active':''}}" data-current="{{idx}}" bindtap="switchNav">
            {{navItem.text}}
        </view>
        </block>
    </view>
    <!-- 全部 -->
    <swiper class="tab-box" current="{{currentTab}}" duration="300" bindchange="switchTab">
      <swiper-item class="tab-content">
        <scroll-view style="height: 100%;" scroll-y="true" bindscroll="scroll" bindscrolltolower="bottom">
          A
        </scroll-view>
      </swiper-item>
      <swiper-item class="tab-content">
        <scroll-view style="height: 100%;" scroll-y="true" bindscroll="scroll" bindscrolltolower="bottom">
          B
        </scroll-view>
      </swiper-item>
      <swiper-item class="tab-content">
        <scroll-view style="height: 100%;" scroll-y="true" bindscroll="scroll" bindscrolltolower="bottom">
          C
        </scroll-view>
      </swiper-item>
      <swiper-item class="tab-content">
        <scroll-view style="height: 100%;" scroll-y="true" bindscroll="scroll" bindscrolltolower="bottom">
          D
        </scroll-view>
      </swiper-item>
    </swiper>
  </view>
.tablist {
  height: 45px;
  width: 100%;
  box-sizing: border-box;
  line-height: 84rpx;
  background: #fff;
  font-size: 30rpx;
  color: #999;
  white-space: nowrap;
  overflow: hidden;
  padding: 0 20rpx;
}
.nav-item {
  color: #222;
  display: inline-block;
  text-align: center;
  width: 25%;
  font-size: 28rpx;
}
.nav-item.active{
  color: #ed413a;
  border-bottom: 4rpx solid #ed413a;
}
.tab-box{
  /* height: 74vh; */
  height: calc(100vh - 245px);
  padding-bottom: 25px;
  box-sizing: border-box;
}
.tab-content{
  overflow-y: scroll;
}

演示效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,可以使用swiperscroll-view组件来实现微信小程序的左右分功能。 首先,在wxml文件中添加swiperscroll-view组件,并设置相应的属性。 ```html <swiper class="swiper-container" current="{{current}}" duration="{{500}}" style="height:{{pageHeight}}px"> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第一内容--> </scroll-view> </swiper-item> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第二内容--> </scroll-view> </swiper-item> <!--更多swiper-item--> </swiper> ``` 其中,swiper组件设置了current属性,用于控制当前显示的码;scroll-view组件设置了scroll-x属性,用于开启水平方向的滚动,并设置了样式宽度为屏幕宽度,高度为面高度。 接着,在js文件中,监听swiper组件的change事件,用于动态更新当前码。 ```javascript Page({ data: { current: 0, windowWidth: wx.getSystemInfoSync().windowWidth, pageHeight: wx.getSystemInfoSync().windowHeight }, onLoad: function () { //... }, onPageChange: function (e) { this.setData({ current: e.detail.current }) } }) ``` 最后,在样式文件中,设置swiper-container和scroll-view的样式,用于实现左右分效果。 ```css .swiper-container { overflow: hidden; position: relative; } .scroll-view { white-space: nowrap; } .swiper-item { display: inline-block; vertical-align: top; width: 100%; } ``` 以上就是使用swiperscroll-view组件实现微信小程序左右分功能的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值