利用better-scroll插件做移动端table可上下左右滑动且固定表头和列

上下左右滑动时固定表头、列&上拉加载

思路
显示结构
.cross .row .row
data.clos data data
data.clos data data
思路步骤
  1. 配置BScroll配置项
  2. 声明BScrollscroll事件函数:onScroll,在里面取得所有.cross/.row/.clos元素,并对每个元素添加transform样式,且记录实时滚动的x,y值。.crossx,y实时同步;.rowx为0,y同步;.closx同步,y为0。
  3. 声明BScrollpullUpLoad事件(上拉加载)函数:onPullUpLoad,在函数里获取数据,用this.$nextTick()获取所有.cross/.row/.clos元素,并对每个元素添加transform样式,设置为声明时所记录的实时滚动的x,y值。
代码
// html
<div class="wrapper">
  <div class="content" ref="BScroll">
    <table>
      <tr>
        <th class="cross"></th>
        <th class="rows"></th>
        <th class="rows"></th>
      </tr>
      <tr>
        <td class="clos"></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td class=
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据提供的引用内容,我们可以使用vue3-seamless-scroll和a-table来实现固定表头的效果。具体步骤如下: 1.安装vue3-seamless-scroll插件 ```shell npm install vue-seamless-scroll --save ``` 2.在main.js文件中引入vue3-seamless-scroll插件 ```javascript import Vue from 'vue' import VueSeamlessScroll from 'vue-seamless-scroll' Vue.use(VueSeamlessScroll) ``` 3.在template中使用vue3-seamless-scroll组件,并将a-table作为子组件嵌套在其中。需要注意的是,需要将a-table的高度设置为固定值,并将其放在一个div中,以便于在滚动时固定表头。 ```html <template> <vue-seamless-scroll :speed="100" :pause-on-hover="true"> <div class="scroll-table" ref="scrollTable"> <a-table :columns="columns" :data-source="data" :scroll="{ y: 300 }"> <template #scroll-body="{ scrollBodyRef }"> <div class="table-body" ref="tableBody"> <a-table :columns="columns" :data-source="data" :scroll="{ y: 0 }"></a-table> </div> </template> </a-table> </div> </vue-seamless-scroll> </template> ``` 4.在script中定义columns和data,用于渲染a-table。 ```javascript <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', fixed: 'left', width: 100 }, { title: 'Age', dataIndex: 'age', width: 100 }, { title: 'Address', dataIndex: 'address', width: 200 }, { title: 'Action', key: 'action', fixed: 'right', width: 100, slots: { customRender: 'action' } } ], data: [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park' }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park' }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park' }, { key: '4', name: 'Disabled User', age: 99, address: 'Sidney No. 1 Lake Park' } ] } }, mounted() { this.$refs.scrollTable.addEventListener('scroll', (event) => { this.adjustTable(event) }) }, methods: { adjustTable(event) { const tableHeader = this.$refs.tableBody.querySelector('.ant-table-header') tableHeader.style.transform = `translateX(-${event.target.scrollLeft}px)` } } } </script> ``` 5.在style中定义样式,使得表头固定在顶部。 ```css <style> .scroll-table { height: 300px; overflow: auto; position: relative; } .table-body { height: 0; overflow: hidden; } .ant-table-header { position: sticky; top: 0; z-index: 1; background-color: #fff; } </style> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值