Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
How to scroll items simultaneously when they are within a table?

I have a huge table where I want to introduce scrolling for the data. I don't want to separate into two tables, but scroll the data simultaneously. Here is a simplified code in JSFiddle and below:

https://jsfiddle.net/oy4pdz8t/7/

<table>
<tr>
<td>fixed</td>
  <td>
    <div id="scrolling1" class=" linked">
      <div class="data">1</div>
      <div class="data">2</div>
      <div class="data">3</div>
      <div class="data">4</div>
      <div class="data">5</div>
      
    </div>
  </td>
</tr>
<tr>
<td>fixed</td>
  <td>
    <div  id="scrolling2" class=" linked">
      <div class="data">1</div>
      <div class="data">2</div>
      <div class="data">3</div>
      <div class="data">4</div>
      <div class="data">5</div>
      
    </div>
  </td>
</tr>
</table>

The CSS is as follows:

#scrolling1{
  display: flex;
  overflow: auto;
  width: 100px;
  background: yellow;
  height: 50px;
  align-items: center;
}

#scrolling2{
  display: flex;
  overflow: auto;
  width: 100px;
  background: green;
  height: 50px;
  align-items: center;
}

.data{
  display: flex;
  flex: 0 0 40px;
  background: red;
  
}

And the jQuery is:

$(function(){

    $('.linked').scroll(function(){
        $('.linked').scrollTop($(this).scrollTop());    
    })

})

Is it possible to make the two scrolling sections scroll simultaneously. As mentioned above, I don't want to have to touch the table structure, since it would be a nightmare.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to use scrollLeft() instead of scrollTop(). scrollLeft() is for horizontal scroll while scrollTop() is for vertical scroll.

$(function() {

  $('.linked').scroll(function() {
    $('.linked').scrollLeft($(this).scrollLeft());
  })

})
#scrolling1 {
  display: flex;
  overflow: auto;
  width: 100px;
  background: yellow;
  height: 50px;
  align-items: center;
}

#scrolling2 {
  display: flex;
  overflow: auto;
  width: 100px;
  background: green;
  height: 50px;
  align-items: center;
}

.item {
  display: flex;
  flex: 0 0 40px;
  background: red;
}
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<table>
  <tr>
    <td>fixed</td>
    <td>
      <div id="scrolling1" class=" linked">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>

      </div>
    </td>
  </tr>
  <tr>
    <td>fixed</td>
    <td>
      <div id="scrolling2" class=" linked">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>

      </div>
    </td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Report

0

$('.linked').on('scroll', function() {
  var y = $(this).scrollLeft();
  $('.linked').scrollLeft(y);
});
#scrolling1{
  display: flex;
  overflow: auto;
  width: 100px;
  background: yellow;
  height: 50px;
  align-items: center;
}

#scrolling2{
  display: flex;
  overflow: auto;
  width: 100px;
  background: green;
  height: 50px;
  align-items: center;
}

.data{
  display: flex;
  flex: 0 0 40px;
  background: red;
  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>fixed</td>
  <td>
    <div id="scrolling1" class="linked">
      <div class="data">1</div>
      <div class="data">2</div>
      <div class="data">3</div>
      <div class="data">4</div>
      <div class="data">5</div>
      
    </div>
  </td>
</tr>
<tr>
<td>fixed</td>
  <td>
    <div  id="scrolling2" class="linked">
      <div class="data">1</div>
      <div class="data">2</div>
      <div class="data">3</div>
      <div class="data">4</div>
      <div class="data">5</div>
      
    </div>
  </td>
</tr>
</table>

You can use jQuery's scroll functions.

Explanation:

Line Explanation
$('.linked').on('scroll', function() { Activate on scroll of either of the .linkeds
var y = $(this).scrollLeft(); Save the scroll position
$('.linked').scrollLeft(y); Scroll both of them to that position
}); Close the block

When the top one is scrolled, this happens:

demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!