Navbar and THead Fixed-Top

时间:2019-03-19 15:16:13

标签: javascript jquery html twitter-bootstrap

I am creating a simple site that will show a list of computers along with some of their information. So far I've got everything functioning pretty much how I want except I can't get the thead to stay fixed to the navbar while scrolling. I'm still pretty new to all this, but I'll list some of the things I've tried below along with a simplified version of my web page.

  1. Putting a div around navbar/table through thead: Doesn't scroll
  2. Making the tbody scrollable and fixing the position of navbar/thead: ruined formatting
  3. Applying fixed position just to thead: Also ruins formatting

Any help is greatly appreciated!

HTML

    <nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input id="myInput" onkeyup="myFunction()" class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>
<br><br>
<table id="myTable" class="table table-striped table-hover tablesorter">
  <thead>
    <tr>
      <th scope="col">Status</th>
      <th scope="col">Computer</th>
      <th scope="col">IP Address</th>
      <th scope="col">MAC</th>
    </tr>
  </thead>
  <tbody id="myTableSearch">
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
     <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
     <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
     <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
  </tbody>
</table>

JS

$(function() {
  $("#myTable").tablesorter();
});

    $(document).ready(function(){
      $("#myInput").on("keyup", function() {
        var value = $(this).val().toLowerCase();
        $("#myTableSearch tr").filter(function() {
          $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
        });
      });
});

https://jsfiddle.net/80Lt2d6p/

3 个答案:

答案 0 :(得分:2)

我们可以使用here提供的答案进行以下修改:

net use X: "\\SERVERNAME\FOLDER" /user:"USER" "PASSWORD"
START X:\

现在移除.table-fixed { width: 100%; } .table-fixed thead th { position: sticky; position: -webkit-sticky; /*top: 0;*/ top:55px; /* the height of the fixed nav-bar */ z-index: 999; background-color: #000; color: #fff; } <br>之后的nav-bar

HTML

这是更新的JSFiddle:
DEMO

答案 1 :(得分:2)

也许这会对您有所帮助。这是基于您自己的小提琴。

1st)通过简单的检查使thead固定,如下所示

$(window).scroll(function() {
if ($(this).scrollTop() >= 100) { 
    $('#myTable').addClass('fixed');  
} else {
            $('#myTable').removeClass('fixed');
    }
});

2nd)应用一些CSS

#myTable.fixed thead { 
  position: fixed; background: #dddddd;
  left: 0; width: 100%;
}

我不确定哪种格式会被破坏,如果您正在谈论th,那么可以使用一些CSS设置样式

https://jsfiddle.net/bLjdhc9x/1/

答案 2 :(得分:1)

.table-area {
  position: relative;
  z-index: 0;
  margin-top: 60px;
}

table.rajresponsive-table {
  display: table;
 
  table-layout: fixed;
  
  width: 100%;
 
  height: 100%;
}

table.rajresponsive-table thead {
  position: fixed;
  top: 50px;
  left: 0;
  right: 0;
  width: 100%;
  height: 50px;
  line-height: 3em;
  
  table-layout: fixed;
  display: table;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input id="myInput" onkeyup="myFunction()" class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>
<br><br>
<section class="content-area">
  <div class="table-area">
    <table class="rajresponsive-table table">
  <thead>
    <tr>
      <th scope="col">Status</th>
      <th scope="col">Computer</th>
      <th scope="col">IP Address</th>
      <th scope="col">MAC</th>
    </tr>
  </thead>
  <tbody id="myTableSearch">
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
     <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
     <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
     <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 1</td>
      <td>192.168.1.21</td>
      <td>92:41:04:FF:15:EB</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 2</td>
      <td>192.168.1.22</td>
      <td>5C:3B:C8:EA:6C:3A</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 3</td>
      <td>192.168.1.23</td>
      <td>BD:2D:94:8A:63:83</td>
    </tr>
    <tr>
      <td><span class="badge badge-success">Online</span></td>
      <td>Computer 4</td>
      <td>192.168.1.24</td>
      <td>9C:D7:16:1F:0B:A3</td>
    </tr>
    <tr>
      <td><span class="badge badge-danger">Offline</span></td>
      <td>Computer 5</td>
      <td>192.168.1.25</td>
      <td>05:C4:95:A6:B2:E6</td>
    </tr>
  </tbody>
</table>
 </div>
</section>