Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda