skip to Main Content

I have a Table with a fixed header as below. The problem is that the card elements scroll on top of the fixed header. I have tried increasing z-index of thead and decreasing z-index of the card element. Nothing seems to be working.

document.getElementById("scrollarea").addEventListener("scroll", function() {
  var translate = "translate(0," + this.scrollTop + "px)";
  $(this).find("thead")[0].style.transform = translate;
});
#scrollarea {
  max-height: 200px;
  overflow: auto;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div id="scrollarea">
  <table class="table table-striped">
    <thead>
      <tr>
        <th>Row 1</th>
        <th>Row 2</th>
        <th>Row 3</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</div>

I want to make the thead be on top when the div is scrolled.

Any kind of help is much appreciated. Thank you.

2

Answers


  1. You should change the position property on scroll and also use z-index:

    // When the user scrolls the page, execute myFunction 
    window.onscroll = function() {myFunction()};
    
    // Get the navbar
    var navbar = document.getElementById("navbar");
    
    // Get the offset position of the navbar
    var sticky = navbar.offsetTop;
    
    // Add the sticky class to the navbar when you reach its scroll position. Remove "sticky" when you leave the scroll position
    function myFunction() {
      if (window.pageYOffset >= sticky) {
        navbar.classList.add("sticky")
      } else {
        navbar.classList.remove("sticky");
      }
    }
    /* Style the navbar */
    #navbar {
      overflow: hidden;
    }
    
    
    /* The sticky class is added to the navbar with JS when it reaches its scroll position */
    .sticky {
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 1;
      background: #FFF;
    }
    
    /* Add some top padding to the page content to prevent sudden quick movement (as the navigation bar gets a new position at the top of the page (position:fixed and top:0) */
    .sticky + .tbody {
      padding-top: 60px;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <div id="scrollarea">
      <table class="table table-striped">
        <thead id="navbar">
          <tr>
            <th>Row 1</th>
            <th>Row 2</th>
            <th>Row 3</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td colspan="3">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                </div>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    Login or Signup to reply.
  2. try changing table to div and add .sticky-top to the parent div.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search