Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda