Avoiding the resizing of table headers when positioned at the top of a window

Having an issue with resizing elements on a webpage. I have dynamically generated a table from JSON data and have a function that sticks the table header to the top of the page when scrolling:

var header = $("#dataheader").offset();

$(window).scroll(function() {
if($(window).scrollTop() > header.top){
    $("#dataheader").css('top','0').css('background-color',white)
} else {
    $('#dataheader').css('position', 'static');
}
});

The CSS width for the header and cells are set to "auto", however, this causes a problem where the header resizes when fixed to the top, misaligning the cells with the columns.

Is there a way to prevent this resizing or to set the element widths according to the columns?

Answer №1

Ensure equal spacing around static position

const headerPosition = $("#dataheader").position();

$(window).scroll(function() {
if($(window).scrollTop() > header.top){
    $("#dataheader").css('position', 'static').css('top','0').css('width',header.width).css('left',header.left).css('background-color','white');
} else {
    $('#dataheader').css('position', 'relative').css('top','').css('width','100%').css('left','').css('background-color','');
}
});

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

An array is not just a mere collection of elements

I have an object that resembles an array var items = [ { started_time: 2017-05-04T12:46:39.439Z, word: 'bottle', questionId: '161013bd-00cc-4ad1-8f98-1a8384e202c8' }, { started_time: 2017-05-04T12:47:26.130Z, word: &apo ...

Increase the size of the grid system column upon clicking on a Bootstrap icon

I am using Google Maps in a tab with Bootstrap, but the map is too small. I would like to change the Bootstrap grid system when I click on a FontAwesome icon: the first column should turn into col-md-8, and the second column should become col-md-4. Here i ...

How can I limit the maximum number of list elements in a UL using jQuery?

Hello, I am using ajax to load search results in the form of list items. Each loaded and matched list item is given a class of .matched. Is there a way to limit the maximum number of .matched list items within my ul.searchresults? Does anyone have sugges ...

When testing the Next.js App locally, the APIs function properly. However, issues arise when attempting to deploy the app

Having trouble deploying my NextJS App APIs to Netlify. Everything runs smoothly locally, but I keep encountering this error when trying to deploy. https://i.sstatic.net/C8FUv.png ...

Implement a custom email template for the contact form utilizing Express and Sendgrid

I have set up a basic contact form that utilizes the Node Sendgrid helper library for sending emails. My goal is to incorporate a template email/contact.jade that will convert to HTML and include the necessary context. I understand that this template shou ...

The variable req.body.username is not defined in the context of JavaScript

I am completely new to JS, Angular.js and node.js. I am currently working on a login-register project but facing a minor issue. Below is my code: login.ctrl.js: var app = angular.module('login-register', []); app.factory('UserLog', ...

Django and Ajax working together with GET requests

Despite having experience in working with Python, Django, and REST framework, I am facing a challenge when trying to make an Ajax request. No matter how many similar questions I come across, I just can't seem to solve the problem at hand. The situatio ...

Is it possible to arrange a react map based on the length of strings?

I am working on a React function that loops through an object and creates buttons with text strings retrieved from the map. I want to display the text strings in order of their length. Below is the code snippet for the function that generates the buttons: ...

Using JavaScript on an iPhone to automatically launch Safari when clicking on a link from a non-default iOS

When "googlechrome://www.lego.com" is opened in mobile Safari, it switches to the Google Chrome iOS app to open the URL. This feature allows for scriptlets like the following one, which enables you to open the current page in the Google Chrome iOS app from ...

Modifying the label focus does not alter the CSS style

I'm struggling with a simple form where I want to highlight the focused labels by changing their background colors. However, the jquery code doesn't seem to be working as expected. Despite no errors showing up in the console, the functionality is ...

JavaScript property counterparts

Recently, I've been working on creating alias's for a specific property in my code. var default_commands = {} default_commands['foo'] = "bar"; My goal is to create multiple aliases for the key 'foo' in the object. For examp ...

Exploration of Non-height Divs

Repeatedly encountering the same issue - a fluid div with floated elements lacking a background due to having "no height." I've experimented with various solutions such as :after selectors, , and artificially setting a height, but none are particularl ...

What is the best method for serving cross-site content - JSONP, iframe, or a different approach?

In the process of developing an ad network, I am faced with the task of integrating third-party websites to include my JavaScript and replace specific divs with my content. Choosing which content to serve dynamically into these divs necessitates a cross-s ...

Error message: Unexpected token discovered, Functioned correctly on Windows yet encountering issues on the VPS. Any suggestions for resolving this?

Challenge: After transitioning my code from a Windows machine to a VPS, everything was working fine on my PC. However, upon migrating to the VPS, I encountered the error listed below: /root/node_modules/discord.js/src/client/Client.js:41 } catch { ...

Retrieving data with model.fetch in Backbone.js when the server response is null

In my app, we utilize model.fetch() to retrieve JSON from the server. The render function is triggered when the model undergoes a change and looks like this: if(_.isUndefined(this.model.get("id_number"))){ this.template = initialTemplate; } else if(th ...

Retrieve the hidden input values from a div using jQuery

Currently, I am in the midst of a project using CakePHP with jQuery and AJAX. Within this project, I have a div that is being repeated in a PHP loop. Here is what the div looks like: <div class="heart"> <input type="hidden" i ...

Printing reports in Angular 9

I have a requirement in my Angular 9 application to generate and print reports. 1. I am looking for suggestions on how to handle printing reports where a user triggers the action by clicking a button, and the report data needs to be fetched from the datab ...

AngularJS HTTP request not functioning properly with duplicate requests in Postman

My postman request is working fine, but the equivalent in angularJS isn't. I'm able to get the response I need in Postman, but for some reason, it's not working in Angular. var settings = { "async": true, "crossDomain": true, ...

Why isn't this code performing well when trying to alter the styling of DOM elements?

JavaScript is causing major issues and is performing poorly. Why is this code not working properly?? (this is a back to top button) function checkScrollTop(){ if (document.body.scrollTop > 300) { document.getElementById("backToTop").style.dis ...

Is it possible to create a visual representation (such as a jpg file) of a website page?

Looking to generate an image of a web page, like creating a miniature version of the HTML and images. It doesn't need to be exact (no need for Flash/JavaScript rendering). I plan to use this on Linux - preferably with a Java library, but a command li ...