【问题标题】:HTML table with mcustomscrollbar and fixed column header带有 mcustomscrollbar 和固定列标题的 HTML 表格
【发布时间】:2016-02-09 10:34:59
【问题描述】:

我创建了一个带有滚动条的表格,其中标题在滚动 y 时固定,而标题在滚动表格 x 时滚动。这是我的代码。

$(function () {
	$('table').on('scroll', function () {
		$("table > *").width($("table").width() + $("table").scrollLeft());
  });
});
html {
    font-family: verdana;
    font-size: 10pt;
    line-height: 25px;
}
table {
    border-collapse: collapse;
    width: 300px;
    overflow-x: scroll;
    display: block;
}
thead {
    background-color: #EFEFEF;
}
thead, tbody {
    display: block;
}
tbody {
    overflow-y: scroll;
    overflow-x: hidden;
    height: 140px;
}
td, th {
    min-width: 100px;
    height: 25px;
    border: dashed 1px lightblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
            <th>Column 4</th>
            <th>Column 5</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Row 1</td>
            <td>Row 1</td>
            <td>Row 1</td>
            <td>Row 1</td>
            <td>Row 1</td>
        </tr>
        <tr>
            <td>Row 2</td>
            <td>Row 2</td>
            <td>Row 2</td>
            <td>Row 2</td>
            <td>Row 2</td>
        </tr>
        <tr>
            <td>Row 3</td>
            <td>Row 3</td>
            <td>Row 3</td>
            <td>Row 3</td>
            <td>Row 3</td>
        </tr>
        <tr>
            <td>Row 4</td>
            <td>Row 4</td>
            <td>Row 4</td>
            <td>Row 4</td>
            <td>Row 4</td>
        </tr>
        <tr>
            <td>Row 5</td>
            <td>Row 5</td>
            <td>Row 5</td>
            <td>Row 5</td>
            <td>Row 5</td>
        </tr>
        <tr>
            <td>Row 6</td>
            <td>Row 6</td>
            <td>Row 6</td>
            <td>Row 6</td>
            <td>Row 6</td>
        </tr>
        <tr>
            <td>Row 7</td>
            <td>Row 7</td>
            <td>Row 7</td>
            <td>Row 7</td>
            <td>Row 7</td>
        </tr>
        <tr>
            <td>Row 8</td>
            <td>Row 8</td>
            <td>Row 8</td>
            <td>Row 8</td>
            <td>Row 8</td>
        </tr>
        <tr>
            <td>Row 9</td>
            <td>Row 9</td>
            <td>Row 9</td>
            <td>Row 9</td>
            <td>Row 9</td>
        </tr>
        <tr>
            <td>Row 10</td>
            <td>Row 10</td>
            <td>Row 10</td>
            <td>Row 10</td>
            <td>Row 10</td>
        </tr>
    </tbody>
</table>

但问题是我无法使用 mcustomscrollbar 实现此功能。我想要这种使用 mcustomscrollbar 或滚动条上不受浏览器影响的任何其他有吸引力的 CSS 滚动的功能。如果有人知道这个问题的解决方案,那么将不胜感激。

【问题讨论】:

    标签: jquery css scroll html-table mcustomscrollbar


    【解决方案1】:

    检查以下内容:

    在你的脚本中添加这个 -

    $(".dataTables_scrollBody").mCustomScrollbar({  // custom scroll bar plugin
    theme:"minimal-dark",  // custom scroll theme set
    axis: "yx",     // custom scroll ver,hor scroll
    callbacks: {    // callback for thead,tbody scroll
    whileScrolling: function() {
    $(".dataTables_scrollHead").scrollLeft(this.mcs.left *-1);
    },
    },
    });
    

    希望,它会有所帮助。需要更多信息也检查一下 -

    http://manos.malihu.gr/repository/custom-scrollbar/demo/examples/callbacks_example.html

    【讨论】:

      【解决方案2】:

      制作一张这样的表格

      <div class="graphData" data-mcs-axis="x">
          <table class="fixed_headers">
              <thead>
                  <tr><th>data1</th><th>data2</th><th>data3</th></tr>   
              </thead>
              <tbody >
                  <tr><td>data1</td><td>data2</td><td>data3</td></tr>
                  <tr><td>data1</td><td>data2</td><td>data3</td></tr>
                  <tr><td>data1</td><td>data2</td><td>data3</td></tr>
                  ...
              </tbody>
          </table>
      </div>
      

      在&lt;/body&gt;之前添加脚本

      $(window).load(function(){
      
        $("table.fixed_headers tbody").mCustomScrollbar({
          mouseWheel:{ preventDefault: true },
          autoExpandScrollbar:true
        });
      
        $(".graphData").mCustomScrollbar({
                  scrollbarPosition:"outside"
        });
      });
      

      此脚本将添加x 滚动到标题和xy 滚动到tbody

      【讨论】:

        【解决方案3】:

        检查以下方式,您可以使用mCustomScrollbar。使用$("tbody").mCustomScrollbar({

        $(function () {
        	$('table').on('scroll', function () {
        		$("table > *").width($("table").width() + $("table").scrollLeft());
          });
        
          $("tbody").mCustomScrollbar({
            theme:"light-3",
            scrollButtons:{
              enable:false
            },
            mouseWheel:{ preventDefault: true },
            scrollbarPosition: 'inside',
            autoExpandScrollbar:true,
            theme: 'dark',
             axis:"yx",
                        setWidth: "auto"
          });
        
        });
        html {
            font-family: verdana;
            font-size: 10pt;
            line-height: 25px;
        }
        table {
            border-collapse: collapse;
            width: 300px;
            overflow: hidden;
            display: block;
        }
        thead {
            background-color: #EFEFEF;
        }
        thead, tbody {
            display: block;
        }
        tbody {
            overflow: hidden;
            height: 140px;
        }
        
        td, th {
            min-width: 100px;
            height: 25px;
            border: dashed 1px lightblue;
        }
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <link href="https://rawgit.com/dragospaulpop/cdnjs/master/ajax/libs/jQuery.custom.content.scroller/3.1.11/jquery.mCustomScrollbar.css" rel="stylesheet"/>
        <script src="https://rawgit.com/dragospaulpop/cdnjs/master/ajax/libs/jQuery.custom.content.scroller/3.1.11/jquery.mCustomScrollbar.concat.min.js"></script>
        
        <table>
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                    <th>Column 3</th>
                    <th>Column 4</th>
                    <th>Column 5</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Row 1</td>
                    <td>Row 1</td>
                    <td>Row 1</td>
                    <td>Row 1</td>
                    <td>Row 1</td>
                </tr>
                <tr>
                    <td>Row 2</td>
                    <td>Row 2</td>
                    <td>Row 2</td>
                    <td>Row 2</td>
                    <td>Row 2</td>
                </tr>
                <tr>
                    <td>Row 3</td>
                    <td>Row 3</td>
                    <td>Row 3</td>
                    <td>Row 3</td>
                    <td>Row 3</td>
                </tr>
                <tr>
                    <td>Row 4</td>
                    <td>Row 4</td>
                    <td>Row 4</td>
                    <td>Row 4</td>
                    <td>Row 4</td>
                </tr>
                <tr>
                    <td>Row 5</td>
                    <td>Row 5</td>
                    <td>Row 5</td>
                    <td>Row 5</td>
                    <td>Row 5</td>
                </tr>
                <tr>
                    <td>Row 6</td>
                    <td>Row 6</td>
                    <td>Row 6</td>
                    <td>Row 6</td>
                    <td>Row 6</td>
                </tr>
                <tr>
                    <td>Row 7</td>
                    <td>Row 7</td>
                    <td>Row 7</td>
                    <td>Row 7</td>
                    <td>Row 7</td>
                </tr>
                <tr>
                    <td>Row 8</td>
                    <td>Row 8</td>
                    <td>Row 8</td>
                    <td>Row 8</td>
                    <td>Row 8</td>
                </tr>
                <tr>
                    <td>Row 9</td>
                    <td>Row 9</td>
                    <td>Row 9</td>
                    <td>Row 9</td>
                    <td>Row 9</td>
                </tr>
                <tr>
                    <td>Row 10</td>
                    <td>Row 10</td>
                    <td>Row 10</td>
                    <td>Row 10</td>
                    <td>Row 10</td>
                </tr>
            </tbody>
        </table>

        【讨论】:

        • 垂直滚动时使用标题没问题。但是在滚动 x 时,标题也应该滚动。
        猜你喜欢
        • 1970-01-01
        • 2010-10-15
        • 2011-04-30
        • 2014-09-08
        • 1970-01-01
        • 2012-01-27
        • 2017-06-19
        • 1970-01-01
        • 2016-11-24
        相关资源
        最近更新 更多