vue3 + element-plus 设置表格自动滚动
侧边栏壁纸
  • 累计撰写 5 篇文章
  • 累计收到 37 条评论

vue3 + element-plus 设置表格自动滚动

枫叶
2024-11-29 / 7 评论 / 7 阅读 / 正在检测是否收录...

  前两天一朋友问我,说element-plus的table表单自动滚动会出现卡顿的问题,估计是没有使用requestAnimationFrame(请求动画帧)导致肉眼可见的卡顿。于是,我整理了下他的需求,给他写了一个demo,具体代码如下:

<template>
  <div>
    <!-- 设置表格高度,确保有足够的空间显示滚动条 -->
    <el-table
      :data="tableData"
      style="width: 100%; height: 300px;"
      ref="tableRef"
    >
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="Name" prop="name"></el-table-column>
      <el-table-column label="Age" prop="age"></el-table-column>
    </el-table>

    <el-button @click="startScrolling">Start Scrolling</el-button>
    <el-button @click="stopScrolling">Stop Scrolling</el-button>
    <el-button @click="scrollToTop">Scroll to Top</el-button>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    // 设置一组假数据
    const tableData = ref([
      { id: 1, name: 'John', age: 25 },
      { id: 2, name: 'Jane', age: 22 },
      { id: 3, name: 'Tom', age: 30 },
      { id: 4, name: 'Lucy', age: 27 },
      { id: 5, name: 'Mike', age: 35 },
      { id: 6, name: 'Sara', age: 28 },
      { id: 7, name: 'David', age: 33 },
      { id: 8, name: 'Emma', age: 26 },
      { id: 9, name: 'Liam', age: 29 },
      { id: 10, name: 'Olivia', age: 24 },
      { id: 11, name: 'Sophia', age: 26 },
      { id: 12, name: 'Jackson', age: 31 },
      { id: 13, name: 'Ava', age: 23 },
      { id: 14, name: 'Isabella', age: 32 },
      { id: 15, name: 'Ethan', age: 27 }
    ]);

    const tableRef = ref(null);
    let animationFrameId = null;
    let isScrolling = false;

    // 滚动表格函数
    const scrollTable = () => {
      if (!tableRef.value) return;

      // 拿到 table
      const table = tableRef.value.layout.table.refs;
      // 获取表格的滚动容器
      const tableBodyWrapper = table.bodyWrapper.firstElementChild.firstElementChild;

      console.log('tableBodyWrapper', tableBodyWrapper);
      if (!tableBodyWrapper) return;

      const scrollHeight = tableBodyWrapper.scrollHeight;
      const scrollTop = tableBodyWrapper.scrollTop;
      const clientHeight = tableBodyWrapper.clientHeight;

      // 如果滚动到底部就停止
      if (scrollTop + clientHeight >= scrollHeight) {
        cancelAnimationFrame(animationFrameId);
        isScrolling = false;
        return;
      }

      // 向下滚动
      tableBodyWrapper.scrollTop += 1;

      // 继续执行下一帧
      animationFrameId = requestAnimationFrame(scrollTable);
    };

    // 启动滚动
    const startScrolling = () => {
      if (!isScrolling) {
        isScrolling = true;
        scrollTable();
      }
    };

    // 停止滚动
    const stopScrolling = () => {
      cancelAnimationFrame(animationFrameId);
      isScrolling = false;
    };

    const scrollToTop = () => {
      if (!tableRef.value) return;
      // 拿到 table
      const table = tableRef.value.layout.table.refs;
      // 获取表格的滚动容器
      const tableBodyWrapper = table.bodyWrapper.firstElementChild.firstElementChild;
      if (tableBodyWrapper) {
        tableBodyWrapper.scrollTop = 0;  // 将滚动位置设置为 0,回到顶部
      }
    };

    return {
      tableData,
      tableRef,
      startScrolling,
      stopScrolling,
      scrollToTop
    };
  }
};
</script>

<style scoped>
/* 可以自定义样式 */
</style>
本文共 170 个字数,平均阅读时长 ≈ 1分钟
0

评论 (7)

取消
  1. 头像
    Typecho
    Linux · Google Chrome

    欢迎加入 Typecho 大家族

    回复
  2. 头像
    rcjkzrrmxz
    Windows 10 · Google Chrome

    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com

    回复
  3. 头像
    ksdmaaczzd
    Windows 10 · Google Chrome

    新盘首开 新盘首开 征召客户!!!coinsrore.com

    回复
  4. 头像
    wnvtoywhjk
    Windows 10 · Google Chrome

    2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

    回复
  5. 头像
    zawppsgdbj
    Windows 10 · Google Chrome

    2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

    回复
  6. 头像

    果博东方客服开户联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方公司客服电话联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方开户流程【182-8836-2750—】?薇- cxs20250806】
    果博东方客服怎么联系【182-8836-2750—】?薇- cxs20250806】

    回复
  7. 头像

    华纳圣淘沙公司开户新手教程

    零基础学会(183-8890-9465薇-STS5099)
    华纳圣淘沙公司开户

    华纳圣淘沙公司开户保姆级教程(183-8890-9465薇-STS5099)

    一步步教你开通华纳圣淘沙公司账户(183-8890-9465薇-STS5099)

    华纳圣淘沙公司开户分步图解

    首次开户必看:(183-8890-9465薇-STS5099)
    华纳圣淘沙全攻略

    华纳圣淘沙公司开户实操手册(183-8890-9465薇-STS5099)
    华纳圣淘沙开户流程视频教程

    手把手教学:(183-8890-9465薇-STS5099)
    华纳圣淘沙公司开户

    华纳圣淘沙公司开户完全指南(183-8890-9465薇-STS5099)

    回复