Looking for consistent vertical and horizontal scrolling behavior in a React project

I am in need of assistance as I lack the necessary expertise in ReactJs to transform this design. Currently, it is written in Jquery but I would like guidance on how to recreate it in ReactJs. Specifically, I want the left div with the topic for each row to remain sticky when scrolling horizontally. It is important that I do not rely on the jquery library anymore; I aim to implement this purely in react using typescript or emotion 10 or a combination of both.

Any help or suggestions would be greatly appreciated!

Here is the JSFiddle link for reference

Snippet of HTML Code:

<div class="main">
  <div class="row">
  <div class="sticky">
    <div>First1</div>
    ...
   </div>
    <div class="content">
      <div>row content</div>
      ...
        <div class="content">
      <div>row content1</div>
      ...
        <div class="content">
      <div>row content1</div>
    
  </div>
</div>

Preview of CSS Code:

.main {
  background-color:blue;
  overflow: scroll;
  height: 200px;
  width: 400px;
}

.row {
  height: 50px;
  clear: both;
  width: 1000px;
  position: relative;
  padding-left: 150px;
  box-sizing: border-box;
}
.sticky, .content {
  float: left;
  width: 150px;
  border: 1px solid black;
}
.sticky {
  background-color: red;
  position: absolute; left: 0; top: 0;
}
.content{
  background-color: green;
}

To achieve horizontal scrollbar similar to the fiddle, here is an example of jQuery code:

$('.main').scroll(function(){
    $(this).find('.sticky').css('left', $(this).scrollLeft());
});

Answer №1

js file :

import $ from 'jquery';  
class Test extends React.Component {

      componentDidMount(){
        $('.main').scroll(function(){
        $(this).find('.sticky').css('left', $(this).scrollLeft());
        });
      }

      render() {
        return (<div className="main">
      <div className="row">
      <div className="sticky">
        <div>First1</div>
        <div>First2</div>
        <div>First3</div>
        <div>First4</div>
        <div>First5</div>
        <div>First6</div>
        <div>First7</div>
        <div>First8</div>
        <div>First9</div>
        <div>First10</div>
        <div>First11</div>
        <div>First12</div>
        <div>First13</div>
        <div>First14</div>
       </div>
        <div className="content">
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
          <div>row content</div>
        </div>
            <div className="content">
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
        </div>
            <div className="content">
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
          <div>row content1</div>
        </div>
      </div>
    </div>);
      }
    }

    React.render(<Test />, document.getElementById('app'));

css file :

.main {
  background-color:blue;
  overflow: scroll;
  height: 200px;
  width: 400px;
}

.row {
  height: 50px;
  clear: both;
  width: 1000px;
  position: relative;
  padding-left: 150px;
  box-sizing: border-box;
}
.sticky, .content {
  float: left;
  width: 150px;
  border: 1px solid black;
}
.sticky {
  background-color: red;
  position: absolute; left: 0; top: 0;
}
.content{
  background-color: green;
}

and html file :

<div id="app">

 </app>

I trust this will be beneficial for you :)

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

Error: The function props.addToCart is not accessible

While attempting to trigger my action on the client's click of the "addToCart" button to add a new product to the cart, I encountered the error message: "TypeError: props.addToCart is not a function." I am relatively new to Redux and have grasped the ...

Craft a brief description for every individual component discovered

Is there a way to identify and shorten all elements containing a <tspan> tag to just ten characters each? For example: <tspan>Bla bla bla bla</tspan> <tspan>Bla bla bla bla</tspan> <tspan>Bla bla bla bla</tspan> ...

Is it possible to find a more efficient approach than calling setState just once within useEffect?

In my react application, I find myself using this particular pattern frequently: export default function Profile() { const [username, setUsername] = React.useState<string | null>(null); React.useEffect(()=>{ fetch(`/api/userprofil ...

Having trouble with typecasting in Angular 9 after receiving an HTTP response?

When initializing my component, it fetches student information from an API. Here is the ngOnInit code for component-version1: ngOnInit(): void { if(!this.student) { this.studentsService.getStudentDetail(this.id).subscribe( (response: Stu ...

Guide on exporting a function from a module as a class property

How to export a function as a class property from a module? Even if modifiers such as public are added, when a class property points to a function within a module, it behaves as though it is private. There are multiple ways to define a property (a, b, c ...

Retrieve the unique identifier of a single post from a JSON file within a NuxtJS project

Is there a way to retrieve the unique post id data from a JSON file in NuxtJS? created() { this.fetchProductData() }, methods: { fetchProductData() { const vueInstance = this this.$axios .get(`/json/products.json`) ...

Enhancing Mocha and Chai tests: Exploring ways to increase strictness

While working with Mocha and Chai, I have come across numerous unreliable tests. I want all of these tests to fail. Similar issues on StackOverflow are often due to async problems and not using done(). Regardless, even if that were my problem, I would stil ...

Rails CarrierWave and jQuery File Uploader

I'm curious about why I keep getting an error in Firebug every time I try to upload an image or images. I followed the tutorial found here: https://github.com/blueimp/jQuery-File-Upload/wiki/Rails-setup-for-V5. Initially, I encounter errors both when ...

Updating by clicking with auto-prediction feature

I have implemented an autosuggestion feature to display results from a database table while typing in an HTML field, and I am looking to utilize JavaScript to post another value from the same row where the autosuggested values are stored. https://i.stack. ...

jQuery's outerHeight() and height functions may experience flickering or fail to update properly when the window is resized to an odd number

Two elements are placed side by side on a webpage. One element, with a fixed size of 100vh, is named .hero-half, while the other element, holding text of varying lengths, is fluid and labeled as .project-details. When the text container extends beyond the ...

"Troubleshooting: Issue with CSS code on Codepen when trying to create

I am attempting to replicate the Google logo using HTML and CSS. While the code functions properly in browsers, there seems to be an issue with Codepen not positioning the horizontal blue line correctly. How can this be resolved? What adjustments should ...

PHP - Variables can only contain text characters

I am in the process of creating a website and I require a specific function. This function is intended to convert the string into plain text only. For example: $string = "Hello world<div width="100000">aaa</div>"; Rendering this as HTML cou ...

Is there a way to specifically retrieve the number of likes or followers from Facebook, Twitter, Instagram, and Snapchat in order to display them on my HTML website?

Can you provide guidance on how to obtain the total numbers of likes or followers for our Facebook, Twitter, Instagram, and Snapchat pages without including the plugin boxes or buttons? We are solely interested in the actual numbers. Would this be feasibl ...

MUI: The color utility previously known as 'fade' has been updated to 'alpha' in order to more accurately reflect its purpose within material-table

While working with material-table (latest version 1.69.3) and MUI v5.0.6 to create an editable table, I encountered this message in my console: https://i.stack.imgur.com/GXKl3.png The error mentions importing alpha, but as fade from the npm package mater ...

Improved efficiency in CSS for left transition animations

Here is the current code I am using: .s2 { top: 150px; left: 20px; position: absolute; transition: left 300ms linear; } I am currently updating the left position dynamically using JavaScript based on scroll events. However, I have noticed that th ...

Save the text found within an element to Appim wd.js

I am a beginner with Appium and I am currently exploring the workshop Git project, which utilizes wd.js and Grunt. From my research in the documentation and forums, I have learned that there are two methods for accessing the text of native elements within ...

Oops! It appears that there is an error saying "Data.filter is not a function"

I've encountered an issue where I pass a prop from parent to child and then return it, resulting in a 'filter is not a function' error. Any assistance in identifying the cause of this error would be greatly appreciated. Here is the array th ...

Unable to reach subdirectories on Nginx and React router while using a proxy server

I have set up an nginx server with a local web app running on port 4000. I managed to configure the Nginx rules for it to be accessible through a proxy, but now I can only access the website through the main URL, such as "https://app.domain.com" (which wor ...

Extract the HTML field value from an AJAX drop-down selection, rather than relying on user input

I have a field that allows users to select from a dropdown list using the following code: $(function() { $('.serial_number').live('focus.autocomplete', function() { $(this).autocomplete("/admi ...

Problem with the menu button overflowing

I'm exploring the process of designing burger menus for the mobile version of my site. I've successfully created a basic burger menu with some JavaScript assistance, but I encountered an overflow issue. The burger menu functions properly, but the ...