微信小程序 swiper选项卡切换 flex弹性布局+scroll-view撑开高度

写swiper的时候高度无法撑开,也不想用js动态设置高度,就可以利用flex弹性布局来撑开高度,效果如下。

在这里插入图片描述

wxml部分

<view class='ranking fz32'>
  <view class='ranking-date bgFFF'>
    <picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
      <view class="ranking-picker">
        <text class='picker-text fz26 fw400'>{{date}}</text>
        <image class="picker-image" src="/images/icon/arrow_d.png"></image>
      </view>
    </picker>
  </view>
  <view class='ranking-swiper'>
    <view class='swiper-tab bgFFF'>
      <view class="swiper-tab-item {{currentTab==index?'tabActive':''}}" wx:for="{{rankTitle}}" wx:key="index" data-current='{{index}}' bindtap="clickTab">
        <text>{{item.title}}</text>
      </view>
    </view>
    <swiper class='swiper-box bgFFF' duration="300" bindchange="swiperTab" current="{{currentTab}}">
      <swiper-item  wx:for="{{rankList}}" wx:key="index" duration="300">
        <view class='swiper-box-title'>
          <view>排名</view>
          <text>姓名</text>
          <view>核销券数</view>
        </view>
        <scroll-view scroll-y>
          <view class='swiper-box-list'>
            <view class='swiper-box-item' wx:for="{{item.subList}}" wx:for-item="newItem" wx:key="index">
              <view>{{newItem.rank}}</view>
              <image src='{{newItem.image}}'></image> 
              <text>{{newItem.name}}</text> 
              <view>{{newItem.score}}</view>
            </view>
          </view>
        </scroll-view>
      </swiper-item>
    </swiper>
  </view>
</view>

js部分

const app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2016-09-01',
    currentTab: 0,
    rankTitle: [{
        title: '销量榜',
      },
      {
        title: '销售额榜',
      },
      {
        title: '签约榜',
      }
    ],
    rankList: [{
      subList: [{
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: 'EOS主网启动直播',
          score: '12131',
        },
      ],
    }, {
      subList: [{
          rank: 1,
          image: '',
          name: '罗威',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: '罗威',
          score: '12131',
        },
      ],
    }, {
      subList: [{
          rank: 1,
          image: '',
          name: '杨峰',
          score: '12131',
        },
        {
          rank: 1,
          image: '',
          name: '杨峰',
          score: '12131',
        },
      ],
    }]
  },


  //日期选择
  bindDateChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换模式
  clickTab: function(e) {
    var that = this;
    if (that.data.currentTab == e.currentTarget.dataset.current){
      return false;
    }else{
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  }   

})

最后附上wxss

page {
  height: 100%;
  background-color: #F1F1F1;
  color: #191919;  
} 

.bgFFF{
  background: #fff
}

.ranking {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.ranking-date {
  border-top: 2rpx solid #e0e0e0;
  border-bottom: 2rpx solid #e0e0e0;
}

.ranking-picker {
  padding: 37rpx 30px;
}

.ranking-picker .picker-image {
  width: 20rpx;
  height: 20rpx;
  margin-left: 15rpx;
}

.ranking .ranking-swiper {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/*榜单选项卡*/

.swiper-tab {
  display: flex;
}

.swiper-tab-item {
  flex: 1;
  text-align: center;
  color: #969696;
  font-size: 24rpx;
  padding: 35rpx 0;
}

.swiper-tab-item.tabActive >text {
  padding: 35rpx 15rpx;
  color: #11be84;
  border-bottom: 1px solid #11be84;
}
 

.swiper-box {
  margin-top: 30rpx;
  flex: 1;
}

.swiper-box scroll-view {
  height: 100%; 
  padding-top:60rpx;
  box-sizing: border-box;
} 
.swiper-box-title {
  display: flex;
  height: 60rpx;
  line-height: 60rpx;
  border-bottom: 1rpx solid #f1f1f1;
  font-size: 24rpx;
  position: absolute;
  width: 100%;
  left: 0;
  top: 0;
}

.swiper-box-title >view:first-child {
  text-align: center;
  width: 120rpx;
}

.swiper-box-title >text {
  flex: 1;
  margin: 0 40rpx;
}

.swiper-box-title >view:last-child {
  text-align: center;
  width: 180rpx;
}

.swiper-box-item {
  display: flex;
  align-items: center;
  padding: 30rpx 0;
}

.swiper-box-item:nth-child(2n) {
  background: #f6f6f6;
}

.swiper-box-item >view:first-child {
  text-align: center;
  width: 120rpx;
}

.swiper-box-item >image {
  width: 68rpx;
  height: 68rpx;
  background: #11be84;
  border-radius: 100%;
}

.swiper-box-item >text {
  flex: 1;
  margin-left: 12rpx;
  line-height: 68rpx;
}

.swiper-box-item >view:last-child {
  text-align: center;
  width: 180rpx;
}


/*隐藏滚动条*/
::-webkit-scrollbar {
width: 0;
height: 0;
color: transparent;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
是的,可以使用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组件实现微信小程序左右分页功能的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值