"Enjoying a table header that scrolls freely with autoscroll feature

Resolved - http://jsfiddle.net/CrSpu/11704/

I'm facing an issue with a table that has autoscroll functionality.

I am looking to freeze the header of the table when automatic scrolling occurs, or you can test it out using my code pen.

I'm uncertain about how to address this problem and freeze the header table using <thead></thead>

This is the code snippet:

$(document).ready(function() {
  pageScroll();
  $("#contain").mouseover(function() {
    clearTimeout(my_time);
  }).mouseout(function() {
    pageScroll();
  });
});

function pageScroll() {
  var objDiv = document.getElementById("contain");
  objDiv.scrollTop = objDiv.scrollTop + 1;
  if (objDiv.scrollTop == (objDiv.scrollHeight - 100)) {
    objDiv.scrollTop = 0;
  }
  my_time = setTimeout('pageScroll()', 25);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contain">
  <table border="1">
    <thead>
      <tr>
        <th colspan="5">Info Data</th>
      </tr>
      <tr>
        <th>Name</th>
        <th>Gender</th>
        <th>Phone</th>
        <th>Country</th>
        <th>Position</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Salman</td>
        <td>Male</td>
        <td>0123456789</td>
        <td>Indonesia</td>
        <td>Front-end Developer</td>
      </tr>
    </tbody>
  </table>
</div>

Answer №1

Here's the solution that will solve your problem:

  1. Include the following code snippet in the tbody section: <tbody id="table_body">
  2. This code snippet defines the CSS styles:

    thead, tbody {

    display: block;
    

    }

    tbody {

    height: 100px;      
    overflow-y: scroll;  
    overflow-x: hidden;  
    

    }

  3. Lastly, make sure to update this line in your JavaScript code:

    document.getElementById("table_body");

Answer №2

Shared on behalf of the original poster.

The issue has been resolved by implementing the following JavaScript code:

var my_timer;
  scrollPage();
  $("#table_body").mouseover(function() {
    clearTimeout(my_timer);
  }).mouseout(function() {
    scrollPage();
  });

var $table = $('.scroll'),
    $bodyCells = $table.find('tbody tr:first').children(),
    columnWidth;

function scrollPage() {  
    var objDiv = document.getElementById("table_body");
  objDiv.scrollTop = objDiv.scrollTop + 1;  
  if (objDiv.scrollTop == (objDiv.scrollHeight - 100)) {
    objDiv.scrollTop = 0;
  }
  my_timer = setTimeout('scrollPage()', 25);
}

// Adjust the width of thead cells when window resizes
$(window).resize(function() {
    // Get the tbody columns width array
    columnWidth = $bodyCells.map(function() {
        return $(this).width();
    }).get();

    // Set the width of thead columns
    $table.find('thead tr').children().each(function(i, v) {
        $(v).width(columnWidth[i]);
    });    
}).resize(); // Trigger resize handler

A live demonstration can be viewed here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for detecting when no checkboxes in a group are selected or when at least one checkbox is selected, and then applying a class to the corresponding div

<div class="accordion-group"> <div class="accordion-heading"> <a href="#collapse" data-parent="#accordionQuiz" data-toggle="collapse1.." class="accordion-toggle"> <strong>1...</strong> Question ...

How can I dynamically change the orderBy parameter based on conditions in an Angular application?

I need to dynamically change the orderBy parameter in a table row based on the result of a method. Here is an example of my current tr setup - <tr class="pl" ng-repeat="thing in things | orderBy:'oldId':reverse" ng-class="{'row-error&apo ...

Avoid having a pre tag enclosed long line exceed its container's width

Within the <pre> </pre> tags, I have a lengthy single line of text that needs formatting. Is there a method to automatically wrap the text or must I manually insert <br> tags within the text? ...

Can you please tell me the name of this specific graph? And could you explain how I can recreate it on a

I am fully aware that this information is just a Google search away. However, I am unsure of the specific name of the chart I am seeking. Therefore, I am unable to conduct an effective search for suitable plugins or APIs. Your understanding is greatly appr ...

Leveraging a multi-dimensional JSON array to dynamically populate select options

I'm working on a scenario where I have 2 select boxes - one with static choices and the other being dynamic based on the selection from the first box. I'm using Ajax along with a PHP file that returns multiple arrays, and my goal is to populate t ...

Tips for achieving a design aesthetic similar to that of the JQuery UI website

On my website, I am using jQuery UI along with the "Smooth Default" CSS theme. However, I have noticed that everything appears larger compared to the jQuery UI website itself. For instance, when looking at the buttons here: http://jqueryui.com/button/ The ...

What is the best way to effectively handle the proxying of objects across multiple levels?

As illustrated in a Stack Overflow thread, utilizing Proxy objects is an effective method for monitoring changes in an object. But what if you need to monitor changes in subobjects? In such cases, you will also have to proxy those subobjects. I am curren ...

Making a POST request to a REST service in Rails 3 using Phonegap

I've encountered an issue with my rails3 rest service while developing a phonegap app using HTML5 and jQuery javascript. Specifically, I'm struggling to make a POST rest call with ajax jQuery due to cross domain security restrictions. Does anyon ...

Is it possible to update the URL dynamically in a Next.js application without relying on the <Link> component?

I'm looking for a way to dynamically change the URL on a page without using <Link>. Specifically, I want to switch to a different URL when a variable changes from false to true, regardless of user interaction. Currently, I'm achieving this ...

Is there a way to use jQuery to hide rows in a data table that contain a specific code, such as "1.1" or "1.2," that includes a

When using ajax, I pass data from two inputs to another page. These values are then used in a query to display the results in a data table on the main page within the success function. Now, I'm looking to implement a condition where any data containi ...

Avoid Conversion of HTML Entities in Table Cells

<table> <tr> <td>&gt;</td> </tr> <tr> <td>&&#xfeff;GT</td> </tr> </table> In the code snippet above, I have table cells containing HTML entities. A re ...

Can CSS be used to generate a grid or error image in Internet Explorer?

Can an image like this be created using CSS in Internet Explorer? I attempted to use filter: progid:DXImageTransform.Microsoft.gradient, but it was unsuccessful. ...

The child divs are not adjusting to the height of the parent container

My goal is to have the height of the children div .cell fill up 100% of the parent's height, but it doesn't seem to be working. This is the HTML code: <div class="header"> header </div> <div class="wrapper"> <di ...

The ng-if directive in AngularJS seems to be malfunctioning

Could someone please help me understand why my "Voted:" checkbox is not controlling the ng-if directive? Strangely enough, the "Keep HTML" checkbox is working fine. <p> <li ng-repeat="x in info"> {{x.name}} Voted: <input type="c ...

How can we effectively create reusable modals in React with the most efficient approach?

Currently, I am developing an application using React and Material UI. In order to streamline the use of Modals across multiple pages, I have implemented a global modal context that dynamically populates the modal with different props based on state change ...

What is the process for altering the color of an HTML output depending on its values?

I created a simple HTML code to showcase some outcomes. The possible results are SUCCESS, Failure, and Still Failing. I want these results to be displayed with corresponding colors, such as green for SUCCESS, and red for both Failure and Still Failing. I ...

What is the significance of the abbreviation 'dbo' in a MongoDB - Express application?

Below is the code snippet provided: app.js const express = require("express"); const app = express(); const cors = require("cors"); require("dotenv").config({ path: "./config.env" }); const port = process.env.PORT | ...

Superimpose above the tbody

I am attempting to implement a loading overlay specifically on top of the tbody element. My current method involves adding a tr as the last child of tbody and positioning it using position: absolute, while setting the parent tbody with position: relative. ...

Is there a way to retrieve the sub-child menu data from a JSON file?

I am currently working on creating a horizontal menu from a json file. However, I am facing issues in retrieving the subchild elements properly. Below is an example of my json file: var data = [{ "menu":[ { "MenuId":1, ...

Iterate through the array to verify that the specified conditions are satisfied for each individual item

I am working with an array of items and need to check if they all meet specific criteria. I've created a for loop to iterate through the array, but I'm concerned about its efficiency. Is there a more optimal way to achieve this? let match = 0; ...