小程序中如何固定表头和首列

<scroll-view scroll-x onScroll="scrollX" scroll-left="{{scrollLeft}}">
  <view class="table__head" style="width:{{tableWidth}}rpx;">
    <view class="table__head__td" a:for="{{headList}}" a:key="attrIndex" a:for-index="attrIndex" a:for-item="attrItem">
      <text class="table__head__td__text">{{attrItem.title}}</text>
    </view>
  </view>
</scroll-view>
<scroll-view scroll-y onScroll="scrollY" class="table" bindscrolltolower="handleScrollToLower">
  <scroll-view scroll-x onScroll="scrollX">
    <view style="height:{{tableHeight}}rpx;">
      <!-- <view class="table__head" style="width:{{tableWidth}}rpx;">
        <view class="table__head__td" a:for="{{headList}}" a:key="attrIndex" a:for-index="attrIndex" a:for-item="attrItem">
          <text class="table__head__td__text">{{attrItem.title}}</text>
        </view>
      </view> -->
      <view class="table__row" a:for="{{200}}" a:key="dataIndex" a:for-index="dataIndex" a:for-item="dataItem" style="width:{{tableWidth}}rpx;">
        <text class="table__row__td" a:for="{{headList}}" a:key="dataIndex" a:for-index="attrIndex" a:for-item="attrItem">{{dataIndex}}</text>
      </view>
    </view>
  </scroll-view>
</scroll-view>
.table{
  display: block;
  position: relative;
  overflow: scroll;
  width: 100%;
  height: 50vh;
}
.table__head{
  height: 96rpx;
  white-space: nowrap;
  position: sticky;
  top: 0rpx;
  z-index: 100;
  height: 88rpx;
  font-size: 24rpx;
  line-height: 88rpx;
  color: #aaabbd;
  background-color: #f8f8f8;
  border-bottom: 2rpx solid #ecf1f8;
  background-color: #fff;
  white-space: nowrap;
  display: flex;
}
.table__head__td{
  width: 200rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
}
.table__head__td:nth-child(1) {
  padding-left: 24rpx;
  width: 260rpx;
  position: sticky;
  z-index: 101;
  left: 0rpx;
}
.table__head__td__text{
  display: inline;
}
.table__row{
  position: relative;
  height: 96rpx;
  white-space: nowrap;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  border-bottom: 2rpx solid #ecf1f8;
}
.table__row__td{
  overflow: scroll;
  white-space: nowrap;
  width: 200rpx;
  display: inline-block;
  background-color: #fff;
  box-sizing: border-box;
  font-size: 26rpx;
  line-height: 96rpx;
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
  text-align: center;
}
.table__row__td:nth-child(1) {
  padding-left: 24rpx;
  width: 260rpx;
  position: sticky;
  z-index: 10;
  left: 0;
}
Page({
  data: {
    headList: [
      { title: '第一列' },
      { title: '第二列' },
      { title: '第三列' },
      { title: '第四列' },
      { title: '第五列' },
      { title: '第六列' },
    ],
    tableHeight: (200 + 1) * 96,
    tableWidth: 200 * 6 + 60,
  },
  scrollY(e) {
    this.setData({
      scrollTop: e.detail.scrollTop,
    });
  },
  scrollX(e) {
    this.setData({
      scrollLeft: e.detail.scrollLeft,
    });
  },
  onLoad() {
    my.setCanPullDown({
      canPullDown:false
    });
  },
})

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值