Положение таблицы с полосой прокрутки, чтобы заполнить страницу - PullRequest
0 голосов
/ 21 января 2019

Я использую библиотеку jquery для создания пользовательской полосы прокрутки - https://codepen.io/dragospaulpop/pen/asBcI

Я создал простой пример, но крайне важно, чтобы таблица заполнялась до остальной части страницы (без активации стандартной полосы прокрутки браузера).

Мой код:

<head>
  <meta charset="UTF-8">
  <title>CSS Only Fixed Table Headers</title>
  <link rel='stylesheet' href='https://rawgit.com/dragospaulpop/cdnjs/master/ajax/libs/jQuery.custom.content.scroller/3.1.11/jquery.mCustomScrollbar.css'>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
  <script src='https://rawgit.com/dragospaulpop/cdnjs/master/ajax/libs/jQuery.custom.content.scroller/3.1.11/jquery.mCustomScrollbar.concat.min.js'></script>
</head>

<body>
<div style="width:100%;height:300px;background:red"></div>
<table class="fixed_headers">
    <thead>
        <tr>
            <th>Name</th><th>Color</th><th>Description</th>
        </tr>
    </thead>
    <tbody id="myBody">
    </tbody>
</table>     

<script>
    $(window).load(function(){

      $("#myBody").append("      <tr>        <td>Apple</td><td>Red</td><td>These are red.</td>      </tr>      <tr>        <td>Pear</td><td>Green</td><td>These are green.</td>      </tr>      <tr>        <td>Grape</td><td>Purple / Green</td><td>These are purple and green.</td>      </tr>      <tr>        <td>Orange</td><td>Orange</td><td>These are orange.</td>      </tr>      <tr>        <td>Banana</td><td>Yellow</td><td>These are yellow.</td>      </tr>      <tr>        <td>Kiwi</td><td>Green</td><td>These are green.</td>      </tr>      <tr>        <td>Plum</td><td>Purple</td><td>These are Purple</td>      </tr>      <tr>        <td>Watermelon</td><td>Red</td><td>These are red.</td>      </tr>      <tr>        <td>Tomato</td><td>Red</td><td>These are red.</td>      </tr>      <tr>        <td>Cherry</td><td>Red</td><td>These are red.</td>      </tr>      <tr>        <td>Cantelope</td><td>Orange</td><td>These are orange inside.</td>      </tr>      <tr>        <td>Honeydew</td><td>Green</td><td>These are green inside.</td>      </tr>      <tr>        <td>Papaya</td><td>Green</td><td>These are green.</td></tr><tr><td>Raspberry</td><td>Red</td><td>These are red.</td></tr><tr><td>Blueberry</td><td>Blue</td><td>These are blue.</td></tr><tr><td>Mango</td><td>Orange</td><td>These are orange.</td></tr><tr><td>Passion Fruit</td><td>Green</td><td>These are green.</td></tr>");

      $("tbody").mCustomScrollbar({
        theme:"light-3",
        scrollButtons:{
          enable:false
        },
        mouseWheel:{ preventDefault: true },
        scrollbarPosition: 'inside',
        autoExpandScrollbar:true,
        theme: 'dark'
      });
    });

</script>

<style>
    tbody {
      height: 150px;
      overflow: hidden;
      /*replace with auto to use default scroll*/
      /*overflow: auto;*/
    }
    .fixed_headers {
      table-layout: fixed;
      border-collapse: collapse;
      display: block;
      width: 100%;
    }
    .fixed_headers th {
      text-decoration: underline;
    }
    .fixed_headers th,
    .fixed_headers td {
      padding: 5px;
      text-align: left;
      box-sizing: border-box;
      display: inline-block;
      width: 33.33%;
    }
    .fixed_headers tr {
      display: block;
      width: 100%;
    }
    .fixed_headers thead {
      background-color: #333;
      color: #FDFDFD;
      display: block;
      width: 100%;
    }
    .fixed_headers tbody {
      display: block;
      width: 100%;
    }
    .fixed_headers tbody tr:nth-child(even) {
      background-color: #DDD;
    }

</style>
</body>

Я хочу сделать высоту таблицы: page_height-red_div_height (размер страницы можно изменить, чтобы он был в процентах). Таким образом, идеальным решением является то, что red_div всегда имеет одинаковую высоту, но высота таблицы не постоянна (может меняться в зависимости от размера окна).

Я попытался изменить css, удалив height: 150px; из tbody и поместил туда height: 100%;, но результат был неправильным - вместо пользовательской полосы прокрутки была активирована стандартная полоса прокрутки браузера. Затем я попытался изменить css на .fixed_headers tbody, но он также не работает.

Как можно решить эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...