Home > database >  How to scroll items simultaneously when they are within a table?
How to scroll items simultaneously when they are within a table?

Time:11-24

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:

demo

CodePudding user response:

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>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related