el-table自定义样式,表头固定,数据过多时滚动

最终效果:(此处没体现出来滚动,数据没那么多)
1.表头固定,设置表头样式,修改表格背景色

<div class="category-table">

<el-table

ref="tableRef"

class="common-table"

height="100%"

:row-style="{ height: rowHeight + 'px' }"

:header-row-style="{

background: `url(${tableHeader}) center no-repeat !important`,

backgroundSize: `100% 100% !important`,

}"

style="width: 100%; height: 100%; background-color: transparent"

>

</el-table>

</div>

动态设置行高,在获取到数据以后,记得加一个$nextTick(),不然会报错

javascript 复制代码
// 在获取到表格数据后,判断数据长度大于0后调用
this.$nextTick(() => {
   this.initRowHeight();
});

// 设置行高
    initRowHeight() {
      let tableHeight =
        Math.round(this.$refs["tableRef"].$el.offsetHeight) -
        Math.round(this.$refs["tableRef"].$el.childNodes[1].offsetHeight);
      this.rowHeight = Math.floor(tableHeight / 10); // 返回小于等于最终结果的最大整数
      setTimeout(() => {
        if (this.$refs.tableRef) {
          this.$refs.tableRef.doLayout();
        }
      }, 1000);
    },
2.写在有scoped 的style标签内
css 复制代码
/* 显示滚动条 */
.category-table ::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  overflow-y: scroll;
}
/* 设置表格的滚动条宽度 */
.category-table >>> .el-table__body-wrapper::-webkit-scrollbar {
  width: 10px;
  height: 8px;
}
/*定义滚动条轨道 内阴影+圆角*/
.category-table >>> .el-table__body-wrapper::-webkit-scrollbar-track {
  border-radius: 8px;
  background-color: transparent;
}
/*定义滑块 内阴影+圆角*/
.category-table >>> .el-table__body-wrapper::-webkit-scrollbar-thumb {
  border-radius: 8px;
  box-shadow: inset 0 0 6px rgba(200, 209, 217, 0.3);
  background-color: rgba(76, 77, 77, 0.1);
}
3.公共的scss样式文件内,没有公共样式文件的话可以放在没有scoped的style标签内,有的话要保证在main.js里引入了
css 复制代码
/* el-table表格组件样式 */
.common-table {

    /* 表格加载中的背景 */
    .el-loading-mask {
        background-color: transparent;
    }

    /** 设置表格暂无数据样式 */
    .el-table__empty-block {
        background-color: transparent;
        color: #a8bfd5;
        letter-spacing: 2px;
    }

    /** 修改表头多选样式 */
    .el-checkbox__inner {
        background-color: transparent;
    }

    .el-checkbox__inner:hover {
        border: 1px solid #6d90ae;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #1173be;
    }

    /* 设置表头样式 */
    &.el-table .el-table__header-wrapper th {
        color: #85b4e6;
        font-weight: normal;
        font-size: var(--font-size-base);
        letter-spacing: 2px;
        background-color: transparent !important;
        background: url("/images/imagine/table-header.png") center no-repeat;
        background-size: 100% 100%;
        border-bottom: 1px solid #0b4f85;
        box-sizing: border-box;
    }

    /** 设置表格的行背景色 */
    .el-table__row {
        background: url("/images/imagine/table-row1.png") center no-repeat;
        background-size: 100% 100%;
    }

    /** 去掉每一行的底边border */
    &.el-table td.el-table__cell {
        color: #aec4da;
        border-bottom: 1px solid #0b4f85;
        font-size: var(--font-size-base);
        letter-spacing: 2px;
    }

    /* 修改表格上侧和左侧的border */
    &.el-table--border,
    &.el-table--group,
    /* 修改表格右侧和底侧的border */
    &.el-table--border:after,
    &.el-table--group:after,
    &.el-table:before {
        border-color: transparent;
    }

    /* 删除表格右侧的border */
    &.el-table--border::after {
        width: 0;
    }

    /** 设置表格左侧第一列的边 */
    &.el-table td.el-table__cell:first-child {
        border-left: 1px solid #0b4f85;
    }

    /** 去掉表格的底边border */
    &.el-table::before {
        height: 0;
    }

    /** 去掉表格头部的border */
    &.el-table--border {
        border: none;
    }

    /** 表格内部每一列右侧border */
    &.el-table--border .el-table__cell {
        border-right: 1px solid #0b4f85;
    }

    /** 表格行的鼠标滑过样式 */
    &.el-table tbody tr:hover>td {
        background: url("/images/imagine/table-row2.png") center no-repeat !important;
        background-size: 100% 100% !important;
    }

    /* 去掉表格滚动条那一列的border */
    &.el-table--border th.el-table__cell.gutter:last-of-type {
        border-bottom-width: 0;
    }
}
相关推荐
IT_陈寒3 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen3 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
Larcher3 小时前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
默_笙3 小时前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别
javascript
牧艺3 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
jump_jump4 小时前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染
javascript·性能优化·前端工程化
红尘散仙4 小时前
想写一个像样的终端 App?试试把 React 的开发体验搬进 Rust TUI
前端·rust
袋鼠云数栈UED团队5 小时前
一套 Spec-First 的 AI 编程工作流
前端·人工智能
袋鼠云数栈前端5 小时前
一套 Spec-First 的 AI 编程工作流
前端·ai+
angerdream5 小时前
Android手把手编写儿童手机远程监控App之vue3 路由守卫
前端