当前位置:首页 > JavaScript

js实现表格滚动

2026-04-07 12:11:38JavaScript

实现表格滚动的方法

使用JavaScript实现表格滚动可以通过多种方式完成,以下是几种常见的方法:

固定表头滚动内容

通过CSS设置表格容器的固定高度和溢出滚动,同时保持表头固定:

<div class="table-container">
  <table>
    <thead>
      <tr>
        <th>Header 1</th>
        <th>Header 2</th>
      </tr>
    </thead>
    <tbody>
      <!-- 表格内容 -->
    </tbody>
  </table>
</div>
.table-container {
  height: 300px;
  overflow-y: auto;
}

使用CSS sticky定位表头

thead th {
  position: sticky;
  top: 0;
  background: white;
}

JavaScript实现同步滚动

当有多个需要同步滚动的表格时:

const tables = document.querySelectorAll('.sync-scroll');
tables.forEach(table => {
  table.addEventListener('scroll', function() {
    tables.forEach(t => {
      if(t !== table) {
        t.scrollLeft = table.scrollLeft;
      }
    });
  });
});

虚拟滚动优化性能

对于大型数据集,使用虚拟滚动技术只渲染可见区域的行:

class VirtualScrollTable {
  constructor(container, rowHeight, visibleRows) {
    this.container = container;
    this.rowHeight = rowHeight;
    this.visibleRows = visibleRows;
    this.data = [];
    this.startIndex = 0;

    this.init();
  }

  init() {
    this.container.style.height = `${this.visibleRows * this.rowHeight}px`;
    this.container.addEventListener('scroll', this.handleScroll.bind(this));
    this.render();
  }

  handleScroll() {
    this.startIndex = Math.floor(this.container.scrollTop / this.rowHeight);
    this.render();
  }

  render() {
    // 只渲染可见行
  }
}

平滑滚动动画

为滚动添加动画效果:

function smoothScroll(element, target, duration) {
  const start = element.scrollTop;
  const change = target - start;
  const startTime = performance.now();

  function animateScroll(currentTime) {
    const elapsed = currentTime - startTime;
    const progress = Math.min(elapsed / duration, 1);
    element.scrollTop = start + change * progress;

    if (progress < 1) {
      requestAnimationFrame(animateScroll);
    }
  }

  requestAnimationFrame(animateScroll);
}

响应式表格滚动

根据窗口大小调整表格滚动区域:

js实现表格滚动

function adjustTableHeight() {
  const windowHeight = window.innerHeight;
  const headerHeight = document.querySelector('header').offsetHeight;
  const tableHeight = windowHeight - headerHeight - 50; // 减去其他元素高度

  document.querySelector('.table-container').style.height = `${tableHeight}px`;
}

window.addEventListener('resize', adjustTableHeight);
adjustTableHeight(); // 初始调用

这些方法可以根据具体需求组合使用,创建出满足不同场景需求的表格滚动效果。

标签: 表格js
分享给朋友:

相关文章

网页表格制作css代码

网页表格制作css代码

基础表格样式 使用CSS为HTML表格添加基础样式,如边框、间距和背景色: table { width: 100%; border-collapse: collapse; margin:…

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Par…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的JWT令牌…

uniapp 表格导入

uniapp 表格导入

uniapp 表格导入的实现方法 使用 uni.chooseFile 选择文件 在 uniapp 中,可以通过 uni.chooseFile API 让用户选择本地文件。该 API 支持多平台,包括…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: fun…

js进度条实现

js进度条实现

使用HTML和CSS创建基础结构 在HTML中创建一个容器元素用于显示进度条,通常使用<div>元素。CSS用于设置进度条的样式,包括宽度、高度、颜色和圆角等属性。 <div cl…