FastAdmin自定义滚动条

效果

实现过程

HTML代码

<style>
    .custom-scrollbar {
        position: fixed;
        /*bottom: 0px;*/
        height: 20px;
        width: 97.5%;
        overflow-y: scroll;
        overflow-x: scroll;
        z-index: 100;
    }

    #scrollDivTable{
        height: 20px;
    }

    /*原滚动条不显示*/
    /*.fixed-table-body::-webkit-scrollbar {*/
    /*    display: none;*/
    /*}*/
</style>

<div id="scrollDiv" class="custom-scrollbar">
    <div id="scrollDivTable"></div>
</div>

JS代码

在对于JS文件中的 var table = $("#table"); 下方添加如下代码:

// 自定义滚动条功能
var setupCustomScrollbar = function() {
    var $table = $('#table');
    var $scrollDiv = $('#scrollDiv');
    var $scrollDivTable = $('#scrollDivTable');

    // 移除可能存在的旧的克隆表格
    $scrollDivTable.empty();

    // 创建一个与原表格列宽相同的表格
    var $clonedTable = $table.clone().removeAttr('id').addClass('cloned-table');
    $clonedTable.find('tbody').remove(); // 只需要表头
    $scrollDivTable.append($clonedTable);

    // 同步原表格和克隆表格的列宽
    function syncColumnWidths() {
        $table.find('thead th').each(function(index) {
            var width = $(this).outerWidth();
                $clonedTable.find('thead th').eq(index).outerWidth(width);
            });
        $scrollDivTable.width($table.outerWidth());
    }

    // 绑定滚动事件
    $scrollDiv.off('scroll').on('scroll', function() {
        $table.parent('.fixed-table-body').scrollLeft($scrollDiv.scrollLeft());
    });

    $table.parent('.fixed-table-body').off('scroll').on('scroll', function() {
        $scrollDiv.scrollLeft($(this).scrollLeft());
    });

    // 窗口调整大小时重新同步列宽
    $(window).off('resize.customScrollbar').on('resize.customScrollbar', syncColumnWidths);

    // 表格数据加载完成后同步列宽
    $table.on('post-body.bs.table', function() {
        setTimeout(syncColumnWidths, 0); // 使用 setTimeout 确保在 DOM 更新后同步
    });

    // 初始同步列宽
    setTimeout(syncColumnWidths, 0);
};
onPostBody: function() {
    setupCustomScrollbar();
}

相关推荐

  1. wpf 为定义控件添加滚动

    2024-06-07 22:26:04       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 22:26:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 22:26:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 22:26:04       82 阅读
  4. Python语言-面向对象

    2024-06-07 22:26:04       91 阅读

热门阅读

  1. Redis几种部署模式介绍

    2024-06-07 22:26:04       23 阅读
  2. TypeScript 在前端开发中的应用

    2024-06-07 22:26:04       28 阅读
  3. 几何(geometry)

    2024-06-07 22:26:04       26 阅读
  4. 分布式光伏发电的工作原理、特点及优势

    2024-06-07 22:26:04       33 阅读
  5. Flink Watermark详解

    2024-06-07 22:26:04       24 阅读
  6. 大数据与数据科学的学科边界

    2024-06-07 22:26:04       25 阅读
  7. 使用机器学习做医学图像分类的开源项目集锦

    2024-06-07 22:26:04       32 阅读
  8. postgressql——ReadBuffer_common函数(7)

    2024-06-07 22:26:04       28 阅读
  9. MySQL优化器的SQL重写规则

    2024-06-07 22:26:04       30 阅读