如何修复表头?

时间:2019-03-01 01:25:41

标签: jquery html css bootstrap-4 fixed-header-tables

我希望固定表头,以便仅滚动表体。
我该如何实现?

* {
  padding: 0px;
  margin: 0px;
}

body {
  padding: 0px;
  margin: 0px;
}

.responsive {
  max-width: 100%;
  height: auto;
}

.table-striped1>tbody>tr:nth-child(odd)>td,
.table-striped1>tbody>tr:nth-child(odd)>th {
  background-color: #4CACFF;
}

.table-hover1 tbody tr:hover td {
  background-color: #21537F;
  color: #fff;
}

.table-striped2>tbody>tr:nth-child(odd)>td,
.table-striped2>tbody>tr:nth-child(odd)>th {
  background-color: lightgreen;
}

.table-hover2 tbody tr:hover td {
  background-color: green;
  color: #fff;
}

#home,
#menu1 {
  height: 300px;
  overflow: auto;
}

#buttonone {
  position: fixed;
  top: 350px;
  z-index: 1;
}

.rig {
  float: right;
  margin-right: 200px;
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<!-- Nav tabs -->
<ul class="nav nav-tabs">
  <li class="nav-item">
    <a class="nav-link active btn btn-info" data-toggle="tab" href="#home">Selected Goods List</a>
  </li>
  <li class="nav-item">
    <a class="nav-link btn btn-success" data-toggle="tab" href="#menu1">Ordered Goods Status</a>
  </li>

</ul>

<!-- Tab panes -->

<div class="tab-content">
  <div id="home" class=" tab-pane active">
    <table class="table table-striped1 table-hover1">

      <thead>
        <tr>
          <th>No</th>
          <th>Selected Goods</th>
          <th>Qut</th>
          <th>Remove</th>
        </tr>
      </thead>

      <tbody>
        <tr>
          <td>1</td>
          <td>Keybord</td>
          <td>
            1
          </td>
          <td>
            <a href="veg_menu.html" class="btn btn-danger" role="button">Remove</a>
          </td>
        </tr>

        <tr>
          <td>2</td>
          <td>Mouse</td>
          <td>
            1
          </td>
          <td>
            <a href="veg_menu.html" class="btn btn-danger" role="button">Remove</a>
          </td>
          <tr>
            <td>3</td>
            <td>Moniter</td>
            <td>
              1
            </td>
            <td>
              <a href="veg_menu.html" class="btn btn-danger" role="button">Remove</a>
            </td>
          </tr>
          <tr>
            <td>4</td>
            <td>cabinet</td>
            <td>
              1
            </td>
            <td>
              <a href="veg_menu.html" class="btn btn-danger" role="button">Remove</a>
            </td>
          </tr>
          <tr>
            <td>5</td>
            <td>SMBS</td>
            <td>
              1
            </td>
            <td>
              <a href="veg_menu.html" class="btn btn-danger" role="button">Remove</a>
            </td>
          </tr>
      </tbody>
    </table>
    <div class="rig">
      <a href="#" class="btn btn-success lang " role="button" id="buttonone">Order Selected Goods</a>
    </div>
  </div>


  <div id="menu1" class=" tab-pane fade">
    <table class="table table-striped2 table-hover2">
      <tr>
        <th>No</th>
        <th>Ordered goods</th>
        <th>Qut</th>
        <th>goods Status</th>
      </tr>

      <tr>
        <td>1</td>
        <td>moniter</td>
        <td>
          1
        </td>
        <td>
          sent
        </td>
      </tr>

      <tr>
        <td>2</td>
        <td>Mouse</td>
        <td>
          1
        </td>
        <td>

        </td>
      </tr>
      <tr>
        <td>3</td>
        <td>Mouse</td>
        <td>
          1
        </td>
        <td>

        </td>
      </tr>
      <tr>
        <td>4</td>
        <td>Mouse</td>
        <td>
          1
        </td>
        <td>

        </td>
      </tr>
      <tr>
        <td>5</td>
        <td>keybord</td>
        <td>
          1
        </td>
        <td>

        </td>
      </tr>
      <tr>
        <td>6</td>
        <td>keybord</td>
        <td>
          1
        </td>
        <td>

        </td>
      </tr>
      <tr>
        <td>7</td>
        <td>keybord</td>
        <td>
          1
        </td>
        <td>

        </td>
      </tr>
    </table>
    <div class="rig">
      <a href="#" class="btn btn-success lang " role="button" id="buttonone">Total Bill Amount</a>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

对于<thead>标签,您可以尝试使用

style="position:fixed"

style="position:sticky ; top:0px"

用于修复始终固定在顶部的表头。