Make TBODY scrollable in Webkit browsers

后端 未结 14 1861
闹比i
闹比i 2020-11-29 20:33

I\'m aware of this question, but none of the answers work in Safari, Chrome, etc.

The accepted strategy (as demonstrated here) is to set the tbody height and overflo

相关标签:
14条回答
  • 2020-11-29 20:47

    Try the first method of this page, pure CSS with a single table (2 divs around the table, and the thead is positionned absolute) : http://www.cssplay.co.uk/menu/tablescroll.html Seems to work on FF4/IE9/IE8 in addition to IE7/FF3.6.

    0 讨论(0)
  • 2020-11-29 20:48

    Here is a working example:

    http://www.imaputz.com/cssStuff/bigFourVersion.html

    You have to add the display:block to the thead > tr and tbody

    0 讨论(0)
  • 2020-11-29 20:49

    I had the same issue and wrote a jQuery script to do this for me... uses two table elements and formats the css accordingly. Hope this helps others who have the same issue...

    http://jsfiddle.net/pe295/1/

    0 讨论(0)
  • 2020-11-29 20:50

    A faced the same problem long ago, and I finally set out the two tables approach. This is the result: http://jsfiddle.net/bGr4V/3/, it works for all browsers (IE6+ incl).

    In this jsFiddle you can play with a clean version.

    My solution was to add a fix cell <th class="fix"> </th> in thead to fill the space of the scroll bar in the tbody, then give one column a variable width (<td class="grow">), so the header fix cell wouldn't unmatch on resizing.

    HTML:

    <div class="fixed_table">
      <div class="head">
        <table>
          <thead>
            <tr>
              <th>Column header</th>
              <th class="grow">Column header</th>
              <th class="fix"> </th>
            </tr>
          </thead>
        </table>
      <div class="body">
        <table class="full_table">
          <caption class="caption">Caption</caption>
          <tbody>
            <tr>
              <td>Data</td>
              <td class="grow">Data</td>
            </tr>
            <tr>
              <td>...</td>
              <td>...</td>
            </tr>
            <tr>
              <td>...</td>
              <td>...</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    

    CSS: has * and _ hack for ie6-7, and a -webkit specific for the header fix-cell matching scroll width in each case.

    .fixed_table table {
      table-layout: fixed;
      width: auto;
      border-width: 0;
      padding: 0;
      border-collapse: collapse;
    }
    
    .fixed_table .body {
      overflow: scroll;
      overflow-x: hidden;
      max-height: 10.75em;
      min-height: 2.5em;
      padding-bottom: 0.8em;
      *padding-right: 17px; /*ie7 & ie6*/
      _padding-right: 0; /*ie6*/
      _height: 10em ;
    }
    
    .fixed_table th, .fixed_table td {
      width: 4.7em;
    }
    
    .fixed_table .grow {
      width: auto;
    }
    
    .fixed_table .fix {
      width: 16px;
      *width: 17px; /*ie7 & ie6*/
      _width: 16px; /*ie6*/
    }
    
    /* webkit specific */
    @media screen and (-webkit-min-device-pixel-ratio:0) {
      .fixed_table .fix{ width: 17px }
    }
    
    0 讨论(0)
  • 2020-11-29 20:53

    Let the table draw as it's way and calculate each column's width and set it in to each heading. Headings are made with divisions and then we can let the table to be scrolled free.

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    .t_heading{
      margin-top: 20px;
      font-family: Verdana, Geneva, sans-serif;
      background-color: #4CAF50;
    }
    
    .heading{
      background-color: #4CAF50;
      color: white;
      float:left;
      padding: 8px 0PX 8PX 0PX;
      border-bottom: 1px solid #ffffd;
      height: 20px;
      text-align: left;
    }
    
    .t_data{
      overflow-y: scroll;
      overflow-x: hidden;
      height:0px;
      width: 100%;
    
    }
    
    .t_content{
        border-collapse: collapse;
        font-family: Verdana, Geneva, sans-serif;
        width: 100%;
    }
    
    .column1,.column2,.column3,.column4{
        padding: 8px 0PX 8PX 0PX;
        text-align: left;
        border-bottom: 1px solid #ffffd;
    }
    .t_row:hover{
      background-color:#f5f5f5;
    }
    </style>
    <script>
    function setBody(){
        var body = document.body,
        html = document.documentElement;
    
        var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
        height = height-300;
        /*
         ** By changing the subtraction value, You can fit the table in to the screen correctly.
         ** Make sure not to come the hscroll.
         ** Or you can set fixed height with css for the div as your wish.
         */
        document.getElementById("t_data").style.height = height+"px";
    
        setColumnWidth("column1");
        setColumnWidth("column2");
        setColumnWidth("column3");
        setColumnWidth("column4");
    }
    
    function setColumnWidth(o){
            var object = o;
            var x = document.getElementsByClassName(object);
            var y = x[0].clientWidth;
            document.getElementById(object).style.width = y+"px";
     }
    </script>
    </head>
    
    <body onload="setBody()">
    <div class="t_heading">
    <div class="heading"  id="column1">Heading 1</div>
    <div class="heading"  id="column2">Heading 2</div>
    <div class="heading"  id="column3">Heading 3</div>
    <div class="heading"  id="column4">Heading 4</div>
    </div>
    <div class="t_data" id="t_data">
    <table class="t_content">
        <tr class='t_row'>
            <td class='column1'>Column1 Row 0</td>
            <td class='column2'>Column2 Row 0</td>
            <td class='column3'>Column3 Row 0</td>
            <td class='column4'>Column4 Row 0</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 1</td>
            <td class='column2'>Column2 Row 1</td>
            <td class='column3'>Column3 Row 1</td>
            <td class='column4'>Column4 Row 1</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 2</td>
            <td class='column2'>Column2 Row 2</td>
            <td class='column3'>Column3 Row 2</td>
            <td class='column4'>Column4 Row 2</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 3</td>
            <td class='column2'>Column2 Row 3</td>
            <td class='column3'>Column3 Row 3</td>
            <td class='column4'>Column4 Row 3</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 4</td>
            <td class='column2'>Column2 Row 4</td>
            <td class='column3'>Column3 Row 4</td>
            <td class='column4'>Column4 Row 4</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 5</td>
            <td class='column2'>Column2 Row 5</td>
            <td class='column3'>Column3 Row 5</td>
            <td class='column4'>Column4 Row 5</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 6</td>
            <td class='column2'>Column2 Row 6</td>
            <td class='column3'>Column3 Row 6</td>
            <td class='column4'>Column4 Row 6</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 7</td>
            <td class='column2'>Column2 Row 7</td>
            <td class='column3'>Column3 Row 7</td>
            <td class='column4'>Column4 Row 7</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 8</td>
            <td class='column2'>Column2 Row 8</td>
            <td class='column3'>Column3 Row 8</td>
            <td class='column4'>Column4 Row 8</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 9</td>
            <td class='column2'>Column2 Row 9</td>
            <td class='column3'>Column3 Row 9</td>
            <td class='column4'>Column4 Row 9</td>
        </tr><tr class='t_row'>
            <td class='column1'>Column1 Row 10</td>
            <td class='column2'>Column2 Row 10</td>
            <td class='column3'>Column3 Row 10</td>
            <td class='column4'>Column4 Row 10</td>
        </tr>
    <!--
    <?php
    $data = array();
    for($a = 0; $a<50; $a++)
    {
        $data[$a] = array();
        $data[$a][0] = "Column1 Row ".$a;
        $data[$a][1] = "Column2 Row ".$a;
        $data[$a][2] = "Column3 Row ".$a;
        $data[$a][3] = "Column4 Row ".$a;
    }
    /*
     ** supose you have data in an array.. which red from database. The table will draw using array data. Or you can draw the table manualy.
     ** tip: If using manual table, No need of
     ** 'var x = document.getElementsByClassName(object); var y = x[0].clientWidth;'.
     ** You can just set ID of first row's cells in to some name and use it to read width.
     */
    for($i=0;$i<sizeof($data);$i++){
        echo "<tr class='t_row'><td class='column1'>".$data[$i][0]."</td><td class='column2'>".$data[$i][1]."</td><td class='column3'>".$data[$i][2]."</td><td class='column4'>".$data[$i][3]."</td></tr>";
    }
    ?>
    -->
    </table>
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 20:54

    I saw Sean Haddy's excellent solution and took the liberty of making some edits:

    • Use classes instead of ID, so one jQuery script could be reused for multiple tables on one page
    • Added support for semantic HTML table elements like caption, thead, tfoot, and tbody
    • Made scrollbar optional so it won't appear for tables that are "shorter" than the scrollable height
    • Adjusted scrolling div's width to bring the scrollbar up to the right edge of the table
    • Made concept accessible by
      • using aria-hidden="true" on injected static table header
      • and leaving original thead in place, just hidden with jQuery and set aria-hidden="false"
    • Showed examples of multiple tables with different sizes

    Sean did the heavy lifting, though. Thanks to Matt Burland, too, for pointing out need to support tfoot.

    Please see for yourself at http://jsfiddle.net/jhfrench/eNP2N/

    0 讨论(0)
提交回复
热议问题