The offsetLeft property in Jquery does not function properly when scrolling to the right

I am looking to update the left position while scrolling using the arrow keys or middle mouse button, rather than just when scrolling up or down.

My current script is as follows:

$(window).scroll(function() {
var scroll = $(window).scrollTop();

if(scroll >= 672) {
  $('.firm-row-th').addClass("stickyHeader");
}else {
  $('.firm-row-th').removeClass("stickyHeader");
}

if($('.stickyHeader')[0]) {
  $('.stickyHeader').css({ 'left': -$('.firm-container').scrollLeft() + "px" });
}
});

The CSS I am using:

.firm-container {
margin-left: 0px;
width: calc(100% - 8px);
white-space: nowrap;
border: 1px solid #222;
overflow-x: auto;
z-index: 10;
position: absolute;
background-color: #fff;
}
.firm-row {
display: table;
table-layout: fixed;
}
.firm-row-th {
display: table;
table-layout: fixed;
}

.stickyHeader {
position: fixed;
top:0;
}

For demonstration purposes, here is a fiddle example.

If you try scrolling right with the sticky header in place, you will notice that it does not refresh. However, scrolling down will trigger the refresh for the sticky header.

Answer №1

It seems like the issue lies in only listening to the scroll-event on the window, which handles up-down scrolling but not on the .firm-container for left-right scrolling.

To fix this, update your JavaScript code as follows:

$(window).scroll(function() {
    handleScroll();
});

$('.firm-container').scroll(function() {
    handleScroll();
});

function handleScroll() {
var scroll = $(window).scrollTop();

    if(scroll >= 2) {
    $('.firm-row-th').addClass("stickyHeader");//.css({ 'left': -$('.firmy-container').scrollLeft() + "px" });
    }else {
    $('.firm-row-th').removeClass("stickyHeader");
    }

    if($('.stickyHeader')[0])
    {
    $('.stickyHeader').css({ 'left': -$('.firm-container').scrollLeft() + "px" });
    }
}

Check out the updated code on jsFiddle: https://fiddle.jshell.net/j4v90rw4/2/

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

Converting a key-value pair string into an array in PHP: A comprehensive guide

When the checkbox is selected, I will extract the values listed below: attribute_value:samsung, attribute_id:1, spec_group_id:2, prod_id:1 To convert the above string into an array format and obtain a single array outside of the loop. Therefore, if I wa ...

Submitting multiple choices and various forms using PHP step-by-step guide

Looking for some guidance on optimizing my HTML/PHP form setup. Here's a breakdown of what I currently have: Form 1: Apple options: -big -medium -small Submit button Form 2: Orange options: -big -medium -small Submit button Ta ...

JavaScript: specify parameters for function inputs

I'm curious about optimizing Javascript/Typescript functions arguments for clean code. To demonstrate, let's consider a basic example. Imagine I have a React component with a view property as props: <Grid view="Horizontal" /> ty ...

Instructions for submitting a form to an HTML page in Django

I'm currently working on sending a form to an HTML file using Django. Here is the form I'm working with: from django import forms class contactForm(forms.Form): name = forms.CharField(required=False, max_length=100,help_text='100 character ...

Combining JavaScript files into a single JavaScript file with Angular

For my Angular 8 project, I have successfully imported Bootstrap CSS files into my styles.css file using the following code: /* You can add global styles to this file, and also import other style files */ @import "~bootstrap/dist/css/bootstrap.min.css"; @ ...

Bidirectional data flow in AngularJS Directives

In an effort to create a dynamic form with different "widgets" based on field types and parameters stored in a database, I have been exploring directives for handling layout changes in response to various scenarios. After experimenting with some examples, ...

Trouble with Buttons not appearing on Datatables.net

Despite numerous attempts and thorough testing, I am unable to get buttons to appear on a datatables.net table. I have diligently added all the necessary scripts for buttons and followed every initialization example provided on datatables.net with no succe ...

Is there a way to have content update automatically?

After writing this block of code, I discovered that clicking on one of the circles activates it and displays the corresponding content. Now, I am looking for a way to automate this process so that every 5 seconds, a new circle gets activated along with its ...

Click on the text within a paragraph element

Is there a way to retrieve the selected text or its position within a paragraph (<p>)? I'm displaying a text sentence by sentence using a Vue.js loop of paragraphs. <p class="mreadonly text-left mark-context" v-for="line in ...

The downward pointing placeholders are not disappearing and being deactivated in Internet Explorer

Having serious issues with Internet Explorer. I've been attempting to use jQuery to hide dropdown items, which is successful in all browsers except IE. Here's the code I originally used: $('TABLE TBODY select').find("option[value=&apos ...

What is the best way to visually refresh a polymer element that displays data from a frequently changing URL?

Hey there! I'm currently facing an issue with displaying a polymer element that contains some important information. This element is supposed to appear when I tap on an icon, but for some reason it doesn't show up even after the data has been loa ...

Suggestions on transitioning from jQuery version 1.2.6 to YUI 3?

I am currently working on a web application that utilizes both jQuery 1.2.6 and YUI 2.6.0, but I am considering upgrading one or both of these libraries. The most recent versions available at the time are jQuery 1.3.2 and YUI 3.0.0 (beta 1). Initially, we ...

I need to verify the existence of an email in the user table using CodeIgniter and AJAX

I am trying to create a feature where users can enter their email, click a button, and then check if the email is already in the database. However, my attempts so far have been unsuccessful. Model public function checkEmailExistence($email) { $th ...

What methods can I use to avoid unnecessary array elements from being generated in Javascript?

When working in PHP, performing the following code: $var = array(); $var[5000] = 1; echo count($var); Will result in the output of 1. However, in JavaScript, missing elements are created. <script type="text/javascript"> var fred = []; f ...

Transform a text into a JSON object that adheres to the correct format

I'm attempting to convert a string containing form values into a valid JSON object, but the JavaScript JSON.parse function is returning an array of char values instead. Here's my code: <!DOCTYPE html> <html> & ...

Following the completion of every asynchronous operation, a callback will be executed

I am facing a situation where I have an array of images that are uploaded with a series of asynchronous operations as shown below: for ( let key in imageFileObject ) { const imageFile = imageFileObject[key] dispatch('get_signed_request', ima ...

Retrieve the current character using the jQuery keyup function

I am looking to retrieve the currently pressed keyup value: For example, in my code snippet alert("wj");, I want to extract the 'j' value as it represents the current value being pressed in the input field. I'm interested in retrieving the ...

Using CSS3 easing on Mobile Safari can help create smooth transitions

Can anyone advise on how to recreate the easing/friction for touch events in Mobile Safari? I am trying to achieve a similar bounce-back effect when dragging contents past the top or bottom of a scroller. I experimented with the cubic-bezier easing functi ...

Can jQuery arrays be sent to TWIG?

I have a jQuery-retrieved array named days: Array ( [0] => Array ( [value] => 4 [name] => text_4 ) [1] => Array ( [value] => 7 [name] => text_7 ) ...

How can I achieve a smooth opacity transition without using jQuery?

Although I understand that JQuery offers functions for this purpose, I am interested in finding a solution using only pure javascript. Is there a way to change the CSS opacity setting over time? Perhaps utilizing a unix time stamp with millisecond precisi ...