el-table表头吸顶(支持fixed列)

css 文件 sticky-table-header.scss

.el-table[is-sticky] {
  overflow: initial;
  --sticky-top: 0px;
  --stick-zIndex: 5;

  .el-table__header-wrapper{
    position: sticky;
    top: var(--sticky-top);
    z-index: var(--stick-zIndex);
  }

  .el-table__fixed, .el-table__fixed-right{
    overflow: visible;
    z-index: var(--stick-zIndex);
    
    .el-table__fixed-header-wrapper {
      position: sticky;
      top: var(--sticky-top);
      width: 100%;
      overflow: hidden;
      z-index: var(--stick-zIndex);
    }
    .el-table__fixed-body-wrapper {
      width: 100%;
      overflow: hidden;
    }
  }

  .el-table__fixed-right {
    .el-table__fixed-header-wrapper {
      display: flex;
      justify-content: flex-end;
    }
    .el-table__fixed-body-wrapper {
      display: flex;
      justify-content: flex-end;
    }
  }

  &.el-table--border::after{
    z-index: var(--stick-zIndex);
  }
}

.el-table__fixed {
  --box-shadow: 10px 0 10px -10px rgba(0, 0, 0, 0.12);
}

.el-table__fixed-right {
  --box-shadow: -10px 0 10px -10px rgba(0, 0, 0, 0.12);
}

.el-table__fixed, .el-table__fixed-right {
  box-shadow: var(--box-shadow);
}

js 指令形式

import '@/styles/sticky-table-header.scss'

export default {
  bind(el, binding) {
    el.setAttribute('is-sticky', true)
    updateStickyTop(el, binding)
  },
  update(el, binding) {
    updateStickyTop(el, binding)
  }
}

const updateStickyTop = (el, binding) => {
  const { value, oldValue } = binding
  if (value === oldValue) return

  const top = Number(value)
  if (!isNaN(top)) {
    el.style.setProperty('--sticky-top', `${top}px`)
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容