Make div and table header fixed on scroll

2019-08-21 11:20发布

问题:

I have a div where I have a dropdown and then on the bottom there's a table. I am using ngx-infinite-scrolling, and when I scroll i want the div and the header of the table to remain fixed as more data is added and scrolled down.

This is a jsfiddle JSFiddle

Html code:

 <div class="topTable row ">

  <div class="col-md-4 col-lg-3 col-sm-4 col-6">
  <form class="form-inline">
      <div class="form-group" style="width:100%;max-width:200px;">
        <div class="input-group" style="width:100%">
            <select class="form-control"  >
                <option >All</option>
                <option >Option1</option>
                 <option >Option1</option>
                  <option >Option1</option>
              </select>

        </div>
   </div>


 <div class="container-fluid">
   <div class="col-md-12">
  <div class="col-md-12 tableDiv"  >
  <div class="constrained"  >
  <table class="table table-striped col-md-12 table-condensed cf" >
  <thead class="table-bordered cf">
    <tr>
      <th>Column1</th>
      <th>Column2</th>
      <th>Column3</th>
      <th>Column4</th>
      <th>Column5</th>
      <th>Column6</th>

    </tr>
  </thead>
  <tbody >
    <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>

   </tbody>
  </table>
 </div>
  <div>
  </div>
 </div>
</div> 
</div>

CSS:

.topTable{
 height:70px;
background:red;
margin-top:-20px;
margin-bottom:20px;     
display: flex;
padding-left: 32px;
border:1px solid rgba(0,0,0,.1)
}

.tableDiv{
margin-top:50px
}

I tried putting position:static on the div, but then on scroll down, table would go on top of it. How to also make table static and its header fixed ? Any help is appreciated

回答1:

You have to separate the top-block from the content-block, otherwise you make everything fixed.

Now you can make the first block fixed, the block has to get 100% width and z-index more than the content block (default is 0 -> so we need 1 or higher).

position:fixed;
width:100%;
z-index: 1;

I have forked your fiddle: http://jsfiddle.net/n958b7q3/1/ changes are marked.

Some tips:

html

  • You got to many divs, only make new div's if really needed
  • Your form has no closing tag </form>

css:

  • You forgot ; after border:1px solid rgba(0,0,0,.1)